2017-04-27 5 views
1

この質問のタイトルは不明ですが、私がしようとしているのは配列{1、4、7}とその配列を{1,4,7,1,4,7}に変換します。新しい配列を作成して指し示すようにC++配列を拡張する

#include <iostream> 

using namespace std; 

void repeatArray(double* arr, int size) 
{ 
    double* newArr = new double[size*2]; 

    double* ptr = newArr; 

    int counter = 0; 
    for (int j = 0; j < 2; j++) 
    { 
     for (int i = 0; i < size; i++) 
     { 
      newArr[counter] = arr[i]; 
      counter++; 
     } 
    } 

    arr = ptr; 
} 

int main() 
{ 
    int SIZE = 3; 
    double* myArray = new double[SIZE]; 
    for (int i=0; i<SIZE; i++) 
     myArray[i] = (i+1)*2; 

    repeatArray(myArray, SIZE); 

    for (int i=0; i<SIZE*2; i++) 
     cout << myArray[i] << endl; 

    delete[] myArray; 
    myArray = nullptr; 

    return 0; 
} 

上記のコードは、 "2,4,6、6.95327e-310,6,6"を出力します。それは "2,4,6,2,4,6"でなければなりません。しかし、コードは、SIZEが3以外のときに機能します。

+0

デバッガを使用してみますか? – CinCout

+0

試したrealloc()? –

答えて

4

あなたはいくつかのエラーがあります。

まず、配列ポインタを値渡しします。そのため、関数内で配列ポインタを変更すると、その内部コピーのみが変更されます。 参照&で渡す必要があります。

次に、forループを丸めるために、中括弧{}はありません。実行するために必要な2つのステートメントのうちの1つだけを実行します。

最後に、元の配列を削除してメモリリークを起こすことはありません。

#include <iostream> 

using namespace std; 

// if you want to change the value of arr outside 
// the function pass by reference &, otherwise 
// you only change a copy of the pointer internal to the function 
void repeatArray(double*& arr, int size) 
{ 
    double* newArr = new double[size*2]; 

    // double* ptr = newArr; // this doesn't seem to do much 

    int counter = 0; 
    for (int j = 0; j < 2; j++) 
    { 
     // use braces {} otherwise the for only loops ONE statement 
     // but you need to loop BOTH statements here 
     for (int i = 0; i < size; i++) 
     { 
      newArr[counter] = arr[i]; // statement #1 
      counter++;    // statement #2 
     } 
    } 

    delete[] arr; // otherwise you have a memory leak 

    arr = newArr; 
} 

int main() 
{ 
    double* myArray = new double[3]; 
    for (int i=0; i<3; i++) 
     myArray[i] = (i+1)*2; 

    repeatArray(myArray, 3); 

    for (int i=0; i<6; i++) 
     cout << myArray[i] << endl; 

    delete[] myArray; 
    myArray = nullptr; 

    return 0; 
} 
0
#include <iostream> 

using namespace std; 

double* repeatArray(double* arr, int size) 
{ 
    double* newArr = new double[size*2]; 

    double* ptr = newArr; 

    int counter = 0; 
    for (int j = 0; j < 2; j++) 
    { 
     for (int i = 0; i < size; i++) 
      newArr[counter] = arr[i]; 
      counter++; 
    } 
    //this 'arr' just a copy of myArray in side of function 
    //arr = ptr;//here, you just assign to a variable inside of function,myArray outside of function has no change. 


//you should return the new pointer 
return newArr; 
} 

int main() 
{ 
    double* myArray = new double[3]; 
    for (int i=0; i<3; i++) 
     myArray[i] = (i+1)*2; 

    double* newArray = repeatArray(myArray, 3); 

    for (int i=0; i<6; i++) 
     cout << newArray [i] << endl; 

    delete[] myArray; 
    myArray = nullptr; 
    delete [] newArray ; 
newArray = nullptr; 
    return 0; 
} 
+0

リサイズ機能でぎこちないメモリリークを修正したいかもしれません。 – WhozCraig

+0

助けていただきありがとうございますが、この解決法はどちらかといえないようです。また、問題の制約は、メソッドを無効にする必要があることです。 – Asif

+0

@Asifの場合、参照先ポインタまたはポインタへのポインタを渡す必要があります。前者は、既存の構文を保持したい場合に使用します。どういうわけか、呼び出し元は新しいシーケンスアドレスを取得する必要があります。そして、立っている問題の制約はあなたの*質問*の一部でなければなりません。 – WhozCraig

1

ポインタを参照渡しし、値渡ししないでください。

void repeatArray(double*& arr, int size) 
{      //^ Pass it by reference. 
    double* newArr = new double[size * 2]; 

    double* ptr = newArr; 

    int counter = 0; 
    for (int j = 0; j < 2; j++) 
    { 
     for (int i = 0; i < size; i++) { 
      newArr[counter] = arr[i]; 
      counter++; 
     } 

    } 
    delete[] arr; // don't forget to release the memory you allocated 
    arr = ptr; 
} 

int main() 
{ 
    double* myArray = new double[3]; 
    for (int i=0; i<3; i++) 
     myArray[i] = (i+1)*2; 

    repeatArray(myArray, 3); 

    for (int i=0; i<6; i++) 
     cout << myArray[i] << endl; 

    delete[] myArray; 
    myArray = nullptr; 

    return 0; 
} 

あなたは値によってポインタを渡すと、void repeatArray(double*& arr, int size)でポインタがmain()myArrayの単なるコピーである、myArrayの値は全く変わりません。

関連する問題