Re: Object Oriented Programming in C, Avoid Memory Allocation

From: Derrick Coetzee (dcnews_at_moonflare.com)
Date: 09/04/04


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.


Relevant Pages

  • Re: When to check the return value of malloc
    ... when constructing a Workforce, we set up the names array correctly, and ... struct workforce { ... Since two containers need allocation, ... struct employee* employees; ...
    (comp.lang.c)
  • Re: Listview and colored line
    ... "lParam" all works fine. ... Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _ ... OldProc = SetWindowLong(hWnd, GWL_WNDPROC, AddressOf WindowProc) ... ' Struct of the NMCUSTOMDRAW struct ...
    (microsoft.public.excel.programming)
  • Re: IT ethics in the workplace
    ... Is personal email on company computers private or not? ... } demolish its own buildings but that does not give every employee the right ... }>places your wife could've gotten fired for misuse of company equipment. ...
    (comp.security.misc)
  • Re: byte size packing in compact framework
    ... private byte ser0; ... private uint testuint; ... public byte TestByte ... I have a simple struct: ...
    (microsoft.public.dotnet.framework.interop)
  • Re: NRA boycot of ConocoPhillips includes Union 76 and CHEVRON!
    ... companies have always set the conditions for employee use of company parking lots. ... And that tends to limit their responsibility for protecting their workers from an employee who has guns parked somewhere in his car and who goes for those guns when upset with his co-workers.. ... I have a problem when the NRA calls for legislation to override private ...
    (talk.politics.guns)