You can do that in C# too. If you declare something in a static variable, even if it's normally allocated dynamically, it will not be garbage collected until the application terminates.
This still creates challenges for multi-threaded applications though. The performance you gain by allocating the memory statically is lost when you have to repeatedly lock/unlock the context for a thread's read/write operations.
This still creates challenges for multi-threaded applications though. The performance you gain by allocating the memory statically is lost when you have to repeatedly lock/unlock the context for a thread's read/write operations.
I think you're still misunderstanding. In C++ the default behavior when you add a member variable to a class, it becomes a part of the class's block of memory. For example:
class Entity { Vector3 position; //position value (as a rotation) Quaternion rotation; //rotation value (a quaternion) //etc. };
Vector3 and Quaternion, while being classes themselves, are not individually allocated. Internally, when you make a "new Entity()", it allocates a block of memory large enough for all it's contained data, so the Vector3, Quaternion, etc. instances it contains are allocated at the same time the Entity class is. Similarly, when you delete the Entity, the Vector3 and Quaternion destructors are called, then the entire memory block of Entity is deallocated in a single step.
This way if your Vector3 class takes 12 bytes, your Quaternion takes 16 bytes, etc., then creating an instance of the above Entity class performs one allocation of 28 bytes total. In BlitzMax, it would perform 3 allocations, one of 8 bytes (two 32-bit memory pointers to the position/rotation values), one of 12 bytes, and one of 16 bytes. BlitzMax/C#'s method is slower (3x the allocations in this case) and more memory consuming (36 bytes instead of 28 in this case), and this is a particularly "tame" example.
This has no negative effect on multi-threaded apps as far as I know. You're thinking of global variables.