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

  • SEARCH
  • Home
  • 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 7730727
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T06:17:53+00:00 2026-06-01T06:17:53+00:00

I’m currently learning about pointers in my C++ book (Programming: Principles and Practice using

  • 0

I’m currently learning about pointers in my C++ book (Programming: Principles and Practice using C++ by Stroustrup). The book had me do the following ‘drill’ to become accustom to pointers and arrays. I’ve commented parts of the drill that aren’t relevant to my issue.

int num = 7;
int* p1 = #

// output p1 address and content...

int* p2 = new int(10);

// initialise each element, and output content...

int* p3 = p2;
p1 = p2;

// output p1 and p2 address and content...

delete[] p1;

/* As all pointers now point to the same array created in the free store, 
   I was under the impression that I only needed to use delete for 1 of 
   the pointers to deallocate memory,as above, but the program crashes 
   if I don't do it for all 3 and execute next section of code? */

p1 = new int(10);
p2 = new int(10);

// Initialise each array to a different range of numbers using a loop,
// output each array, change elements in p2 to be the same as p1, output...

delete[] p1;
delete[] p2;

The last part is where I am having trouble. When outputting each array, the elements values are the same. My guess is that p1 still == p2, due to the code a few lines before. I thought that when you use the ‘new’ keyword it returns an address, referencing a different, newly allocated block of memory and therefore p1 would no longer == p2. The only way I got it to work was to directly create 2 arrays and have p1 and p2 reference them using the & operator. Any explanation as to what I’m doing wrong is appreciated.

  • 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-01T06:17:54+00:00Added an answer on June 1, 2026 at 6:17 am
    int* p2 = new int(10);
    
    // initialise each element, and output content...
    
    int* p3 = p2;
    p1 = p2;
    
    // output p1 and p2 address and content...
    
    delete[] p1;
    

    This code leads to undefined behavior, because you allocate with new and free the memory with delete[].

    int* p2 = new int(10);
    //allocates a single int with value 10
    

    is different from

    int* p2 = new int[10];
    //allocates an uninitialized array of 10 ints
    

    That aside (although a serious issue, as all undefined behavior), the problem was this:

    int* p2 = new int(10);
    int* p3 = p2;
    p1 = p2;
    //all pointer point to the same memory location
    
    delete[] p1;
    //delete that memory
    //all three pointers are now invalid
    

    Attempting to free the memory again via delete p2 or delete p3 will again lead to undefined behavior, and probably a crash, since you already deleted that memory. That is why allocating new memory will fix the crash.

    Bottom line: don’t free the same memory multiple times.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am reading a book about Javascript and jQuery and using one of the
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I am currently running into a problem where an element is coming back from

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.