Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Did the author make a mistake in explaining stack and heap in C++ or am I misreading something?

Tags:

c++

c++11

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:

Another point to note is that typically new uses a different block of memory than do the ordinary variable definitions that we have been using. Both the variable nights and pd have their values stored in a memory region called the stack, whereas the memory allocated by the new is in a region called the heap or free store.


Initial Question:

Now my concern is this: the variable pd was create by the keyword new, so it should be stored in the region called heap just like the variable pt, since they were both created by the keyword new.

Am I missing something here? Thank you very much in advance for your inputs.

Revised Question/Follow-up based on the hold:

This question was put on hold by 5 people because they couldn't understand what I was asking. I believe that my question has already been answered but for those who are still not sure about what I was initially asking please read along:

I was unclear about the author's explanation about where the variables and their values were stored in memory. Up to the author explanation, I had a belief that any memory created dynamically (or should I say during runtime after compiling) by using the keyword new gets stored in the heap not the stack.

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

Please try to use the code above as the reference and in particular the **variables (nights, pd, and pt) in your answer so that I can understand it from that code's perspective.

like image 488
rnd809 Avatar asked Jun 08 '17 07:06

rnd809


People also ask

Are pointers stored in stack or heap in C?

Pointer is allocated on the stack and the object it is pointing to is allocated on the heap.

What variables are stored in stack and heap memory?

stack : stores local variables. heap : dynamic memory for programmer to allocate. data : stores global variables, separated into initialized and uninitialized. text : stores the code being executed.

What is stored in heap and stack in C?

Stack and a Heap ? Stack is used for static memory allocation and Heap for dynamic memory allocation, both stored in the computer's RAM . Variables allocated on the stack are stored directly to the memory and access to this memory is very fast, and it's allocation is dealt with when the program is compiled.

Why stack memory is used in C?

Stack memory allocation is considered safer as compared to heap memory allocation because the data stored can only be access by owner thread. Memory allocation and de-allocation is faster as compared to Heap-memory allocation. Stack-memory has less storage space as compared to Heap-memory.


1 Answers

The pointer variables pt and pd are stored on the stack. The values they point at, allocated with new, are stored on the heap.

If I write a sign with an arrow labelled "lake", it doesn't mean that the sign itself is a lake, nor that it must be mounted in a lake. Rather, it should be mounted on solid ground, pointing in the direction of the lake.

like image 177
Lundin Avatar answered Oct 05 '22 06:10

Lundin