With this code, the book tells me that this does not pop the stack, but why exactly does it not bang?
#include <stdio.h>
int main(){
int *p;
while(1){
p = new int;
}
}
With this code, the book tells me that this does not pop the stack, but why exactly does it not bang?
#include <stdio.h>
int main(){
int *p;
while(1){
p = new int;
}
}
In a simplified way we can consider that there are two major areas for memory allocation and manipulation:
Stack
Heap.
Thestackisamuchsmallerareaandtheheaporheapisamuchlargermemoryarea.Thestackhasastandardsizeof1MB(onemegabyte)inwindows!
However,thisheapmemoryregionisfarmoreeffectiveatlocatingandmanipulatingresourcescomparedtoaheap.
DeclaringlocalvariableswillbeallocatedtoStack
Pointersarealsointhestackandfunctioncallsarestackedinthestack
Soifyourunthecodebelow,therewillbeastackoverflowerror,becausetherewillbeanattempttoallocate3millionand300,000integersofthevectorvariablethatislocaltothemain()function.
#include<stdio.h>intmain(){intvetor[3300000];//TENTANDOALOCAR3MILHOESE300MILINTEIROSprintf("Esta linha não será exibida pois ocorrerá antes STACKOVERFLOW");
return 0;
}
Well, if we consider in windows that an integer occupies 4B (four bytes) then we have the attempt to allocate 3,300,000 x 4B = 13,200,000 Bytes 1MB (one megabyte) equals 1,048,576 Bytes
13.200.000B /1.048.576B EQUIVALE A 12,58MB (twelve point fifty-eight megabytes) Much larger than 1MB of stack space Soon, YOU WILL BURST!
See what happens when you run this code in Visual Studio Community 2015:
#include <stdio.h>
int main(){
int *p; // aloca uma variável ponteiro na stack(pilha)
while(1){ //loop infinito
p = new int; // a variável p contida na pilha recebe o endereço de memória alocado para um inteiro através de new
}
}
Note that new allocates memory in the Heap that is much larger so throughout the program the infinite loop will: Create a new memory region to allocate an integer (possibly 4KB) and the address of this memory region will be assigned in the pointer variable p that is in the stack Then note that the pointer variable p is not being created again! What is happening is just the change of the address contained within it. Of course, 4 new KBs are being allocated in the Heap with each loop, but this will not be enough to occupy the entire heap in a short time. If this occurs there would be an attempt to invade memory beyond the one initially allocated to the program * (See this subject of base and limit register in the Discipline Operating Systems) *
Note: Typical C language programs use the malloc and free functions that allocate and deallocate in heap memory. In C ++ there is new and delete that also allocates and deallocates objects in heap memory, but with memory management debug enhancements useful for detecting possible memory leaks. (see also smart pointers std :: unique_ptr and std :: shared_ptr in c ++?)
The variable p
has been declared in this function. It will have the size of 4 or 8 bytes depending on the architecture. This variable is unique throughout the function and it seems every application is this code. Why would I pop the stack with just one variable of such small size?
The stack has a fixed size, it does not have to pop. The overflow only occurs when it is being occupied more and more, which does not occur. Obviously if nobody stops this application it will never end.
At some point the application will be very slow or have a problem with heap because virtual memory has a limit and there is a limit to RAM.
If the architecture is 32 bits when running slightly more than 1 billion times, it will occupy the 4GBB of total memory. Since not everything should be in RAM you should have some slowness to play on secondary storage. If you have a next-generation SSD, you will not be sorry.
If the 64-bit architecture will require more than 4 quintiles of interactions to fill all space, of course you will need a secondary storage prepared to support such virtual memory, you would need hundreds of HDDs with the current capacity. I think it would take a few centuries to run this with current hardware. If you take out new
, there is no more allocation in heap , so everything is done in the heap, always in the same place, since there is always only one variable, heap can pop. The stack will occupy in essence, leaving aside small details, will occupy 4 bytes (may be different, but in practice will be 4 bytes).
The new
operator is responsible for allocating in heap and it is being executed several times by allocating one integer at a time, which nor is it just 4 bytes , but this is another subject. It always returns a pointer that is stored in p
overlapping the previous value, that is, the previously created object has no pointer pointing to it, is inaccessible. Since a delete
is not given in the object, it stays there by taking up space unnecessarily.
In a managed memory language this would not occur and the garbage collector would free up memory.
See more about stack and heap .