Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9260769
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T12:58:41+00:00 2026-06-18T12:58:41+00:00

SO, I am running into an error which I cannot figure out (unless my

  • 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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T12:58:43+00:00Added an answer on June 18, 2026 at 12:58 pm

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am running into an error I am having trouble figuring out. I have
I am running into this error in my jenkins xcode build which I'm attempting
I'm running into 'Error: Java heap space, java.lang.OutOfMemoryError: Java heap space' when compiling an
I'm running into an error but I can't seem to find a solution. My
I'm running into an error with the parse_ini_file function. Here's the section of the
I'm running into an error, unhandled error from mysql_next_result(), that I've never seen before
I keep running into this error MemCacheError (Broken pipe): Broken pipe on my Rails
I am running into an error that I am unable to isolate the root
After changing the import as a from-import i'm running into this error: from datetime
I am building a XML RSS for my page. And running into this error:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.