When an object on the heap goes out of scope

Consider the following program:

int main() { while(...) { int* foobar = new int; } return 0; } 

When does foobar go out of scope ?

I know that when using new attributes are allocated on the heap and must be deleted manually with delete , in the code above, this causes a memory leak. However, with regard to the area?

I thought this would disappear from the scope as soon as the while loop ends, because you do not have direct access to it. For example, you cannot delete after the loop ends.

+6
source share
8 answers

Be careful, foobar is local to the while loop, but the distribution on the heap has no scope and will only be destroyed if you call delete on it.

Variable and distribution are in no way associated with the compiler. In fact, distribution occurs at run time, so the compiler does not even see it.

+13
source

foobar is a local variable that goes out of scope at the end of a block.

*foobar - dynamically distributed object with a manual lifetime. Since he does not have the time covered by life, the question does not make sense - he has no opportunity from which he can go. Its service life is manually controlled, and the object lives until you become its delete .

Your question is dangerously burdened by prejudice and prejudice. It is best to approach C ++ with a clean mind and an open attitude. Only in this way will you be able to fully appreciate the language miracles.


Here's a clean and open approach: Think about 1) storage classes (automatic, static, dynamic), 2) the lifetime of the object (beveled, permanent, manual), 3) the semantics object (value (copies) versus reference (aliases)), 4 ) RAII and single-user responsibility classes. Clear your mind a) stack / heap , b) pointers , c) new / delete , d) destructors / copy constructors / assignment operators .

+9
source

This is a pretty amazing memory leak. You have a variable on the stack pointing to the memory allocated on the heap. You need to delete the memory on the heap before losing the reference to it when the while loop loop is complete. Alternatively, if you don't want to fuss about memory management, always use smart pointers to own raw memory on the heap and let it clear itself.

 #include <memory> int main() { while(...) { std::unique_ptr<int> foobar = new int; } // smart pointer foobar deletes the allocated int each iteration return 0; } 
+3
source

The pointer (foobar) leaves the scope as soon as the program enters the closing bracket of the while loop. Therefore, if the expression in ... remains true, the memory will leak out every time the loop is executed, when you lost the handle of the selected object with this closing bracket.

+2
source

Here foobar is an int pointer occupying memory on the stack. An int instance created dynamically with new goes into the heap. When foobar goes out of scope, you lose the link to it, so you cannot delete the memory allocated on the heap.

The best solution would be:

 while(--) { int foobar; }//it goes out of scope here. deleted from stack automatically!! 

If you still want to use dynamic allocation, do the following:

 while(--) { int* foobar=new int; //do your work here! delete foobar; //This deletes the heap memory allocated! foobar=NULL; //avoid dangling pointer! :) } 
+1
source

Since foobar is declared in the body of the loop, it goes out of scope at the end of each iteration of the loop. Then it is updated, and new memory is allocated again and again until the cycle ends. The actual object that foobar points to is never out of scope. The scope does not apply to dynamically distributed (aka heap) objects, only to automatic (stack) objects.

0
source

foobar goes out of bounds after each cycle of the loop.

The memory you allocate and assign foobar leaks out because it is still allocated on the heap, but references to it are not available in the program.

0
source

A foobar pointer is created on the stack, but a new int is created on the heap. In the case of a while loop, every time the code loops, foobar drops out of scope. The created int is stored on the heap. At each iteration, a new int is created, and the pointer is reset, which means that the pointer can no longer access any of the previous int (s) on the heap.

There seems to be no shortage in each of the previous answers, and even in this case the heap falls out of scope. Maybe I'm using terminology incorrectly, but I know that at some point the heap is reset too. This can happen after the program no longer works or when the computer is turned off, but I know that this is happening.

Let's look at this issue from a different perspective. I wrote any number of programs that leak memory. Over the years, I owned my computer, I'm sure I missed more than 2 gigabytes of memory. My computer has only 1 gigabyte of memory. Therefore, if the heap NEVER falls out of scope, then my computer has magical memory. Do any of you want to explain exactly when the heap falls out of scope?

-2
source

Source: https://habr.com/ru/post/903935/


All Articles