Thread: C++ Object heap stack

  1. #1
    Its not rocket science vasanth's Avatar
    Join Date
    Jan 2002
    Posts
    1,683

    C++ Object heap stack

    I have a doubt .. consider the below situation

    Code:
    object a;
    object b;
    b=a;

    now what happens to the memory thats allocated to object b ... and am I right in saying that the memory for the objects in the above example gets created on the stack..

    thanx in advance

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    It depends on the scope of the definition.
    http://www.cppworld.com/forum/index....t=0&#entry4236
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    When you define the object b (object b;), storage is allocated for b. When you later initialize it (b = a), you simply access the memory space of b and insert the value of a.

    However, you don't use the heap. The heap (or free store) is used when you dynamically allocate memory with the use of the new expression.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  4. #4
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    That post at cppworld confuses me...

    Isn't what the author calls "global data" also part of the stack?
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    If you are talking about an object of class type, it also depends on behaviour of the class. If the class assigns memory dynamically, some memory is on the "stack" (in common parlance, which I consider rather useless) and some is not.

    The assignment "b=a" will typically invoke a function named object::operator=() which takes a reference to object as an argument. The catch is that the compiler supplied default version of that function (supplied if the programmer doesn't supply one) simply copies individual members of a into b by value. That can cause errors if the class manages (say) memory resources: in that case the assignment operator needs to ensure that the assignment does not (direclty or indirectly) cause a memory leak or undefined behaviour.

    For example;
    Code:
    class object
    {
        public:
            object() : x(new int) {};
            ~object() {delete x;};
        private:
            int *x;
    };
    
    int main()
    {
        object a;
        object b;
        b = a;
    }
    will cause undefined behaviour, as the result of the assignment is object's destructor will destroy the same memory (a.x) twice (once when a is destroyed, once when b is destroyed). This needs to be fixed, for example;
    Code:
    class object
    {
        public:
            object() : x(new int) {};
            object &operator=(const object &a) {*x = *(a.x);};
            ~object() {delete x;};
        private:
            int *x;
    };
    
    int main()
    {
        object a;
        object b;
        b = a;
    }
    Last edited by grumpy; 06-08-2006 at 05:49 PM. Reason: Disable smilies

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory allocation from stack and heap ?
    By Bargi in forum C++ Programming
    Replies: 5
    Last Post: 05-29-2008, 12:37 AM
  2. stack and heap objects
    By John_L in forum C++ Programming
    Replies: 4
    Last Post: 03-18-2008, 10:20 AM
  3. heap question
    By mackol in forum C Programming
    Replies: 1
    Last Post: 11-30-2002, 05:03 AM
  4. Replies: 6
    Last Post: 10-30-2002, 09:03 PM
  5. Stack and Heap
    By Barjor in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 01-29-2002, 09:11 AM