Вопрос

If I've already used new to allocate memory to int* p, but then if I want use new again, will the previously allocated memory remain valid?

int *p;
p= new int[5]; //is this going to remain valid after the following statement?
p= new int[10];

If yes, then am I required to delete[] it? I tried doing that but the compiler says that the pointer is valid, so I can't delete it. Is there any other way to get rid of the previously allocated memory?

Это было полезно?

Решение

The second use of new[] is, in itself, completely independent of the first. The second use of new[] does not in any way invalidate the first block of memory that you allocated.

When you allocate memory, you are asking the system to allocate memory, and tell you the address of the memory that it, the system, allocated. It is your responsibility to remember that address so that you can refer to it in the future. You need to be able to refer to the memory in order to access it and eventually deallocate it.

This is your code:

int *p;
p= new int[5];
p= new int[10];

The first assignment records the address of the first allocated block. The second assignment overwrites that value with the address of the second block.

So, the problem you have is that you no longer have a variable that contains the address of the first block of memory. Whilst that first block is still valid, you are unable to refer to it. In particular, since you can no longer refer to it, you cannot deallocate it and so it will be leaked.

If you wish to be able to deallocate both blocks of memory, then you have to be able to refer to both blocks. Which means that you must have two variables to hold the two distinct addresses.

int* p1 = new int[5]; 
int* p2 = new int[10];

Now you can refer to blocks blocks separately. When you are done, deallocate the memory:

delete[] p1;
delete[] p2;

On the other hand, if you want to delete the memory before allocating a new block, you can do that:

int* p = new int[5]; 
// use p
detete[] p;

int* p = new int[10];
// use p
delete[] p;

Другие советы

The prev remain somewhere on the heap. You can use valgrind to verify that. If you want to use the sane pointer again and again you might consider to create a function that delete the last allocation and allicate new space each time...

To add to what David Heffernan said, when you have this statement:

int *p;
p= new int[5];

don't think of p as being assigned to the memory that was allocated. The memory exists independently of p, but you're using p as a sort of post-it note to jot down where that memory is located. When you assign a new value to p, you're simply overwriting your note. You haven't done anything to the memory that was allocated-- you just don't know where it is anymore.

If the memory you previously allocated is not needed anymore, you have to use delete [] to free the memory. Otherwise, the memory is still out there, but you have no way to access it any more, thus a memory leak.

You will need to delete[] p; in order to avoid memory leak. The memory doesn't get released by itself. What's the exact information the compiler is giving in terms of "can't delete it"?

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top