Where are pointers allocated in memory?

This is a discussion on Where are pointers allocated in memory? within the C++ Programming forums, part of the General Programming Boards category; Objects created by "new" and "malloc" are allocated in the "heap" while others are allocated in the "stack". But where ...

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    7

    Lightbulb Where are pointers allocated in memory?

    Objects created by "new" and "malloc" are allocated in the "heap" while others are allocated in the "stack". But where is the pointers returned by such functions are allocated in the memory. Pointers are also a type of variable as far as I know, so they need to be placed somewhere in the memory like other variable, but where? Is it "stack"?

    Another question that confuses me is that when you create a object using "new" in a function, the object is allocated some space in the "heap" area, but why all the books and online tutorials are saying all the local variables are allocated in the "stack" area? So what's really been allocated some space in the "stack" ?

    Thanks in advance!

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,793
    The pointer variables themselves are allocated on the stack, the memory areas they point to can either be the stack or the heap.

    Code:
    int foo;
    int * bar;      // Memory for bar allocated on the stack
    bar = &foo;     // Point bar to the stack
    bar = new int;  // Point bar to the heap
    "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
    Dump Truck Internet valis's Avatar
    Join Date
    Jul 2005
    Posts
    357
    the stack starts at the bottom and grows up, the heap starts at the top and grows down until the two meet.
    The stack is good for small chunks of data because you have instructions to easily work with it and a stack pointer to store your current position in the stack (esp), the heap is made for storing large chunks of data. The stack is faster as well. The stack is used for more temporary data, when your code compiles, local stack variables are simply your current position in the stack with an offset to the position your variable was created at, to destroy it, you simply add (it grows up) to your stack pointer to point it behind your local variable.
    A pointer is created on the stack because (on x86) it's simply a double word, but the data it points to is usually a hunk of memory, so it's allocated on the heap.

  4. #4
    Registered User
    Join Date
    Jul 2005
    Posts
    7
    Thanks for reply guys.

    But Valis said:
    to destroy it, you simply add (it grows up) to your stack pointer to point it behind your local variable.
    do you mean you have to explicitily destroy objects in the "stack"? But what I thought is that only objects in the "heap" needs to be "free"ed (in C) or "delete"ed (in C++) explicitily when you no longer need them. So which one is right?

  5. #5
    Dump Truck Internet valis's Avatar
    Join Date
    Jul 2005
    Posts
    357
    yeah, C handles all the stack stuff for you, you don't need to explicitly destroy stack variables.

  6. #6
    Registered User
    Join Date
    Jul 2005
    Posts
    7
    Quote Originally Posted by valis
    yeah, C handles all the stack stuff for you, you don't need to explicitly destroy stack variables.
    Thanks Valis, that's clear now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory allocated
    By saswatdash83 in forum C Programming
    Replies: 2
    Last Post: 07-15-2008, 02:28 AM
  2. Memory allocation/reallocation
    By magda3227 in forum C Programming
    Replies: 10
    Last Post: 07-04-2008, 03:27 PM
  3. Will free() clean up this allocated memory?
    By simguy in forum C Programming
    Replies: 3
    Last Post: 01-18-2007, 04:21 PM
  4. Daemon programming: allocated memory vs. fork()
    By twisgabak in forum Linux Programming
    Replies: 2
    Last Post: 09-25-2003, 02:53 PM
  5. clearing memory allocated to a struct
    By jdinger in forum C++ Programming
    Replies: 3
    Last Post: 04-25-2002, 11:21 PM

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