1

i declare a dynamic array int *idArray; and i want to copy the value to a tempArray, then i will change the order of tempArray, but i don't want to change the order idArray, how can i write it? I have tried to implement it, but when the order of tempArray be changed, the order of idArray also change.

int *idArray = new int[size]; 
int *tempArray = idArray;
int m, n;
for(int k = 0; k < size; k++) {
    m = rand() % size;
    n = tempArray[m];
    tempArray[m] = tempArray[k];
    tempArray[k] = n; 
}
LoveTW
  • 3,746
  • 12
  • 42
  • 52
  • 4
    if you are talking about c++, why not std::array or at least std::vector? do you really need raw pointers? It is not a temporary array, it is a pointer. – CyberGuy Apr 18 '12 at 11:59

4 Answers4

5
std::vector<int> idArray(size); 
std::vector<int> tempArray = idArray;

Problemo solvo.

Also, you can use std::random_shuffle- you don't have to shuffle the vector yourself.

Puppy
  • 144,682
  • 38
  • 256
  • 465
1

I'd suggest using std::vector<> for array. std::vector<> will handle copying and other memory management for you. Plus it remembers array's size. (new[] doesn't.)

Otherwise you need to do following:

int *idArray = new int[size]; 
int idArray_size = size; // note this. you need to save array's size somewhere.
.......
int *tempArray = new int[idArray_size];

/* copy */
for(int i = 0; i < idArray_size; i++)
    tempArray[i] = idArray[i];

int m, n;
for(int k = 0; k < size; k++) {
    m = rand() % size;
    n = tempArray[m];
    tempArray[m] = tempArray[k];
    tempArray[k] = n; 
}
nothrow
  • 15,882
  • 9
  • 57
  • 104
  • Hmmm... Dubious. If I were you, I would just show the code using `std::vector`. As it is, it's border-line downvotable. Right now, I decided not to, though. – Puppy Apr 18 '12 at 12:04
  • 2
    You can do the copying semantically more clearly (and possibly also more efficiently) by using `std::copy(idArray, idArray+size, tempArray);` – Michael Wild Apr 18 '12 at 12:05
  • @DeadMG, it depends. I answered his question (not wanting to use `std::array` can be perfectly valid scenario), plus suggested better solution. I don't want to force him to use 'c++' way, when others are possible. – nothrow Apr 18 '12 at 12:09
  • 1
    @Yossarian: He's coding in C++. What other way is there to code in C++ except the C++ way? – Puppy Apr 18 '12 at 12:56
0

Because your temp array is Pointed On your Id array

int *tempArray = idArray;

So in stead of this you Declare Yout *tempArray and manualy copy value from Idarray to temp Array

Chlebta
  • 3,090
  • 15
  • 50
  • 99
  • 1
    Actually, merely _declaring_ your own array isn't enough to also use it. You will have to _[define](http://stackoverflow.com/a/1410632/140719)_ it. – sbi Apr 18 '12 at 12:18
-3

This is the actual memory you are allocating and idArray points to it:

int *idArray = new int[size]; 

And this is just a pointer to that same memory address:

int *tempArray = idArray;

You should declare tempArray same as idArray and then just copy the content to it:

int *idArray = new int[size];
int *tempArray = new int[size];
memcpy(tempArray,idArray,size * sizeof(int));
drodil
  • 2,292
  • 1
  • 22
  • 37