Re: Object Oriented Programming in C, Avoid Memory Allocation
From: Derrick Coetzee (dcnews_at_moonflare.com)
Date: 09/04/04
- Next message: Derrick Coetzee: "Re: Object Oriented Programming in C, Avoid Memory Allocation"
- Previous message: Jens.Toerring_at_physik.fu-berlin.de: "Re: How does free() knows?"
- In reply to: Andrew Au: "Object Oriented Programming in C, Avoid Memory Allocation"
- Next in thread: Derrick Coetzee: "Re: Object Oriented Programming in C, Avoid Memory Allocation"
- Reply: Derrick Coetzee: "Re: Object Oriented Programming in C, Avoid Memory Allocation"
- Reply: Derrick Coetzee: "Re: Object Oriented Programming in C, Avoid Memory Allocation"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Sat, 04 Sep 2004 11:54:44 -0400
Andrew Au wrote:
> 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 [ . . . ]
This is a concern, since static allocation is more efficient (especially
the freeing part) and many calls to functions using such objects locally
could start to have an impact. Fragmentation on the other hand is not a
concern, since this could be fixed with pools or by using a better
memory allocator.
Here's a little trick I once came up with that lets you create both
public and private members in a C data structure. First put something
like this in your header file:
struct employeePrivate {
int age;
int salary;
};
typedef struct {
char* name;
char privateData[sizeof(struct employeePrivate)];
} Employee;
Employee objectCreate();
In other source files, they can access the public "name" element
directly, but in order to touch the private data they would have to
explicitly mess with the privateData array, which is unlikely to happen
accidentally (and easy to locate if someone does it). In the object's
implementation source file, you place this macro:
#define pri(obj, field) \
(((struct employeePrivate *)(&((obj).privateData)))->field)
Used like this:
Employee emp = ...;
pri(emp, salary) = pri(emp, age) * 1000;
These accesses will be just as efficient as normal accesses of struct
members, once the compiler has worked things out. All this code is
untested though. I hope this helps.
-- Derrick Coetzee I grant this newsgroup posting into the public domain. I disclaim all express or implied warranty and all liability. I am not a professional.
- Next message: Derrick Coetzee: "Re: Object Oriented Programming in C, Avoid Memory Allocation"
- Previous message: Jens.Toerring_at_physik.fu-berlin.de: "Re: How does free() knows?"
- In reply to: Andrew Au: "Object Oriented Programming in C, Avoid Memory Allocation"
- Next in thread: Derrick Coetzee: "Re: Object Oriented Programming in C, Avoid Memory Allocation"
- Reply: Derrick Coetzee: "Re: Object Oriented Programming in C, Avoid Memory Allocation"
- Reply: Derrick Coetzee: "Re: Object Oriented Programming in C, Avoid Memory Allocation"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|
|