Hi,
If I'm not mistaken, the following code will allocate 5 integer elements from memory to 'ptr':

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

If after this code I enter the following:

ptr = new int[7];

will the original 5 elements be overwritten or deallocated? Or will they just stay resident in memory for nothing?

Thanks,
Dean

It will be a dangling pointer, so just be allocated for nothing.
Because you overwrote ptr, you can't call delete[] anymore on the original ptr.

Hi,
If I'm not mistaken, the following code will allocate 5 integer elements from memory to 'ptr':

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

Not quite correct. new will allocate space for five integers, stored contiguously, somewhere in the memory. It will then return the address of the first byte of the memory it allocated. Since you're doing int *prt = new ... , ptr ends up containing this memory address.

If after this code I enter the following:

ptr = new int[7];

will the original 5 elements be overwritten or deallocated? Or will they just stay resident in memory for nothing?

Here, you're allocating another block of space, big enough for seven integers, somewhere in memory (but definitely not on top of the old memory) and then aiming ptr at it. You haven't done anything to the old block of five integers. They're still there, you just have no way of getting at them and neither does anything else on the system. This is the basis of a memory leak. To avoid this, you should let the compiler know that you intend to de-allocate the original memory and free up the pointer with:

int *ptr = new int[5];
delete[] ptr;
ptr = new int[7];

I have a related question, actually, but I'll ask it as a new post :)

I have a related question, actually, but I'll ask it as a new post :)

No need, answered my own question. OK, say you declare a pointer to a block of memory with new like you did, and then you move this pointer. What happens then, when you try to call delete[] on this pointer?

int *ptr = new int[5];
++ptr;
delete[] ptr;

The answer is nothing... at compile time. However, this will cause the program to spectacularly abort with an "trying to free and invalid pointer" style error. To safeguard against this, you can declare your pointer as const , like this:

int * const ptr = new int[5];
++ptr;
delete[] ptr;

Now, you get a compile-time error and it's easy to fix the problem with:

int *ptr = new int[5];
int *elementPtr = ptr;
++elementPtr;
delete[] ptr;
/* Don't need to delete elementPtr, since the memory has already been freed when we freed ptr */

I think that's all OK, maybe there's a really good reason not to declare pointers to blocks of memory as * const , if someone knows then I'd be interested to know.

Sorry to sort of hijack your thread.

lol dw, tnks for elaborating on the subject guys... I needed to know this because I'm trying to create an expandable queue by backing up the original dynamically allocated array, deleting it, allocating a new one with the required amount of memory and then reinstating the backed up data in the new array. That technique worked, but I think I could do it a bit easier if I just assigned a pointer to the original memory location, allocated memory to the original pointer, reinstate the old data in the new array, and delete the backup pointer, like this:

int *origPtr = new int[5];    // the original array
int *bckupPtr = origPtr;      // backup the old data
origPtr = new int[7];         // create new array

for(int i = 0; i < 5; ++i)
    origPtr[i] = bckupPtr[i]; // re-enter old data

delete[] bckupPtr;            // delete backup pointer

Do you think I've got any memory leaks with this code?

Do you think I've got any memory leaks with this code?

No, I don't think so. This should work as you expect.

This article has been dead for over six months. Start a new discussion instead.