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 7723953
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T04:34:23+00:00 2026-06-01T04:34:23+00:00

First off, I apologize if this has been asked before. I can’t seem to

  • 0

First off, I apologize if this has been asked before. I can’t seem to find the right info.

The following code does not print “300” as I thought it would:

#include <iostream>

int main()
{
  int *array;
  int *arrayCopy = array;

  array = new int[4];

  array[0] = 100;
  array[1] = 200;
  array[2] = 300;
  array[3] = 400;

  std::cout << arrayCopy[2];

  return 0;
}

However, it does, if I move the line

int *arrayCopy = array;

below the line that follows it in the above code. Why is that?

(PS: I know there is a memory leak, and that std::vector is better… I’m just curious).

  • 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-01T04:34:24+00:00Added an answer on June 1, 2026 at 4:34 am

    Maybe you’re thinking of using a reference to a pointer? Here’s what happens with your current code:

    int *array; // Currently points to an undefined (invalid) memory location.
    int *arrayCopy = array; // Now this points to the same undefined memory location as array.
    
    array = new int[4]; // Now array points to valid memory, but arrayCopy still points to undefined space.
    

    If you did something like this though, it’s different:

    int *array; // Points to undefined
    int *&arrayCopy = array; // This is a reference to array. That means if you change array, arrayCopy will also reflect the changes.
    
    array = new int[4]; // Now since array points to valid space, arrayCopy does too.
    

    Technically, this isn’t entirely true because different things are happening. A reference is essentially the same level of indirection as a pointer, except the compiler does all the dereferencing for you. But what I described is essentially how it works. If you just stick the extra & in there, your code will do what you were thinking.

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

Sidebar

Related Questions

First off, let me apologize if this has been asked already, but I can’t
First off, I apologize if this question has been asked before. I've done a
First off, apologies if this question has been asked before but I couldn't find
Ok, first off, I applogize if this question has been asked before, I've spent
First off - apologies if this or a similar question has been asked before.
First off, I'd like to apologize in advance for not knowing this. I've been
First off I apologize... I have posted this question before, but I did a
First off, I apologize if this doesn't make sense. I'm new to XHTML, CSS
First of all I apologize in advance for this question, a bit off the
First off I apologize if there is another post out there that answers this,

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.