Is the author wrong in explaining the stack and heap in C ++, or am I misunderstanding something?

Here is the code:

int main() { using namespace std; int nights = 1001; int * pt = new int; // allocate space for an int *pt = 1001; // store a value there cout << "nights value = "; cout << nights << ": location " << &nights << endl; cout << "int "; cout << "value = " << *pt << ": location = " << pt << endl; double * pd = new double; // allocate space for a double *pd = 10000001.0; // store a double there cout << "double "; cout << "value = " << *pd << ": location = " << pd << endl; cout << "location of pointer pd: " << &pd << endl; cout << "size of pt = " << sizeof(pt); cout << ": size of *pt = " << sizeof(*pt) << endl; cout << "size of pd = " << sizeof pd; cout << ": size of *pd = " << sizeof(*pd) << endl; return 0; } 

Now here is the author’s note about the code:

One more note: usually the new one uses a different block of memory than the usual variable definitions that we used. Both the night and pd variables have their values ​​stored in a region of memory called a stack , while memory allocated by a new one is in an area called heap or free storage .




Starting question:

Now I'm worried about this: the pd variable was created using the new keyword, so it should be stored in an area called heap as the pt variable, since they were created using the new keyword.

Am I missing something? Thank you in advance for your submissions.

Revised issue / subsequent observation based on the hold:

This question was suspended by 5 people because they could not understand what I asked. I believe that my question has already been answered, but for those who are still not sure what I originally asked, please read:

I was not aware of the author’s explanations about where the variables and their values ​​were stored in memory. Prior to explaining the author, I figured that any memory created dynamically (or should I say, at runtime after compilation) using the new keyword is stored on the heap , not the stack .

So, this confused me when he wrote that the variable pd has a value stored on the stack , but again, how is it possible if the variable was created during "runtime" with the keyword new , so it should be on the heap , not the stack .

Please try using the above code as a reference and in particular ** variables (nights, pd and pt ) in your answer so that I can understand this from the point of view of the code.

Thank you in advance!

+54
c ++ c ++ 11
Jun 08 '17 at 7:58
source share
5 answers

Pointer variables pt and pd are stored on the stack. The values ​​they point to are highlighted in new, stored on the heap.

If I write a sign with an arrow labeled "lake", this does not mean that the sign itself is a lake, or that it should be installed in the lake. Rather, it should be set on solid ground, pointing in the direction of the lake.

+95
Jun 08 '17 at 8:17 on
source share

The only mistakes the author made are

  • Do not call delete after using pointers.

  • The reuse of endl is in doubt. Use \n instead.

  • using namespace std; Although often used in textbooks to achieve brevity, it is not recommended in production code, especially in headings.

  • Using int * pt = new int; *pt = 1001; int * pt = new int; *pt = 1001; instead of int* pt = new int(1001); is doubtful, since the author has it, *pt is in an overly uninitialized state between two operators. This makes your code vulnerable to instabilities.

  • (minor). You should always think about the possibility that new can throw a std::bad_alloc exception if the selection fails.

I would not worry that the terms stack and heap are too many; they represent the concept of language implementation is not part of the language itself. Prefer the terms of automatic and dynamic storage. What does the C ++ standard use, so why come up with the whole load of alternative terminology?

In fact, I would burn the book and get a copy of Straustrap.

+18
Jun 08 '17 at 8:02
source share

Images will help.

 Automatic storage (stack) Dynamic storage (heap) ------------------------- ---------------------- Item Address Value Address Value ---- ------- ----- ------- ----- nights 0xff001000 1001 pt 0xff001004 0x00b0fff0 ------> 0x00b0fff0 1001 pd 0xff00100c 0x00b0fff4 ------> 0x00b0fff4 10000001.0 

nights , pt and pd objects have auto storage duration. In most implementations, this means that they are allocated from the runtime stack. The nights object lives at 0xff001000 1 and stores the value 1001 . The pt object lives at 0xff0010004 and stores the address of the dynamic object created by new , which is 0x00b0fff0 . The pd object lives at 0xff00100c and stores the address of another dynamic object created by new , which is 0x00b0fff4 .

Heap objects at addresses 0x00b0fff0 and 0x00b0fff4 store the values 1001 and 10000001.0 respectively.

Edit

FWIW, I wrote a dumper utility some time ago that uploads the addresses and contents of objects; with code

 #include <cstdio> #include "dumper.h" using namespace std; int main( void ) { int nights = 1001; int *pt = new int; *pt = 1001; double *pd = new double; *pd = 1000001.0; char *names[] = { "nights", "pt", "pd", "*pt", "*pd" }; void *addrs[] = { &nights, &pt, &pd, pt, pd }; size_t sizes[] = { sizeof nights, sizeof pt, sizeof pd, sizeof *pt, sizeof *pd }; dumper( names, addrs, sizes, 5, stdout ); return 0; } 

I get a conclusion

  Item Address 00 01 02 03 ---- ------- -- -- -- -- nights 0x7fff9efe7c6c e9 03 00 00 .... pt 0x7fff9efe7c60 10 20 50 00 ..P. 0x7fff9efe7c64 00 00 00 00 .... pd 0x7fff9efe7c58 30 20 50 00 0.P. 0x7fff9efe7c5c 00 00 00 00 .... *pt 0x502010 e9 03 00 00 .... *pd 0x502030 00 00 00 00 .... 0x502034 82 84 2e 41 ...A 

In this case, the addresses are valid. On my system (x86_64 / Linux SLES-10), stack addresses start high and grow “down” (towards lower addresses), and heap addresses start from a low level and grow “up” (towards higher addresses).

x86 is unimportant, that is, the address byte is the least significant byte; multibyte objects should be read from right to left.




  • All addresses are composed of air and are not intended to represent a real implementation or real world architecture.

+16
Jun 08 '17 at 16:01
source share

Now, I am concerned that the pd variable was created using the new keyword, so it should be stored in an area called the heap, just like the pt variable, since they were created using the new keyword.

No, it is not. The code:

  double * pd = new double; // allocate space for a double 

This is no different from:

 double * pd; pd = new double; 

Thus, pd itself is not created by the new operator, but only its value. But he talks about pd , not its meaning.

+15
Jun 08 '17 at 8:02
source share

The variable pd is stored in the stack memory, however, the memory location indicated by pd is allocated in the heap memory. I think that the author wanted to emphasize precisely this difference between these concepts.

+6
Jun 08 '17 at 8:54 on
source share



All Articles