PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

Classes and memory usage in C++

I've just noticed that when a class is created it only allocates the amount of memory required to hold the class properties. Does this mean I could manually make a 'new' class by having a structure with the same properties as a class, set it's values and then cast a pointer to the class to that bit of memory?

Code:

class Test
{
private:
int Priv1;
public:
int Prop1, Prop2, Prop3;
}

struct
{
int Priv1, Prop1, Prop2, Prop3;
} TestStruct;

TestStruct arrayOfClasses[20];


At the moment my list class uses HeapAlloc and allocs in variable size chunks as required, but I am wondering if I can also allocate a block of actual classes in the same way because at the moment I have typically a couple of thousand "new" calls for a game, but with the lists those items are stored in being preallocated.
joshbu [MSFT] · 28 Apr 2004, 03:31 UTC
Well, the sizeof(Test) == sizeof(TestStruct) so long as you don't add a virtual function to Test. Also, now you have to remember to modify TestStruct every time you modify Test. It's just not a good solution from a software engineering/long term maitience point of view.

The mo better way of doing custom allocation with C++ is to override the new operator. You can override new on a per-object or on a global scope. These override functions could call out to external memory allocators that implement pools or some other small-block strategy.

There's a great writeup of this in Effective C++ by Scott Meyers.
Thanks joshbu,

I've seen others talk about overriding new as well, so I think it's time to investigate.
There is a syntax called "placement new" which will allow you to construct an object into existing, preallocated memory. I'm not sure if it is supported in eVC3 or eVC4, but if it is, that's another way to do what you want, without writing your own new operator.

Don't do arcane casting tricks. They are dangerous and there are other, proper ways to do what you want.
Implementing operator new (and delete) inside his classes is the best (and cleaness) way to do it. Placement new would be the hack to stay away from.
Thierry wrote:Implementing operator new (and delete) inside his classes is the best (and cleaness) way to do it. Placement new would be the hack to stay away from.
Why is it a hack? Most STL implementations I've looked at are implemented like this...

Just asking... obviously having class specific new/delete is the simpler approach if it works for the problem at hand.

- HM
hm wrote:Why is it a hack? Most STL implementations I've looked at are implemented like this...


Fair enough. But when you use the STL you don't directly deal with these issues. Everything is wrapped and transparent to the user. So unless you intend on doing something similar, it's better to not use this method.

hm wrote:Just asking... obviously having class specific new/delete is the simpler approach if it works for the problem at hand.- HM


That's my point. This way you don't have to keep track of what was allocated with placement-new when deleting something. Using this method, you don't have to change any code that creates/destroy objects. Not so with placement new/delete.

I really see placement new/delete as facilities to build memory manager (and the STL is a good example), not something to be used everywhere in your program. Overriding operator new and delete for your class is much cleaner and can be enabled/disabled easily without having to change the code everywhere.
joshbu [MSFT] · 29 Apr 2004, 17:12 UTC
I believe once you start using placement new, you have to manually call your destructor, which would be non-standard enough to make it worth staying away from unless you really need it.
joshbu [MSFT] wrote:I believe once you start using placement new, you have to manually call your destructor, which would be non-standard enough to make it worth staying away from unless you really need it.


Precisely.
I only pointed out placement new as the "proper" way to do what the original poster wanted. I didn't claim that what the original poster wanted to do was itself the recommended approach.
mlepage wrote:I only pointed out placement new as the "proper" way to do what the original poster wanted. I didn't claim that what the original poster wanted to do was itself the recommended approach.


Don't feel attacked mlepage, you are not. I am simply explaining the alternative you hinted at and why it is better. (For the benefit of the OP and any other interested in this discussion.)
Nope I don't feel attacked, just explaining my original intent. My point was that the cast was bad, that even if that is exactly what you want to do, there is a non-cast way to do it.

Overriding new and delete is another way to do it.

A third way is to just have a fixed pool of objects which are sometimes initialized and used, and sometimes unused (but still existing). This is what Quake does.