heap or stack?

This is a discussion on heap or stack? within the C++ Programming forums, part of the General Programming Boards category; Code: /*-----class1.cpp-----*/ #include <stdio.h> class Sample { public: int a, b; void fonk(void); }; void main(void) { Sample t; Sample* ...

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    61

    heap or stack?

    Code:
    /*-----class1.cpp-----*/
    #include <stdio.h>
    class Sample {
    public:
    int a, b;
    void fonk(void);
    };
    void main(void)
    {
    Sample t;
    Sample* T=new Sample();
    t.a;
    T->a;
    }


    What are the differences between t and tnew?
    I heard that t is located in stack but tnew is located in heap.Is this true or enough?

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,798
    There is no tnew. You mean what's the difference between t and T?

    Both t and T are located on the stack. The difference is that T is a pointer that happens (in this instance) to point to an area of memory located on the heap while the memory area occupied by t is on the stack.

    BTW, main returns an int.
    Last edited by hk_mp5kpdw; 01-23-2006 at 03:56 PM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Jan 2006
    Posts
    61
    Quote Originally Posted by hk_mp5kpdw
    There is no tnew. You mean what's the difference between t and T?

    Both t and T are located on the stack. The difference is that T is a pointer that happens (in this instance) to point to an area of memory located on the heap while the memory area occupied by t is on the stack.

    BTW, main returns an int.
    ok.Thanks, i wrote it very fast.firstly i correct the code
    Code:
    #include <cstdio>
    #include <cstdio>
    class Sample {
    public:
    int a, b;
    void fonk(void);
    };
    int main()
    {
    Sample t;
    Sample* tnew=new Sample();
    t.a;
    tnew->a;
    return 0;
    }
    ok.
    I understand that ,in my codes:
    tnew->a and tnew->b are all located in heap.And its starting address(tnew) is stored in stack.
    for Sample t , t and also its members are stored in stack.
    Is this true?
    If i am wrong or my answer is not enough.Please advice me.
    Thanks for answerr

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,798
    Yes.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Registered User
    Join Date
    Jan 2006
    Posts
    61
    ok thanks...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. stack and pointer problem
    By ramaadhitia in forum C Programming
    Replies: 2
    Last Post: 09-11-2006, 11:41 PM
  2. Question about a stack using array of pointers
    By Ricochet in forum C++ Programming
    Replies: 6
    Last Post: 11-17-2003, 09:12 PM
  3. error trying to compile stack program
    By KristTlove in forum C++ Programming
    Replies: 2
    Last Post: 11-03-2003, 05:27 PM
  4. heap question
    By mackol in forum C Programming
    Replies: 1
    Last Post: 11-30-2002, 04:03 AM
  5. Stack Program Here
    By Troll_King in forum C Programming
    Replies: 7
    Last Post: 10-15-2001, 05:36 PM

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