Where are C ++ pointers stored on the stack or on the heap?

I'm trying to understand the difference between stack memory and heap , and this question on SO as well as this explanation did a pretty good job explaining the basics.

In the second explanation, however, I came across an example to which I have a specific question, an example is this:

heap allocation example

It is explained that the object m stands out on the heap , I just wonder if this is the full story. In my opinion, the object itself really stands out on the heap because the new keyword was used to create it.

However, isn't it that the pointer to the object m at the same time allocated on the stack ? Otherwise, as it were, the object itself, which, of course, sits on the heap . I feel that for the sake of completeness, this should have been mentioned in this lesson, leaving it out, which causes me some confusion, so I hope someone can clarify this and tell me that I'm right, understanding that this example should have basically two statements that would have to say:

1. The pointer to the object m was allocated on the stack

2. the object m (therefore, the data that it carries, as well as access to its methods) was allocated on the heap

+9
c ++ memory-management pointers
Jun 24 '14 at 7:37
source share
4 answers

Your understanding may be correct, but the statements are incorrect:

A pointer to the object m allocated on the stack.

m is a pointer. It is on the stack. Perhaps you meant a pointer to a Member object.

The object m itself (the data that it carries, as well as access to its methods) was allocated on the heap.

It would be correct to say that the object pointed to by m is created on the heap

In general, local objects and function / method parameters are created on the stack. Since m is a local function object, it is on the stack, but the object that m points to is on the heap.

+9
Jun 24 '14 at 7:44
source share

"stack" and "heap" are common software jargon. In particular, storage is not required to be managed internally through the stack or heap data structure.

C ++ has the following storage classes

  • static
  • auto
  • dynamic
  • flow

Roughly speaking, dynamic corresponds to a heap, and automatically corresponds to a stack.

Turning to your question: a pointer can be created in any of these four storage classes; and the objects pointed to can also be in any of these storage classes. Some examples:

 void func() { int *p = new int; // automatic pointer to dynamic object int q; // automatic object int *r = &q; // automatic pointer to automatic object static int *s = p; // static pointer to dynamic object static int *s = r; // static pointer to automatic object (bad idea) thread_local int **t = &s; // thread pointer to static object } 

Named variables declared without a qualifier are automatic if inside the function or static otherwise.

+12
Jun 24 '14 at 8:19
source share

When you declare a variable in a function, it always goes on the stack. Thus, your Member* m variable is created on the stack. Note that m itself is just a pointer; he does not indicate anything. You can use it to point an object to a stack or to a heap, or to nothing.

A variable declaration in a class or structure is different — those where an instance of a class or structure is ever created.

To create something on the heap, you use new or std::malloc (or their variants). In your example, you create an object on the heap using new and assigning your address m . To avoid memory leaks, you need to free objects on the heap. If highlighted with new , you need to use delete ; if highlighted using std::malloc , you need to use std::free . The best approach, as a rule, is to use a smart pointer, which is an object that contains a pointer and has a destructor that frees it.

+4
Jun 24 '14 at 7:46
source share

Yes, the pointer is allocated on the stack, but the object that the pointer points to is allocated on the heap. You're right.

However, isn't it that the pointer to the object m is simultaneously allocated on the stack?

I assume you were referring to the Member object. The pointer is allocated on the stack and held there throughout the function (or its area). After that, the code can work:

 #include <iostream> using namespace std; struct Object { int somedata; }; Object** globalPtrToPtr; // This is into another area called // "data segment", could be heap or stack void function() { Object* pointerOnTheStack = new Object; globalPtrToPtr = &pointerOnTheStack; cout << "*globalPtrToPtr = " << *globalPtrToPtr << endl; } // pointerOnTheStack is NO LONGER valid after the function exits int main() { // This can give an access violation, // a different value after the pointer destruction // or even the same value as before, randomly - Undefined Behavior cout << "*globalPtrToPtr = " << *globalPtrToPtr << endl; return 0; } 

http://ideone.com/BwUVgm

The code above stores the address of the pointer on the stack (and a memory leak, too, because it does not free Object allocated memory with delete ).

Since after exiting the function, the pointer is ā€œdestroyedā€ (that is, its memory can be used for any program you like), you can no longer safely access it .

The above program can either: work properly, crash, or give you another result. Accessing freed or freed memory is called undefined behavior .

+2
Jun 24. '14 at 7:39
source share



All Articles