内存泄漏的指针问题C ++

时间:2016-09-12 03:36:57

标签: c++

我正在尝试初始化一个数组,然后将内存重新分配到同一个地址。这是我的代码:

    //**begin #include files************
#include <iostream> // provides access to cin and cout

//--end of #include files-----------
//----------------------------------

using namespace std;
//----------------------------------

//**begin global constants**********
const int arraySize = 5;
//--end of global constants---------
//----------------------------------


//**begin main program**************
int main()
{
    cout << endl << endl;
    int* nArray = new int[arraySize];
    cout << "  --->After creating and allocating memory for nArray." << endl;
    cout << "  nArray address is <" << nArray << "> and contains the value " << hex << *nArray << dec << endl;
    for (int i = 0; i < arraySize; i++)
    {
        nArray[i] = i*i;
    }
    cout << "  --->After initializing nArray." << endl;
    cout << "  nArray address is <" << nArray << "> and contains the value " << hex << *nArray << dec << endl << endl;
    for (int i = 0; i < arraySize; i++)
    {
        cout << "  nArray[" << i << "] = " << nArray[i] << " at address <" << nArray + i << ">" << endl;
    }
    // You'll need a command here to fix the memory leak
    cout << endl << "  --->Before reallocating memory for nArray." << endl;
    cout << "  nArray address is <" << nArray << "> and contains the value " << hex << *nArray << endl;
    int* aux = new int[arraySize + 2];
    cout << dec << "  --->After reallocating memory for nArray." << endl;
    cout << "  nArray address is <" << nArray << "> and contains the value " << hex << *nArray << dec << endl;
    for (int i = 0; i<arraySize; i++) {
        aux[i] = nArray[i];
    }
    delete[] nArray;
    nArray = aux;
    cout << endl << "  --->After reinitializing nArray." << endl;
    cout << "  nArray address is <" << nArray << "> and contains the value " << hex << *nArray << dec << endl << endl;
    for (int i = 0; i < arraySize + 2; i++)
    {
        cout << "  nArray[" << i << "] = " << nArray[i] << " at address <" << nArray + i << ">" << endl;
    }
    // . . . and also here.
    cout << endl << "  --->Getting ready to close down the program." << endl;
    cout << "  nArray address is <" << nArray << "> and contains the value " << hex << *nArray << dec << endl;
    // Wait for user input to close program when debugging.
    cin.get();
    return 0;
}
//--end of main program-------------
//----------------------------------

我尝试过使用delete nArray;然后再次初始化但它不起作用。不知道该怎么做,但我搜索了几个小时,任何帮助将不胜感激!

注释行显示应添加语句的位置。

这是我需要的结果:

enter image description here

const int arraySize = 5;


int main()
{
    int* nArray = new int[arraySize];
    for (int i = 0; i < arraySize; i++)
        nArray[i] = i*i;

    int* aux = new int[arraySize + 2];
    for (int i = 0; i<arraySize; i++) 
        aux[i] = nArray[i];

    delete[] nArray;
    nArray = aux;
}

3 个答案:

答案 0 :(得分:1)

++ 是一种在C ++中实际重新分配矢量的方法。所以你要做的是:

  1. 分配一个更大尺寸的新矢量 - 将指针存储在aux
  2. 复制aux
  3. 内容中的旧nArray内容
  4. delete[] nArray vector
  5. 为nArray矢量指定辅助
  6. // 1,
    int* aux=new int[arraySize+2];
    // 2.
    // deliberately simple here
    for(size_t i=0; i<arraySize; i++) {
      aux[i]=nArray[i];
    }
    // 3.
    delete[] nArray; // the entire memory occupied bu nArray is recycled
    // 4.
    nArray=aux; // make nArray point to the newly allocated chunck
    
    // supplementary, adjust the arraySize, as it is now larger by 2
    // scratch that, the rest of the code works afterwards using arraySize + 2
    // arraySize += 2;
    

    或者,Stroustrup recommends使用std :: vector。

    ++ 对于某些值&#34; isn&#t; t&#34; - 见链接

答案 1 :(得分:1)

您可以使用“placement new

重复使用相同的位置

您的示例可以是:

//**begin #include files************
#include <iostream> // provides access to cin and cout

//--end of #include files-----------
//----------------------------------

using namespace std;
//----------------------------------

//**begin global constants**********
const int arraySize = 5;
//--end of global constants---------
//----------------------------------


//**begin main program**************
int main()
{
    int reserved[arraySize+2]; // pre allocated enough memory

    cout << endl << endl;
    int* nArray = new (reserved) int[arraySize]; // placement new place nArray at "reserved" location
    cout << "  --->After creating and allocating memory for nArray." << endl;
    cout << "  nArray address is <" << nArray << "> and contains the value " << hex << *nArray << dec << endl;
    for (int i = 0; i < arraySize; i++)
    {
        nArray[i] = i*i;
    }
    cout << "  --->After initializing nArray." << endl;
    cout << "  nArray address is <" << nArray << "> and contains the value " << hex << *nArray << dec << endl << endl;
    for (int i = 0; i < arraySize; i++)
    {
        cout << "  nArray[" << i << "] = " << nArray[i] << " at address <" << nArray + i << ">" << endl;
    }
    // You'll need a command here to fix the memory leak
    // Well, not now.
    cout << endl << "  --->Before reallocating memory for nArray." << endl;
    cout << "  nArray address is <" << nArray << "> and contains the value " << hex << *nArray << endl;
    int* aux = new (reserved) int[arraySize + 2]; // and again, placement new place aux at "reserved" location
    cout << dec << "  --->After reallocating memory for nArray." << endl;
    cout << "  nArray address is <" << nArray << "> and contains the value " << hex << *nArray << dec << endl;
    for (int i = 0; i<arraySize; i++) {
        aux[i] = nArray[i];
    }
    //delete[] nArray; // you can't do this now
    nArray = aux;
    cout << endl << "  --->After reinitializing nArray." << endl;
    cout << "  nArray address is <" << nArray << "> and contains the value " << hex << *nArray << dec << endl << endl;
    for (int i = 0; i < arraySize + 2; i++)
    {
        cout << "  nArray[" << i << "] = " << nArray[i] << " at address <" << nArray + i << ">" << endl;
    }
    // . . . and also here.
    cout << endl << "  --->Getting ready to close down the program." << endl;
    cout << "  nArray address is <" << nArray << "> and contains the value " << hex << *nArray << dec << endl;
    // Wait for user input to close program when debugging.
    cin.get();
    return 0;
}
//--end of main program-------------
//----------------------------------

答案 2 :(得分:0)

要删除数组,您必须执行以下操作:delete[] nArray;