通过创建新的C ++数组并指向它来扩展它

时间:2017-04-27 05:17:53

标签: c++ arrays pointers

这个问题的标题可能不清楚,但我要做的是编写一个带有数组的程序,比如{1,4,7},并将该数组转换为{1,4,7,1 ,4,7}。

#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.95257e-310,6,6”。它应该是“2,4,6,2,4,6”。但是,当SIZE不是3时,代码可以工作。这里有什么想法吗?

3 个答案:

答案 0 :(得分:4)

你有一些错误。

首先,您要按值传递数组指针,因此当您在函数内部更改它时,您只需更改其内部副本。您需要通过引用 &传递它。

接下来,您不会在{}循环中使用大括号for,因此它只运行您需要它运行的两个语句中的一个。

最后,您永远不会删除原始数组,因此您有内存泄漏。

#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;
}

答案 1 :(得分: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)中的指针只是myArraymain()的副本,myArray的值根本不会改变

答案 2 :(得分: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;
}