Pointers and the free store

This is a discussion on Pointers and the free store within the C++ Programming forums, part of the General Programming Boards category; Hey guys...just had a late night question to ask...if you create a pointer and you assign it to an address ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    153

    Pointers and the free store

    Hey guys...just had a late night question to ask...if you create a pointer and you assign it to an address on the free store....do you actually have the local pointer itself on the stack even though it points to memory on the heap? Also...(and I'm pretty sure this one would be poor programming practice, but just hypothetically)if you create a pointer to memory on the heap, then free that memory and then assign the pointer to a local variable of some sort...is the pointer suddenly converted to a variable on the stack? Just curious...thanks! - Chap

  2. #2
    Registered User
    Join Date
    Sep 2004
    Posts
    197
    Well, depends on where the pointer is, if its say a global variable, or in a function (main for example), then it probably is on the stack, but if its part of a class, and you dynamicly allocate an object of the class, the pointer that is part of the object on the "free store" as you called it (some one correct me if I am wrong, I think this is the same thing as the Heap) will be with it on the freestore. Now if the pointer to that object is inside of a function like main which uses its own stack, that one I think would be there.

    On your second question, it doesn't matter where the pointer points, its always just pointing to an address, it doesn't care about where the variable its pointing to is. Actually unless your using a class to wrap pointers, they are actually quite "dumb" as (and my knowledge is limited) they are just a variable that stores the address in an integer style format, although how big it is is dependent on the archetecture. what I mean by dumb is they don't check themselves or any thing, which is why you get memory leaks unless the runtime (or compiler, again my knowledge is limited) has some sort of garbage collection built in.

    Also, every one who reads this post, please read my sig and help me out if I screwed up.
    If any part of my post is incorrect, please correct me.

    This post is not guarantied to be correct, and is not to be taken as a matter of fact, but of opinion or a guess, unless otherwise noted.

  3. #3
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    I'd say, Xipher, you nailed it.

    (and I'm pretty sure this one would be poor programming practice, but just hypothetically)if you create a pointer to memory on the heap, then free that memory and then assign the pointer to a local variable of some sort
    I don't see why not; it won't happen often (at least, scenarios where it would be 'good' practice won't), but I can imagine doing something like that in order to make part of my code more widely usable.

    is the pointer suddenly converted to a variable on the stack?
    As Xipher has explained, the pointer is simply another variable; where it is declared is where it will stay.
    Code:
    int* pInt = new int;  //pInt is a variable on the stack, pointing to a variable on the heap.
    
    int** ppInt = new int*;  //ppInt is a pointer on the stack, pointing to a pointer on the heap.
    *ppInt = new int;  //*ppInt is a pointer on the heap, pointing to another variable on the heap.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  4. #4
    Registered User
    Join Date
    Sep 2004
    Posts
    197
    Quote Originally Posted by Hunter2
    I'd say, Xipher, you nailed it.
    Wow, I actually got something right for a change, lol.
    If any part of my post is incorrect, please correct me.

    This post is not guarantied to be correct, and is not to be taken as a matter of fact, but of opinion or a guess, unless otherwise noted.

  5. #5
    Registered User
    Join Date
    Aug 2003
    Posts
    470
    Well, depends on where the pointer is, if its say a global variable, or in a function (main for example), then it probably is on the stack, but if its part of a class, and you dynamicly allocate an object of the class, the pointer that is part of the object on the "free store" as you called it (some one correct me if I am wrong, I think this is the same thing as the Heap) will be with it on the freestore.
    Xipher, if you have global or static pointer, the pointer will usually be stored inside the program's data or bss segment and the memory for which the pointer points to will be stored inside the heap. The rest of what you said seems correct to me.

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Maybe somewhat of a tangent, but this thread reminded me of this post.
    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.*

  7. #7
    Registered User
    Join Date
    Sep 2004
    Posts
    197
    Quote Originally Posted by okinrus
    Xipher, if you have global or static pointer, the pointer will usually be stored inside the program's data or bss segment and the memory for which the pointer points to will be stored inside the heap. The rest of what you said seems correct to me.
    Thanks for the correction, we never really did cover global/static variables too well in computer architecture.
    If any part of my post is incorrect, please correct me.

    This post is not guarantied to be correct, and is not to be taken as a matter of fact, but of opinion or a guess, unless otherwise noted.

Popular pages Recent additions subscribe to a feed

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