0

SO,

I am running into an error which I cannot figure out (unless my understanding is just incorrect).

I have the following code:

    int doubleSize=size*2;
    int *newArr = new int[doubleSize];
    for(int i=0; i<size; i ++) {
        newArr[i]=jon[i];
    }
    size*=2;

    display(newArr);
    jon=newArr;
    display(jon);
    delete[] newArr;
    display(jon);

After the first and second calls I get exactly what I want/expect. On the third display call the 0 and 1 indices are memory addresses, the rest of the values in the indices match the previous 2 calls. What could be causing this?

I also have another follow up question, with code as I have it, will not deleting jon[] cause the 'old' jon[] to stay in memory?

Thanks!

3 Answers 3

3

You have undefined behavior, so anything can happen.

int *newArr = new int[size*2];
// now newArr is a pointer to a memory area
jon=newArr;
// now jon is a pointer to the same area, whatever jon pointed to before is leaked
delete[] newArr;
// now the memory area no longer exists
display(jon);
// using jon is now illegal, it has the address of a deleted memory area

Probably the right solution is:

int *newArr = new int[doubleSize]; // allocate a new area
for( int i=0; i<size; ++i ) {       // fill it in
    newArr[i] = jon[i];
}
delete [] jon; // get rid of the old area we don't need anymore
jon = newArr;  // now jon is linked to our brand new data in a brand new area
Sign up to request clarification or add additional context in comments.

5 Comments

Hmm; would not deleting newArr not cause it to stay in memory? And every time the function is called with this code there would be more and more memory being used w/ these worthless values? edit: I guess what I am asking would removing the delete[]newArr line make this code 'good'? It seems I would have problems every time this function is called, or would the new allocation of newArr overwrite the old?
@Dax: It's whatever jon pointed to before that stayed in memory but can no longer be used. So you should release that memory (after reading it into the new area, of course).
@DaxDurax, wellcome to the wonderful world of C/C++, where you are responsible of tracking what allocated values won't be used anymore, and can be safely destroyed. The so-called "higher-level languages" lift that burden (and the concomitant bugs) from the programmer, but at a high cost in memory usage and performance. If you are interested, there are garbage collectors for C/C++, look for Boehm.
Yeah, that was my original solution, however I have scope issues there (myArr is just temp in this function, jon isn't). I will have to look into the memcpy....thanks for the answer!
@vonbrand, Not really when you employ RAII.
3

When you delete[] newArr, you're unallocating the memory at address newArr. Since jon is also pointing to that same memory (because you set jon = newArr), the memory is being overwritten with some other values (probably in your display function). What you need to do, is use some copy function (like memcpy) to copy the data to a newly allocated block at jon, not just point jon at the same spot as newArr.

1 Comment

Actually, it's probably delete[] that overwrites the content. Many implementations do this intentionally to help catch use-after-free errors. Others store bookkeeping data in freed regions.
1

The last display call is trying to display jon that is the same as newArr - what you have just deleted! Thus the behaviour will be undefined.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.