Why don’t you blow the pile?

Asked

Viewed 714 times

3

With this code, the book tells me it doesn’t blow the stack, but why doesn’t it just burst?

#include <stdio.h>

int main(){
   int *p;
   while(1){
      p = new int;
   }
}
  • Why would it pop? There’s only one variable in the stack.

  • but the loop while Fz progresses stays in an eternal loop ?

  • When you use new, you are using dynamic allocation itself. Normally the stack gets static things, not dynamic. You’d have a pile of pop if you did int main() { main(); }, then I’d be stacking function call main about main

2 answers

6

The variable p was declared in this function. It will have a size of 4 or 8 bytes depending on the architecture. This variable is unique in the whole function and it seems that every application is this code. Why would the stack burst with only one variable of so little size?

The stack has fixed size, there is no reason to burst. The burst only occurs when it is occupied more and more, which does not occur. Obviously if no one stops this application it will never end.

At some point the application will be very slow or you will have a problem with the heap because virtual memory has limit and there is RAM limit.

If the architecture is 32 bits when it runs just over 1 billion times, it will occupy the 4GBB of the total memory. As not everything should be in RAM should have some slowness to play in secondary storage. If you have a state-of-the-art SSD you won’t even feel this much.

If the architecture is 64-bit it will need more than 4 quintillions of interactions to fill all the space, of course it will need a secondary storage prepared to support such virtual memory, it 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 the new no more allocation in heap, so it’s all done in the pile, always in the same place, since there’s always only one variable, so there’s no heap can burst. The stack, in essence, leaving aside small details, will occupy 4 bytes (may be different, but in practice will be 4 bytes).

The operator new is responsible for allocating heap and it is running several times allocating an integer at each time, which is not only 4 bytes, but this is another subject. It always returns a pointer that is stored in p superimposing the previous value, that is, the object created earlier is without any pointer pointing to it, is inaccessible. As it is not given a delete in the object, it is there occupying space without need.

In a managed memory language this would not occur and the garbage collector would release memory.

See more about the stack and the heap.

  • Let me get this straight, the pile isn’t filling, but is the heap? As the heap fills up, do you start using HD/SSD storage instead of RAM? If yes, there would be "heap overflow" at some point (by the HD/SSD getting full or by some "preset limit" popping)?

  • pq a heap is filling if it’s just a pointer ?

  • Just fill what was reserved by the operating system for virtual memory and there will be memory overflow. The pointer only exists one at a time and stays in the stack as it is in the response. The heap is filling because every loop iteration is having a new integer allocated in the heap, with the operator new. And you’re never telling him to move, as you should at some point that he’s no longer needed. Each allocation generates a pointer, only it is being played over the previous pointer since there is only one variable.

  • Even all these allocations cannot be accessed by the code since the pointer to each of them is lost soon after another is created.

  • so at each loop p is getting more 4 bits of memory

  • but the new operator is called several times by q understand it allocates more memory .

  • No, it’s allocating 4 bytes (roughly) into heap. This is because the new is being called each time. The p receives 4 or 8 bytes of the pointer where there was the allocation, but it is overwriting because there is only one variable. Now I’m going to close here because I’m just repeating what has been said before.

  • ok obg guess q I’m confusing stack and heap,

  • https://answall.com/q/3797/101

  • THANK YOU agr yes it was clear ,I was confusing heap with stack

  • Blz, it’s a shame you preferred the answer that has several errors. For she is negative while mine has several votes, but if you wish to learn wrong, only you will lose.

Show 6 more comments

-2


In a simplified way we can consider that there are two large areas for memory allocation and manipulation:

Stack(stack)

Heap(monte).

inserir a descrição da imagem aqui

The stack is a much smaller area and the heap or stack is a much larger memory area. The stack has in windows a standard size of only 1MB (one megabyte)!

However, this region of stack memory is far more effective at locating and manipulating resources compared to heap.

When we declare local variables they will be allocated in the Stack

Pointers also stay in the stack and function calls are stacked in the stack

So if you run the code below will occur the stack overflow error, because there will be attempt to allocate 3 million and 300 thousand integers of the vector variable that is local of the main function().

#include <stdio.h>

int main()
{
    int vetor[3300000]; 
 //TENTANDO ALOCAR 3 MILHOES E 300 MIL INTEIROS
    printf("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 EQUATES TO 12.58MB (twelve fifty-eight-megabyte comma) Much larger than 1MB of stack space(stack) SOON, IT WILL BURST!

See what happens when you run this code in Visual Studio Community 2015:

inserir a descrição da imagem aqui In your code what happens?

#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 Heap which 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 occurring is only the change of the address contained within it. Of course, 4 new KB are being allocated to Heap each loop, but that won’t be enough to fill the entire heap in a short time. If this occurs there would be a memory invasion attempt error in addition to the one initially allocated to the program*(See this subject of base register and limit in the discipline Operating Systems)*

Obs: Typical C-language programs use the malloc and free functions that allocate and desalt into heap memory. In C++ there is new and delete that also allocates and displaces objects in heap memory, but with useful memory management debug enhancements to detect possible memory leaks. (see also smart pointers Std::unique_ptr and Std::shared_ptr in c++?)

  • 1

    obg dude this was the best explanation, I’m seizing c++ with a book ,I come from java never had q deal with managing memory till agr ,but this explanation took me a lot of doubt (OUR WINDOWS HAS ONLY 1 MEGA SPACE ,SO Q POINTERS ARE SO USEFUL )

  • Thanks Dougllas, See that I edited now put the correct size that an integer occupies in windows that is 4 or 8 Bytes! See the new accounts! Hug

Browser other questions tagged

You are not signed in. Login or sign up in order to post.