Advertisement Jump to content
Sign in to follow this  

memory allocation query

This topic is 4962 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Well I don't know what you mean by "dynamically on the stack", since you usually create objects dynamically on the heap. I assume you use C++.
You can, however, use the placement new operator to create objects dynamically on stack memory. Please note that this is dangerous and should be used with care (e.g. use it only when you know what you are doing!).

#include <new>

// some memory block allocated on the stack
char globalMemoryOnStack[GLOBAL_MEMORY_SIZE];

// some class - I left out declaration and definition.
class A;

void SomeFunc() {

// use placement-new to allocate an object of type "A" dynamically on the stack
A * a = new(globalMemoryOnStack) A;

// use A and don't call "delete" on it! call the destructor explicitly instead:


PS: And be careful, there're only a few valid instances where you actually want to use this method.

Share this post

Link to post
Share on other sites
You can use alloca to allocate the memory on the stack, then do as darookie suggested:

A * a = new(alloca(sizeof(A))) A;

(If your compiler does not support alloca, you can easily write one by yourself.)

Share this post

Link to post
Share on other sites
Guest Anonymous Poster
Original post by makingroulette
How do you create instances of an object dynamically on the stack? can you give an a code example
Your question is unclear to me. Normally you either keep an object on the stack, or you allocate it dynamically.

To allocate memory from the stack there's a function called alloca or _alloca, but you should be careful when using it. For instance, it's typically a bad idea to store user input of arbitrary size on the stack, since it's a flaw that could cause stack overflow.

Share this post

Link to post
Share on other sites
I asked the question because I was reading a book called Microsoft Visual C++ .NET Step By Step page 132 The .NET approach to controlling object lifetimes

Heres the extract that is puzzling me, the sentence starting with 'Unmanaged'

Using the .NET mechanism, you still create objects dynamically using the new operator, but the system is responsible for deleting objects, not you. The system keeps track of references to objects, and when an object is no longer being referenced by anyone, it becomes a candidate for garbage collection.

This has more consequences for the programmer than you might at first think:

Objects are always created using new. Unmanaged code lets you create objects on the heap (as automatic variables) or dynamically on the stack. For garbage collection to work, objects have to be accessed through some sort of reference, which in C++ is a pointer.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!