A
Andrew Au
Dear all,
I am trying to write a piece of software that use Object Oriented design
and implement it with C, I did the following
== In Object.h ==
typedef struct ObjectStructure* Object;
Object object_create();
int object_getIntegerAttribute(Object object);
void object_setIntegerAttribute(Object object, int integerAttribute);
void object_unload(Object object);
== In Object.c ==
#include <stdlib.h>
struct ObjectStructure {
int integerAttribute;
};
Object object_create() {return (Object)malloc(sizeof(struct
ObjectStructure));}
int object_getIntegerAttribute(Object object) {return
object->integerAttribute;}
void object_setIntegerAttribute(Object object, int integerAttribute)
{object->integerAttribute = integerAttribute;}
object_unload(Object object) {free object}
// I am afraid if the above code even compile, since I am simply writing the
code in the news client without a compiler.
While it is good to encapsulate the Object's structure, now, I must always
create the object in a dynamic memory allocation way (obviously, coz' the
outside world has no idea of the structure's size).
Now I am in dillema, I would like to have encapsulation for the object, so
that further changes to the Object does not affect the rest of the code, but
at the same time, I would like to avoid dynamic memory allocation as much as
possible, following the rule that ONLY unknown size of memory (p.s. I don't
have very large memory block) should be dynamically allocated, and any
pieces of dynamic memory should be reused as much as possible. (For
performance issue)
What should I do? At the same time, I would like to seek method refactor my
code so as to minimize memory allocation, are there any suggestion?
Memory Pool is great, and I am already using them.
I am trying to write a piece of software that use Object Oriented design
and implement it with C, I did the following
== In Object.h ==
typedef struct ObjectStructure* Object;
Object object_create();
int object_getIntegerAttribute(Object object);
void object_setIntegerAttribute(Object object, int integerAttribute);
void object_unload(Object object);
== In Object.c ==
#include <stdlib.h>
struct ObjectStructure {
int integerAttribute;
};
Object object_create() {return (Object)malloc(sizeof(struct
ObjectStructure));}
int object_getIntegerAttribute(Object object) {return
object->integerAttribute;}
void object_setIntegerAttribute(Object object, int integerAttribute)
{object->integerAttribute = integerAttribute;}
object_unload(Object object) {free object}
// I am afraid if the above code even compile, since I am simply writing the
code in the news client without a compiler.
While it is good to encapsulate the Object's structure, now, I must always
create the object in a dynamic memory allocation way (obviously, coz' the
outside world has no idea of the structure's size).
Now I am in dillema, I would like to have encapsulation for the object, so
that further changes to the Object does not affect the rest of the code, but
at the same time, I would like to avoid dynamic memory allocation as much as
possible, following the rule that ONLY unknown size of memory (p.s. I don't
have very large memory block) should be dynamically allocated, and any
pieces of dynamic memory should be reused as much as possible. (For
performance issue)
What should I do? At the same time, I would like to seek method refactor my
code so as to minimize memory allocation, are there any suggestion?
Memory Pool is great, and I am already using them.