initial values of data members of objects on the stack vs heap

This is a discussion on initial values of data members of objects on the stack vs heap within the C++ Programming forums, part of the General Programming Boards category; Hi, I noticed a strange behaviour when converting my code from dynamically allocating objects to declaring the objects on the ...

  1. #1
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,183

    initial values of data members of objects on the stack vs heap

    Hi,
    I noticed a strange behaviour when converting my code from dynamically allocating objects to declaring the objects on the stack. It seems that members in dynamically allocated objects are initialized to zero, while members of objects declared on the stack carry random values. Is it defined behaviour or is it implementation specific? (I observed this behaviour with GCC 4.1 on Linux)

    I wrote a simple test program as an example.
    Code:
    #include <iostream>
    
    using std::cout;
    using std::endl;
    
    class A {
            public:
                    int x;
    };
    
    int main() {
            A *A_ptr = new A;
            cout << A_ptr->x << endl; //always prints "0"
    
            A a;
            cout << a.x << endl; //prints a different value every time
    }
    Thank you very much.

  2. #2
    </3 Segfaults
    Join Date
    Jul 2007
    Posts
    27
    This info would be interesting to me, too.

    My high school programming teacher said that when you declare variables, they contain whatever was left in them from the previous program. However, I'm not sure if that still applies with all computers/some computers/no computers, because his career ended pretty long ago.

    Try writing a program that takes up all the memory and fills it with zeroes. Close it, and immediately initialize your example program and see if it outputs more zeroes.

    Mine always outputs 134518684 for ints (unsigned).
    Last edited by Differ; 07-30-2007 at 11:17 PM.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,304
    For A_ptr, the default constructor for A is invoked. This would default initialise the members, and thus the int member is zero initialised. As such, I believe that this is standard behaviour.

    EDIT:
    Oh wait, something is wrong with my reasoning. For a, the default constructor should be invoked, and this should result in the same behaviour as for A_ptr. hmm...

    Oh, I understand: A is a POD type in "disguise" (I am more used to using struct instead of class for a POD struct). As such, x really is left uninitialised. I am wrong for A_ptr as well, in that case: that x is zero initialised is implementation defined, since its value is indeterminate in both cases.
    Last edited by laserlight; 07-30-2007 at 11:20 PM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    920
    Why is the constructor not called for automatic built-in types?


    *edit* Googling got me this
    Just as with malloc() the memory returned by new is not initialized; only static memory has a default initial value of zero.
    from this: http://www.acm.org/crossroads/xrds1-1/ovp.html
    which, it seems, is empirically disproved.
    Last edited by CodeMonkey; 07-30-2007 at 11:19 PM. Reason: typo
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  5. #5
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,183
    Try writing a program that takes up all the memory and fills it with zeroes. Close it, and immediately initialize your example program and see if it outputs more zeroes.
    hmm, I don't know if I know how to write such a program. If you are referring to something like:
    Code:
    char *ptr;
    while (ptr = malloc(sizeof(char))) {
         *ptr = 0;
    }
    last time I tried it, it crashed my computer =).

    Oh, I understand: A is a POD type in "disguise" (I am more used to using struct instead of class for a POD struct). As such, x really is left uninitialised. I am wrong for A_ptr as well, in that case: that x is zero initialised is implementation defined, since its value is indeterminate in both cases.
    I see. Thank you for clarifying.

    Just as with malloc() the memory returned by new is not initialized; only static memory has a default initial value of zero.
    from this: http://www.acm.org/crossroads/xrds1-1/ovp.html
    which, it seems, is empirically disproved.
    I am not sure if I got what the author meant. is static memory referring to the stack?

    Thank you

  6. #6
    </3 Segfaults
    Join Date
    Jul 2007
    Posts
    27
    Code:
    int *ptr;
    ptr = new int[5000];

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    A class that doesn't have a constructor will not have a "constructor call" - or at least, the constructor will not fill any of the data inside the class.

    Any data on the stack will have random content when the function is entered, no matter if it's a class or some other data.

    new is generally based on malloc (or at least, it's normally using the same underlying mechanisms), and whilst malloc isn't guaranteed to clear the allocated memory, it is normally how it works. I've also seen debug versions of malloc that fill memory with "0xCC" or "0xAA" or some such, to make it easier to identify when using un-initialized data (zero tends to be pretty common, so if something is zero, you don't really know if it was set to zero or just "happened to be zero").

    It is definitely good practice to either have a constructor, or fill the data by hand after using malloc/new.

    But in practice, both methods of getting memory for the object (on stack or via new), both leave x as "undefined".

    --
    Mats

  8. #8
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,183
    int *ptr;
    ptr = new int[5000];
    it doesn't change anything on my system (still random values)

    A class that doesn't have a constructor will not have a "constructor call" - or at least, the constructor will not fill any of the data inside the class.

    Any data on the stack will have random content when the function is entered, no matter if it's a class or some other data.

    new is generally based on malloc (or at least, it's normally using the same underlying mechanisms), and whilst malloc isn't guaranteed to clear the allocated memory, it is normally how it works. I've also seen debug versions of malloc that fill memory with "0xCC" or "0xAA" or some such, to make it easier to identify when using un-initialized data (zero tends to be pretty common, so if something is zero, you don't really know if it was set to zero or just "happened to be zero").

    It is definitely good practice to either have a constructor, or fill the data by hand after using malloc/new.

    But in practice, both methods of getting memory for the object (on stack or via new), both leave x as "undefined".
    Thank you for the information. I was expecting both methods to produce undefined memory (from what I read somewhere else), but was surprised that gcc's implementation of new actually zeros the memory. Thank you for clarifying.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Buffers , heaps , stacks ...
    By BlaX in forum Tech Board
    Replies: 9
    Last Post: 02-17-2009, 03:09 PM
  2. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 11:13 AM
  3. Question about a stack using array of pointers
    By Ricochet in forum C++ Programming
    Replies: 6
    Last Post: 11-17-2003, 10:12 PM
  4. error trying to compile stack program
    By KristTlove in forum C++ Programming
    Replies: 2
    Last Post: 11-03-2003, 06:27 PM
  5. What is a Stack, Heap and Queue ?
    By pritesh in forum C Programming
    Replies: 3
    Last Post: 03-17-2002, 12:16 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21