Allocating memory for a class

5 posts / 0 new
Last post
jwanderson88's picture
Last seen: 1 year 3 weeks 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),

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's picture
Last seen: 1 week 4 days 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's picture
Last seen: 4 weeks 23 hours 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's picture
Last seen: 1 year 3 weeks 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
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.

Stark544's picture
Last seen: 3 weeks 3 days ago
Joined: 2023-10-16 13:04
Re: Allocating memory for a class

Allocating memory for a class is a crucial step in the world of programming. It lays the foundation for dynamic and efficient object creation and manipulation. When we set aside memory resources for a class, we enable the creation of instances that can store and process data seamlessly. This memory allocation ensures that our classes can perform their designated tasks effectively. It's akin to providing a canvas for an artist to paint their masterpiece. Just like how connects individuals with valuable resources, allocating memory for a class links us to the potential of crafting elegant and functional code, thereby enhancing the overall software development process.

Log in or register to post comments