RazaUsman_k
RazaUsman_k

Reputation: 704

changing pointer of a dynamically allocated array

When we declare something dynamically like this

int *ptr = new int [100];

and then change the pointer address (i.e point it to something else)

int pointer[5] = {1,2,1,3,1,};
ptr = pointer ; 

now what happens to that memory which contained 100 integers?

is there anyway to regain control of that memory ? and if not, does it lead to a memory leak?

Upvotes: 1

Views: 3474

Answers (3)

juanchopanza
juanchopanza

Reputation: 227608

now what happens to that memory which contained 100 integers?

It is "leaked" because you lost the only handle you had to it.

is there anyway to regain control of that memory ?

No, unless you take care to create another handle before re-assigning ptr.

int* ptr = new int[100];
int* ptr2 = ptr;
...
ptr = pointer;
delete [] ptr2;

In real code you would not rely on raw pointers to allocated memory, but rather use whichever RAII type works best for your problem (e.g. std::vector, std::unique_ptr etc.)

Upvotes: 1

kuroi neko
kuroi neko

Reputation: 8671

Any block of memory allocated with new is unavailable until it is released, either by a corresponding delete or when the process in which the memory was allocated terminates.

The memory allocator actually has no clue about what references you maintain on allocated objects.It just allows you to release the corresponding memory blocks if you manage to provide valid addresses.
How you keep track of these addresses is entirely up to you.

So if for some reason you loose track of a chunk of memory (by overwriting a data pointer as in your example, or simply forgetting to use it at some point to release the memory), you will deprive your system of this bit of memory until the underlying process terminates.

This becomes a memory leak if the "memory lapse" is unintentional, even more so if this occurs repeatedly.

For instance, if you need some data that has the same life span as the process, you can do a one-time allocation and never release the memory (since it will be released automatically upon termination). That could horrify the good practices zealots, but would nevertheless not qualify as a memory leak.

On the other hand, there are other ways of messing up you memory allocator : passing an invalid pointer to delete (including a reference to an already released object, i.e. deleting the same instance of an object more than once, or a pointer to something that was not allocated by new, like a local or global variable) will bring you the wonderful realm of undefined behaviours, which could cause random memory losses, data corruption or nuclear meltdowns.

Upvotes: 1

Peter
Peter

Reputation: 36637

In the sample you give, ptr is the only pointer that references the hundred int created by new int [100] (i.e. points at the first element).

Changing the value of ptr does not affect the hundred int. It means that ptr no longer contains the address of the first element.

Practically, after the assignment ptr = pointer, the allocated memory is said to be leaked - it has been allocated, there is no way in standard C++ to access it, and no way in standard C++ to release it (e.g. make the memory it consumes available for reallocation in another new expression).

The only way to keep control of the memory in your case is to store the value of ptr in another variable (or a member of a structure, or an element of an array of pointers, ....) BEFORE reassigning it. If the value of ptr is stored before reassigning ptr, and the place it is stored still exists, the value can be retrieved.

Practically, it would be better to use a standard container (e.g. a std::vector<int>) and not use a new expression in your code. That way, the contents of the container are accessible as long as the container exists (and cease to exist, as far as your program is concerned, when the container does).

Upvotes: 1

Related Questions