1

I'm looking for someone to correct my understanding of pointers and arrays.

Say I have two arrays:

int* array1 = new int[10];
int* array2 = new int[10]{ 1,2,3,4,5,6,7,8,9,10 };

When I do array1 = array2 I get the following address values:

array1: 0000027964C831D0
array2: 0000027964C831D0
&array1[0]: 0000027964C831D0
&array2[0]: 0000027964C831D0

When I do for (size_t i = 0; i < 10; i++) { array1[i] = array2[i]; } I get:

array1: 0000027964C832B0
array2: 0000027964C83710
&array1[0]: 0000027964C832B0
&array2[0]: 0000027964C83710

The first case (non-looping) makes sense to me. I'm assigning the address of the array in array2 to array1 so now I have two different pointers pointing to the same array (or more specifically, array2[0]), hence &array1[0] and &array2[0] are the same.

But with the second case (looping) from the start, the pointers have different addresses. Am I right in saying that the for-loop created a copy of the array? Though that sounds wrong to me.

9
  • 8
    The loop copied the values from one array to the other. You already had 2 arrays to start with (due to the new int[10]; initializations) Commented Oct 20, 2020 at 15:04
  • @UnholySheep Thanks for the comment. Here is what I understand then. In the first case, array1 points to array2 and there is a memory leak if I don't delete before doing array1 = array2. For the second case, all the values of array2 are just copied to array1. So even if I don't delete before looping there is no memory leak? Commented Oct 20, 2020 at 15:08
  • In the first case, array1 and array2 are pointers. In second case you are actively copying by value. Edit: removed unecessary usage of "essentially" Commented Oct 20, 2020 at 15:08
  • @Aditya Not only "essentially" - they are pointers. Commented Oct 20, 2020 at 15:09
  • @molbdnilo unnecessary usage of words has been a bad habit for me...my bad lol Commented Oct 20, 2020 at 15:11

1 Answer 1

4

Pointers are addresses. Think of them as pieces of paper with a house address written on them.

int* array = new int[10];

is building a row of 10 houses (new int[10]), then writing down where those houses are and storing the address in array.

Saying array is an array is a problem that will confuse you. It is mistaking the address of a house for a house. A piece of paper saying 123 road street isn't my house, even if it uniquely names my house.

Mistaking the map for the territory is really easy, and leads to confusion. You have to do some work to untangle that.

array1=array2 is taking the piece of paper array1 and writing the address of the 2nd block of houses over it. It is doing nothing to the houses. They are still out there -- but you might no longer have a record of where they are. So now they will become derelict and lost (this is called a memory leak).

array1[0] = array2[0] taking the piece of paper called array2, walking to the address listed on it, and looking at the first house there (aka 0). Then it is pulling out a scanner and scanning the entire house.

You then walk over to the spot array1 says there is a row of houses. You go to the first house (aka 0) and you pull out your scanner. You flip the switch from scan to print, and aim it at the house.

It proceeds to destroy the house that is there, and print a copy of the house you scanned over on the block named by array2.

You then walk back to the block named by array2, scan array2[1] the 2nd house on the block, walk back and print it over array1[1] the second house on the first block.

This is a very, very different operation than scratching the address of the block on a piece of paper and replacing it.

Now, &array1[0] is going to the block named by array1, going to the first house [0], then reading the address written on it (&). As we name blocks by the address of the first house, this turns out to match what we have written down in array1 itself.

array1[1] is going to have a different address -- &array1[0] -- but it will be next-door to array1[0].

Sign up to request clarification or add additional context in comments.

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.