the scope of a pointer?

12,149

Solution 1

The variable itself is stored on the stack or DATA segment, but the memory it points to after being allocated with new is within the heap.

Solution 2

void main()
{
  int* p;          // p is stored on stack
  p = new int[20]; // 20 ints are stored on heap
}
// p no longer exists, but the 20 ints DO EXSIST!

Hope that helps.

Solution 3

void func()
{
  int x = 1;
  int *p = &x;
}
// p goes out of scope, so does the memory it points to

void func()
{
  int *p = new int;
}
// p goes out of scope, the memory it points to DOES NOT

void func()
{
  int x = 1;
  int **pp = new int*;
  *pp = &x;
}
// pp goes out of scope, the pointer it points to does not, the memory it points to does

And so forth. A pointer is a variable that contains a memory location. Like all variables, it can be on the heap or the stack, depending on how it's declared. It's value -- the memory location -- can also exist on the heap or the stack.

Typically, if you statically allocate something, it's on the stack. If you dynamically allocate something (using either new or malloc) then it's on the heap. Generally speaking you can only access dynamically allocated memory using a pointer. This is probably where the confusion arises.

Solution 4

It is necessary to distinguish between the pointer (a variable that holds a memory location) and the object to which the pointer points (the object at the memory address held by the pointer). A pointer can point to objects on the stack or on the heap. If you use new to allocate the object, it will be on the heap. The pointer can, likewise, live on the heap. If you declare it in the body of a function, then it will be a local variable and live in local storage (i.e. on the stack), whereas if it is a global variable, it will live somewhere in your application's data section. You can also have pointers to pointers, and similarly one can allocate a pointer on the heap (and have a pointer-to-a-pointer pointing to that), etc. Note that while I have referenced the heap and stack, the C++ only mentions local/automatic storage and dynamic storage... it does not speak to the implementation. In practice, though, local=stack and dynamic=heap.

Solution 5

A pointer is a variable containing the address of some other object in memory. The pointer variable can be allocated:

  • on the stack (as a local auto variable in a function or statement block)
  • statically (as a global variable or static class member)
  • on the heap (as a new object or as a class object member)

The object that the pointer points to (references) can likewise be allocated in these three places as well. Generally speaking, though, a pointed-to object is allocated using the new operator.

Local variables go out of scope when the program flow leaves the block (or function) that they are declared within, i.e., their presence on the stack disappears. Similarly, member variables of an object disappear when their parent object goes out of scope or is deleted from the heap.

If a pointer goes out of scope or its parent object is deleted, the object that the pointer references still exists in memory. Thus the rule of thumb that any code that allocates (news) an object owns the object and should also delete that object when it's no longer needed.

Auto-pointers take some of the drudgery out of the management of the pointed-to object. An object that has been allocated through an auto_ptr is deleted when that pointer goes out of scope. The object can be assigned from its owning auto_ptr to another auto_ptr, which transfers object ownership to the second pointer.

References are essentially pointers in disguise, but that's a topic for another discussion.

Share:
12,149
numerical25
Author by

numerical25

Updated on June 04, 2022

Comments

  • numerical25
    numerical25 almost 2 years

    Ok, so I did find some questions that were almost similar but they actually confused me even more about pointers.

    C++ Pointer Objects vs. Non Pointer Objects

    In the link above, they say that if you declare a pointer it is actually saved on the heap and not on the stack, regardless of where it was declared at. Is this true ?? Or am I misunderstanding ??? I thought that regardless of a pointer or non pointer, if its a global variable, it lives as long as the application. If its a local variable or declared within a loop or function, its life is only as long as the code within it.