Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using "new" multiple times on the same pointer

Tags:

c++

What happens when I do something like

   int * ptr = new int; 
   *ptr = 5;
   // ... do some stuff here
   ptr = new int; 
   // ... reuse ptr to do some other stuff

as opposed to

   int * ptr1 = new int; 
   *ptr1 = 5;
   // ... do some stuff here
   delete ptr1;
   int * ptr2 = new int; 
   // ... use ptr2 now

????

Does the same thing happen at the hardware level? In other words, in the first case, does ptr = new int; move on from its previous pointer/value pair, and what happens to those old values? Do they get replaced, do they just float around somewhere, etc.?

like image 577
Hillary Rodham Clinton Avatar asked Apr 17 '15 21:04

Hillary Rodham Clinton


People also ask

What happens if you free a pointer twice?

Double free errors occur when free() is called more than once with the same memory address as an argument. Calling free() twice on the same value can lead to memory leak.

What happens if you malloc a pointer twice?

It merely allocates new space and returns a pointer to it. Then that new pointer is assigned to newPtr , which erases the old value that was in newPtr .

What happens if you free a pointer twice in C?

If we free the same pointer two or more time, then the behavior is undefined. So, if we free the same pointer which is freed already, the program will stop its execution.

What happens if I free a pointer?

The free function causes the space pointed to by ptr to be deallocated, that is, made available for further allocation. which means that a later call to malloc (or something else) might re-use the same memory space. As soon as a pointer is passed to free() , the object it pointed to reaches the end of its lifetime.


2 Answers

Your int *ptr is just a variable that stores an address, nothing more.

After your first int * ptr = new int;, it contains the address to a dynamically allocated integer. After your second int * ptr = new int;, it contains the address to another dynamically allocated integer.

What happens then is nothing special, the only thing is that you didn't call delete so the memory allocated for the first integer will never be freed. There's nothing to keep track of it, its address isn't stored anywhere, and so it will keep being useless allocated space until the program ends.

like image 155
coyotte508 Avatar answered Oct 13 '22 11:10

coyotte508


In the first example, the pointer is overwritten, but the object it pointed to still exists and is "floating around" somewhere. This causes memory leaking.

If this happens in a frequently used function or in a loop, you could easily exhaust your memory, storing values that you can't nor won't access any more.

Leaking is in fact a very common error. A good practice is to avoid it by using smart pointers such as shared_ptr. These keep track of a usage count, and free the object automatically if it's no longer used. For example:

 shared_ptr<int> ptr = make_shared<int>();   // allocate an int
 *ptr = 5;
 // ... do some stuff here
 ptr = make_shared<int>();  // the old object is no longer used so deleted automatically
 // ... reuse ptr to do some other stuff    
like image 25
Christophe Avatar answered Oct 13 '22 11:10

Christophe