|
-
November 29th, 2002, 12:32 PM
#1
stack allocation
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.
-
November 29th, 2002, 12:56 PM
#2
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);
Get this small utility to do basic syntax highlighting in vBulletin forums (like Codeguru) easily.
Supports C++ and VB out of the box, but can be configured for other languages.
-
November 29th, 2002, 01:30 PM
#3
Re: stack allocation
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:
Code:
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.
-
November 29th, 2002, 01:44 PM
#4
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.
Get this small utility to do basic syntax highlighting in vBulletin forums (like Codeguru) easily.
Supports C++ and VB out of the box, but can be configured for other languages.
-
November 29th, 2002, 02:26 PM
#5
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...
-
November 29th, 2002, 03:35 PM
#6
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".
Get this small utility to do basic syntax highlighting in vBulletin forums (like Codeguru) easily.
Supports C++ and VB out of the box, but can be configured for other languages.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|