Memory leak when assigning a new value to a variable?

Yes, I'm one of those guys who learn C ++ coming from Java, being spoiled by an automatic garbage collector. There is a special situation where I wonder if there is a memory leak or not. Consider in C ++:

bool *test = new bool(false); test = new bool(true); delete test; 

Am I losing memory here? Or should I first call delete before assigning a new value? Like this:

 bool *test = new bool(false); delete test; test = new bool(true); delete test; 

The feeling of my feeling tells me that the first is correct, since the test pointer points to the same address in memory and assigns a new value to its variable, will not change this address. Or does the new operator allocate another address in memory? Can someone give me a key, or have I messed up everything?

+4
source share
7 answers

Yes, you are leaking, and the C ++ way to do this is:

 bool test = false; test = true; // ta-da - no leak. 

You can take a second approach - however, you are likely to draw a lot of frowned ...

+6
source

Yes, it is you who must delete before rewriting the address. Or better yet, select a stack or use a smart pointer.

+5
source

You really are spoiled.

The problem is that you do not distinguish between an address and a variable.


 bool *test = new bool(false); 
  • Allocate space with automatic duration for test
  • Allocate space with dynamic duration for bool
  • Write false to this space
  • Store the address of this space in test

 test = new bool(true); 
  • Allocate space with dynamic duration for bool
  • Write true to this space
  • Store the address of this space in test (the previously saved address is ruthlessly discarded because it is not stored anywhere, the memory it points to can never be recovered, which means that you have a leak).

 delete test; 
  • Read the address stored in test
  • Free up space at this address

The feeling of my feeling tells me that the first is correct, since the pointer points to the same address in memory and assigns a new value to its variable, will not change this address. Or does a new statement allocate another address in memory?

Semantically, you should keep in mind that new always returns the address in the new space in memory (obviously false, since memory is reused). Therefore, each call to new must correspond to one call to delete .

Therefore, your gut feeling is wrong, new does not create a β€œsimple” variable. If you come with Java, it can be really shocking, and you will need a strong guide to understand the intricacies of programming.

+1
source

Yes, you are losing memory. For each new must be the following delete . If you rewrite a pointer with a new address, you lose the ability to join the selection with delete .

0
source

Yes, your first example is actually a memory leak. Each call to new must have a consistent delete call (unless new succeeds).

In C ++, the usual way to do what you are trying to do is simply declare bool locally:

 bool b = truel b = false; 

If for some reason you really need dynamic allocation, there are smart pointers that manage memory, so you don’t have to worry about calling delete . You can look at scoped_ptr , unique_ptr and shared_ptr .

Finally, C ++ has an excellent standard library that handles many possible containers and algorithms, preventing you from reinventing them and eliminating the need to deal with dynamic allocation in a variety of cases.

If you are serious about learning C ++, I would choose one of the books from the list of SO C ++ books and learn from scratch, rather than trying to translate Java idioms into C ++ (this just won't work).

0
source

bool is a built-in data type. Why do you need to allocate such a memory? You can easily push onto the stack.

0
source

Yes, in the first case, you skip memory. The second way is the right way to do it. In your first case ...

When you call a new second time, the first selection is out of scope, but still exists. In Java, this is normal because GC will clean it for you. However, in C ++ there is no such way with raw spointers.

Each new call must also have a call to delete at some point. Also, after deleting the pointer, set the value to null or 0. This will prevent accidental deletion of invalid memory.

0
source

All Articles