CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Sep 1999
    Posts
    102

    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.

  2. #2
    Join Date
    Aug 2002
    Location
    Madrid
    Posts
    4,588
    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.

  3. #3
    Join Date
    Jun 2001
    Location
    Switzerland
    Posts
    4,443

    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.
    Gabriel, CodeGuru moderator

    Forever trusting who we are
    And nothing else matters
    - Metallica

    Learn about the advantages of std::vector.

  4. #4
    Join Date
    Aug 2002
    Location
    Madrid
    Posts
    4,588
    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.

  5. #5
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    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...

  6. #6
    Join Date
    Aug 2002
    Location
    Madrid
    Posts
    4,588
    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
  •  





Click Here to Expand Forum to Full Width

Featured