Allocating memory for a class

4 posts / 0 new
Last post
jwanderson88
jwanderson88's picture
Offline
Last seen: 1 year 4 months ago
Joined: 2019-04-13 19:29
Allocating memory for a class

This is a general C++ question, I think.

If you allocate memory for a class and the class has functions, in other words the class has code, there are couple of questions. Actually every class has code because there is a constructor and destructor.

gameterrain = (class TerrainElement *)IExec->AllocVecTags(sizeof(TerrainElement),
AVT_Type, MEMF_SHARED,
TAG_DONE);

1. Does it allocate memory for the functions?
2. Does it copy the code for the function to the memory?
3. Where does the code come from? Do you end up with two copies of the code?

thomas
thomas's picture
Offline
Last seen: 1 day 6 hours ago
Joined: 2011-05-16 14:23
Re: Allocating memory for a class

The code of classes is part of your program. It is loaded together with your main routine before your program starts. It exists only once. It is reentrant which means it can run multiple times with different data instances. It is not replicated.

To allocate an instance of a class in your program you normally would just declare it as a variable.

TerrainElement gameterrain;

This kind of variable is local to the subroutine it is declared in and will be freed at the end of the routine.

If you want to allocate it explicitly and let it live longer, then you would use the new operator.

TerrainElement *gameterrain = new TerrainElement;

Both declaration and the new operator will automatically allocate memory for the declared data portion of the class and call the constructor which can do further initalization. The constructor can also call AllocVecTags if the class needs additional memory. This memory then must be freed in the destructor because the C++ runtime does not know about it.

You won't ever call AllocVecTags to allocate memory for an instance of a class object. This has to be done by the runtime.

hypex
hypex's picture
Offline
Last seen: 1 month 2 weeks ago
Joined: 2011-09-09 16:20
Re: Allocating memory for a class

Does that compile? I've not done my own C++ programming on OS4 so don't know how it would react. It looks strange feeding a class to AllocVecTags() as it would expect a struct. Can you give give a class to a specific C function in C++? OS4 C does support the APICALL operator where a struct can be re-purposed as a virtual class (so to speak) with virtual methods embedded as functions that pass the object as the first argument. But if using C++ you won't need any C++ emulation tricks. You just need a mechanism to back call OS C functions.

jwanderson88
jwanderson88's picture
Offline
Last seen: 1 year 4 months ago
Joined: 2019-04-13 19:29
Re: Allocating memory for a class

I know you can overload the "new" operator by defining your own. I've done it lots of times and it seems to work. Here's a typical piece of code:

class CBitmap
{
public:
CBitmap();
~CBitmap();
BOOL CreateCompatibleBitmap(CDC* pDC, int nWidth, int nHeight);
BOOL CreateBitmap(int nWidth, int nHeight, UINT nPlanes, UINT nBitcount, const void* lpBits);
BOOL CreateBitmapIndirect(LPBITMAP lpBitmap);
void* operator new(size_t size);
void operator delete(void *deadObject, size_t size);
struct BitMap* cbm;
int wid, hgt;
int GetBitmap(BITMAP* pBitMap);
};

void* CBitmap::operator new(size_t size)
{
CBitmap* nbm;
nbm = (CBitmap*)IExec->AllocVecTags(sizeof(class CBitmap), TAG_END);
nbm->cbm = NULL;
return nbm;
}

It works.

Of course you have to deallocate the memory yourself when you are done.

Log in or register to post comments