仅在达到一定限制之前才释放2D指针数组的内存

时间:2018-07-10 10:25:20

标签: c++ c++11 pointers

我有一个使用指针动态创建的2D数组。我只想删除某些连续的行,而不是全部。这是我编写的要删除的代码:

#include <iostream>
using namespace std;

void clearMemoryAll(int **matchedIndicesArray, int rows)
{

  for(int i = 0; i < rows; i++)
  {
    delete [] matchedIndicesArray[i];
  }

  delete [] matchedIndicesArray;

}

int main()
{
  // Program having 10M x 4 = 40M elements

  int rows = 10000000;
  int **matchedStagesMatrix;
  matchedStagesMatrix = new int*[rows];

  int cols = 4;


  for(int i = 0; i < rows; i++)
  {
    matchedStagesMatrix[i] = new int[cols];
    for (int j = 0; j < cols; j++)
    {
      matchedStagesMatrix[i][j] = 1;
    }
  }



  clearMemoryAll(matchedStagesMatrix, rows);
  while (1) {}


  return 0;
}

很显然,此代码将删除2D数组的所有行。如何仅删除某些前100行而不是一次删除所有行?我不能简单地将100作为参数传递给该函数,因为当控件到达函数的for循环之外时,它总是尝试删除完整的矩阵。矩阵应删除,以便在删除某些行后仍然可以使用。 我知道向量是一个很好的选择,但我对指针的工作方式以及如何代替使用向量进行操纵感到好奇。

编辑:另外,我计划多次使用此删除功能,即我将一次删除矩阵行一次,每次仅删除某些行,直到所有行都被删除为止。已删除。因此,不能每次都执行for循环之外的最后一行。

2 个答案:

答案 0 :(得分:3)

如果使用向量,可以做到这一点,由于向量的方法,向量更易于处理。

int n,x;
std::cin>>n>>x;
std::vector<int*> myVec;
int* row=new int[n];
for(int i=0;i<n;i++)
    std::cin>>row[i];
myVec.push_back(row);
//do this for all your rows;
myVec.erase(myVec.begin(),myVec.end()+x); //delete first x rows;
//you can play with the line above to delete lines in a range or sth

答案 1 :(得分:1)

还有其他方法可以完成您要完成的任务,但可以说,我们坚持您在问题中所讨论的方式。

如果删除某行,则需要记住删除的那一行,并确保不要再次访问它。

为此,您可以维护一个标志数组,其大小等于行数。这些标志起初都是0。

示例-如果2D数组中总共有10行,则

int flag[rows] = 0;

删除某一行时,可以将该行的标志值更改为1。

示例-如果我们删除第四行,则

flag[3] = 1;

之后,每当需要遍历2D数组时,都可以跳过标志值等于1的行。

这是我修改的示例代码:

#include <iostream>
using namespace std;


void printArray(int **matchedIndicesArray,int flag[], int rows, int cols)
{
    for(int i=0; i<rows; i++)
    {
    if(flag[i]==1)    //if flag for the row is 1, that means it is deleted, and so we skip it
    {
      continue;
    }
        for(int j=0; j<cols; j++)
        {
            cout<<matchedIndicesArray[i][j]<<" ";
        }
        cout<<endl;
    }
    cout<<endl;
}

void clearMemorySome(int **matchedIndicesArray, int flag[], int rows)
{

  for(int i = 0; i < rows/2; i++)  //here I chose to delete half the rows
  {
    delete [] matchedIndicesArray[i];
    flag[i] = 1;   //to remember which row has been deleted, we change the value of flag to 1
  }
  return;
  //delete [] matchedIndicesArray;   //this is commented out because we are only deleting certain rows at a time

}

int main()
{
  // Program having 10 * 3 = 30 elements

  int rows = 10;
  int **matchedStagesMatrix;
  matchedStagesMatrix = new int*[rows];

  int cols = 3;

  int flag[rows]={0};    //initially the flag value for every row is 0

  for(int i = 0; i < rows; i++)
  {
    matchedStagesMatrix[i] = new int[cols];
    for (int j = 0; j < cols; j++)
    {
      matchedStagesMatrix[i][j] = 1;
    }
  }

  cout<<"The 2D array before half of the rows are deleted\n";
  printArray(matchedStagesMatrix, flag, rows, cols);

  clearMemorySome(matchedStagesMatrix, flag, rows);
  cout<<"The 2D array after half of the rows are deleted\n";
  printArray(matchedStagesMatrix, flag, rows, cols);

  return 0;
}

以上代码的输出为:

  

删除一半行之前的2D数组

     

1 1 1 1 1 1

     

1 1 1

     

1 1 1

     

1 1 1

     

1 1 1

     

1 1 1

     

1 1 1

     

1 1 1

     

1 1 1

     

删除一半行后的2D数组

     

1 1 1

     

1 1 1

     

1 1 1

     

1 1 1

     

1 1 1