Link to home
Start Free TrialLog in
Avatar of agon
agon

asked on

OOP implementation on ANSI C

Hi guys!  I've been programming using ANSI C for awhile.  Then one day I thought if I could implement the OOP concept on ANSI C, for interest, and searched for some articles.  The most detailed article I've found is by Axel-Tobias-Schreiner, although I cannot completely understand its contents.  So I tried to mimic the sample codes and use the study-by-code approach.  However, it says "Segmentation fault (core dumped)" each time I try to run my sample program.  It'd be appreciated if anyone can point out where it goes wrong.


#include<stdio.h>

typedef struct {
/* Properties */
      unsigned long      length;
      char *            addr;
/* Methods */
      void      (* setString)(void * self, char * strIn);
      char *      (* getString)(void * self);
} String;

void setString(void * self, char * strIn)
{
      String * str = (String *) self;
      unsigned long str_size = strlen(strIn);/* determine length */
      
      str->addr = (char *) malloc(str_size + 1);
      strcpy(str->addr, strIn);/* copy the actual content */
}

char * getString(void * self)
{
      String * str = (String *) self;
      
      return str->addr;
}

int main(int args, char * argv[])
{
      String strA;
      
      strA.setString(& strA, "Example");
      printf("[%s]\r\n", strA.getString(& strA));
      
      return 0;
}


p.s.  As I've figured out, if I remove the "strA." before the setString() and getString(), which means calling the function directly, not through the "typedef String", the program runs perfectly.  But if I do that, it loses the meaning of implementing the OOP concept, because a method SHOULD belong to a class.  Perhaps I should do something about the two lines in main().
Avatar of shajithchandran
shajithchandran
Flag of India image

U r getting the error because u haven't initialised the pointer to the function in the structure.
the pointer setString and getString are uninitialised.
ASKER CERTIFIED SOLUTION
Avatar of stsanz
stsanz

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of martynjpearson
martynjpearson

The problem is that the struct has two variables which are pointers to functions. However, they are never initialised to point to the implementations of the functions.

The simple way to fix this is by adding these two lines after the line "String strA;" :

    strA.setString = setString;
    strA.getString = getString;

Hope this helps
Martyn
Do this
strA.setString=setString;
strA.getString=getString;

before invoking the function.

shaj
Oh god i just missed it!!

Avatar of agon

ASKER

First of all, I want to thank all of you!
I'm very surprised that there are people knowing this, and the responses are sooooo fast!!!
coz the majority is just going for the OOP languages directly. (well, at least the people around me)
It seems I should try to understand the article better before I post.  hehehe...  ^_^!
No problems - obviously shajithchandran and stsanz are faster typers than me though :)
>>I'm very surprised that there are people knowing this, coz the majority is just going for the OOP languages directly
Many people who know C++ have first known C.
Thanks for the points!
Avatar of agon

ASKER

By the way, I heard about the split-score feature, do you guys know how to use it?
This should help you with splitting points : https://www.experts-exchange.com/help/closing.jsp#3

Martyn