5

I'm trying to get this clear up in my mind.

int* arrayA = new int[3];
int arrayB[3] = {1,2,3}

arrayA = arrayB;

Does the value of arrayB get copied over to arrayA? Or does arrayA become a pointer to arrayB?

1
  • For reference, int* arrayA declares a pointer-to-integer variable called arrayA. Calling it an array doesn't make it one, though. Commented Sep 24, 2013 at 12:53

5 Answers 5

7

Naked new is evil, especially for arrays. Use std::vector if you need dynamic array and std::array if you need static one.1

Now the actual answer is simple. arrayA is a pointer. So it's made to point to arrayB. That has additional effect that:

  1. you now leaked the allocated pointer.
  2. attempt to delete arrayA will now crash or something else unpleasant, because you would be trying to free unallocated memory.

Actually while C++ allows assigning structures and classes, it does not support assigning arrays at all. Unless part of an object either managed as in std::vector or as array member as in std::array (note: std::array is C++11).


1 The main reason is that the containers guarantee the resources will be released in their destructors, so called RAII idiom, avoiding most opportunities for memory leaks and dangling references.

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

2 Comments

Whilst I agree that using stl classes for managing pointers and arrays makes sense, the question was more about actually understanding what was going on at the raw level.
@user103052: I've added link to RAII, an important reason to prefer the high-level containers.
6
int* arrayA = new int[3];

arrayA now stores the address in memory of a different memory block thats large enough to contain 3 ints which is stored on the heap.

int arrayB[3] = {1,2,3};

arrayB is a block of memory thats large enough to contain 3 ints which is stored on the stack.

arrayA = arrayB;

Copies the address of the memory block that's stored on the stack into the variable arrayA. You have now lost the only reference to memory block stored on the heap, and have no way to free up the memory (a memory leak)

The memory stored on the stack and now pointed to by arrayA will not be safe to access when the current function returns. In particular returning arrayA (or arrayB) is not safe.

Comments

2

For

Does the value of arrayB get copied over to arrayA?

Nope. It will not copy arrayB to arrayA. If you want to do so, you can do like,

for (int i=0;i<3;i++) arrayA[i]=arrayB[i]; //This is two arrays has same value

does arrayA become a pointer to arrayB?

Yes. It become a pointer to array of arrayB. After arrayA = arrayB; statement, memory allocated in the first line can not be accessible and it is a leak.

arrayA=arrayB; result in copying the address of arrayB to arrayA. arrayA will start pointing to the arrayB. Check it using,

printf("\n Base address of A[%u] base address of B[%u]", arrayA, arrayB);

Hope this helps.

Comments

0

Array name represents the base address of the array.

arrayA=arrayB;

stores the base address of arrayB to arrayA. The further values can be accessed by incrementing the pointer arrayA.

i.e

*(arrayA) value is 1

*(arrayA+1) value is 2

*(arrayA+2) value is 3

The memory created should be freed at the last using

delete[] arrayA

Comments

0
Q. Does the value of arrayB get copied over to arrayA? 
Ans. NO

Or

Q. does arrayA become a pointer to arrayB?
Ans YES.

But the memory chunk which you allocated using new is lost. As no one is having the address which was starting point of that chunk.

This is vindicated by this program :

#include<iostream>
using namespace std;

const int MAX=4;
int main(){
   int* arrayA = new int[MAX];
   for(int i=0;i<MAX;i++){  arrayA[i]=i;    }
   int arrayB[MAX] = {10,20,30,40};
   arrayA = arrayB;
   for(int i=0; i<MAX;i++){ cout<<" a["<<i<<"] = "<<arrayA[i]<<endl;    }
   return 0;
}

The output of this code is :

 a[0] = 10
 a[1] = 20
 a[2] = 30
 a[3] = 40

on

g++ (Ubuntu 4.8.4-2ubuntu1~14.04) 4.8.4

which is true in general as well.

visualization of this is somewhat like this

arrayA-> [0][1][2][3]
arrayB-> [10][20][30][40]

Intially arrayA has address of [0] ( + sum extra memory on the negative indexes for size used by compiler to do the de-alloctation ).

after
arrayA=arrayB;

arrayA has address of [10];

suppose you call delete on arrayA what shall happen ??

*** Error in `./a.out': double free or corruption (out): 0x00007fff561281d0 ***

This is evident from the following code also

#include<iostream>
using namespace std;

const int MAX=4;
int main(){
    int* arrayA = new int[MAX*2];
    for(int i=0;i<MAX;i++){ arrayA[i]=i;    }
    int* arrayC = new int[MAX];
    int j=0;
    for(int i=MAX*100;i>=0;i-=10,j++){  arrayC[j]=i;    }
    arrayA = arrayC;
    for(int i=0; i<MAX;i++){    cout<<" a["<<i<<"] = "<<arrayA[i]<<endl;    }

    delete[] arrayA;
return 0;
}

The out put is :

 a[0] = 400
 a[1] = 390
 a[2] = 380
 a[3] = 370
*** Error in `./a.out': free(): invalid next size (fast): 0x0000000002028040 ***
Aborted (core dumped)

Just pay attention to :

"invalid next size"

This clearly says about the size value written before the arrayA in the negative indexes.

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.