Click to See Complete Forum and Search --> : stack allocation
mikledet
November 29th, 2002, 11:32 AM
Hi,
As far as I know, when allocating an array from heap like that: int var[100];, the array comes set all to 0's.
While when allocating from stack i.e: int *var;
var = new int[100];, I get the array in a (so far) free address space but with the junk that was inside.
1. Is this all true?
2. If it is (and if not as well) how can I allocate from the stack, and have the array "clean" with out the need to go manualy and setting all the array members to 0?
In other words, how can I allocate dinamicaly, fast, and "clean"?
Thanks in advance
Dani.
Yves M
November 29th, 2002, 11:56 AM
1. Yes
2. Allocating on the stack doesn't make all the entries equal to 0. You can use _alloca to allocate memory on the stack, but you still have to set all elements of the array to a default value. You can for example use memset(var, 0, sizeof(int) * 100);
Gabriel Fleseriu
November 29th, 2002, 12:30 PM
Originally posted by mikledet
Hi,
As far as I know, when allocating an array from heap like that: int var[100];, the array comes set all to 0's.
While when allocating from stack i.e: int *var;
var = new int[100];, I get the array in a (so far) free address space but with the junk that was inside.
1. Is this all true?
2. If it is (and if not as well) how can I allocate from the stack, and have the array "clean" with out the need to go manualy and setting all the array members to 0?
In other words, how can I allocate dinamicaly, fast, and "clean"?
It is exactly the other way around:
int i[100]; //allocates from the stack
int *i = new int[100]; //allocates from the free-store (not the same as the heap)
But yes, you are right, when you allocate from the free-store, the block contains garbage, if you are allocating built-ins, like int. You can zero-out the memory, for example with memset().
Dynamic allocation is slower than static allocation, mostly.
Yves M
November 29th, 2002, 12:44 PM
Ups, yes :)
As far as I know, when allocating an array from heap like that: int var[100];, the array comes set all to 0's.
this is not true.
int *i = new int[100]; //allocates from the free-store (not the same as the heap)
What ? Isn't it allocating memory on the heap ? Of course "heap" is an ill-defined word since the process heap (managed by Windows) is different from the "conceptual heap" which is managed by new / delete / malloc / free. But what is the "free-store" ?
Dynamic allocation is slower than static allocation, mostly.
Yes, but there are obvious and non-obvious cases when static allocation doesn't work.
Andreas Masur
November 29th, 2002, 01:26 PM
Originally posted by Yves M
What ? Isn't it allocating memory on the heap ? Of course "heap" is an ill-defined word since the process heap (managed by Windows) is different from the "conceptual heap" which is managed by new / delete / malloc / free. But what is the "free-store" ?
In general the terms 'heap' and 'free-store' are referring to the same...if you read many tutorials you will find this common description.
Nevertheless there are differences which are described in the following article (http://www.gotw.ca/gotw/009.htm)...
Yves M
November 29th, 2002, 02:35 PM
Thanks Andreas :) Gabriel pointed me to the description in Sutter's Exceptional C++ ;) (it's exactly the one on gotw :) )
But yes, ok the free-store and the "heap" are different in C++ but conceptually the only thing that is important is that new / free and m(r, c)alloc / delete don't make sense as combinations. Apart from that the heap and the free-store are handled in the same way and you can't assume any more about the "free-store" than about the "heap".
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.