Skip to main content
GameDev.net gamedev.net
🔒 Locked

Memory allocation methods

Started by Billy Lee Nov 5, 2004 at 9:08 AM 5 replies 1.2k views
Original Post
Billy Lee
Billy Lee
Something that I've been a little confused about: I've heard of the words dynamic and (I think) static memory allocation bandied about in reference books and programming sites. Are these the same as pointer-based and handle-based memory allocation? Also, when are pointer and handle-based memory allocation used and what are the advantages/disadvantages of each? My understanding is that windows programming is handle-based. Am I right?
nmi
nmi
No

Static memory allocation occurs during compile-time. Statically allocated memory are global variables and static class members.

Dynamic memory allocation occurs during run-time. When talking about dynamically allocated memory usually the heap is meant. However also the stack is some very fast but very simple memory manager, also doing dynamic allocation.

Pointer-based memory allocation returns a pointer to the memory region where the data is stored whereas handle-based memory allocation only returns an identifier. Smart-pointers are also some kind of handle-based memory allocation, if you like.

Advantages of handle-based allocation is the indirection when accessing the memory, e.g. the data may be moved to another location and your application does not notice (e.g. used in garbage collectors). But this indirection needs extra time and space.


chollida1
chollida1
Just a small point on the last post, global variables aren't necessarily stack based or allocated at compile time.

YOu can easily have a global pointer say

CMyClass * pmyGlobal_G;

and in any method at runtime call

pmyGlobal_G = new CMyClass();

or

delete pmyGlobal_G;
pmyGlobal_G = NULL;

YOu might have meant that nmi but I just wanted to clarify that globals can be allocated at runtime:)

CheersChris
Billy Lee
Billy Lee
Thanks for clearing that up. :)
Agony
Agony
Quote:
Original post by chollida1
Just a small point on the last post, global variables aren't necessarily stack based or allocated at compile time.

...

Technically, they are indeed statically allocated. In your example, the pointer
pmyGlobal_G
is a statically allocated global variable. It may point to a dynamically allocated chunk of memory, but just considering the pointer itself, it is statically allocated.

Also, local variables are statically allocated.

Basically, if you use new or malloc(), the memory is dynamic. Otherwise, it is static. (Often times, you might be using dynamic memory, but not realize it, because you are using a library or someone else's code that allocates the memory dynamically, and then informs you of it. So either new or malloc() are still used (or some other OS-dependent method), you just don't see it.)
"We should have a great fewer disputes in the world if words were taken for what they are, the signs of our ideas nly, and not for things themselves." - John Locke o
chollida1
chollida1
Agony

100% correct. I just didn't want him thinking that you couldnt' dynamically allocate a global variable representation:), meaning the memory it points to:)

Cheers
Chris
CheersChris
chollida1
chollida1
and I guess I should point out that not all stack variables are statically allocated. alloca allocates variables at runtime on the stack, although its completely non portable and compiler dependant:)

Cheers
Chris
CheersChris

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.