用C ++释放锯齿状数组

时间:2015-08-27 11:21:01

标签: c++

#include <iostream>
#include <stdlib.h>
using namespace std;

float **div(float **A, float **B, int row, int column) {
  float **D;
  D = new float *[row];
  // D= ( float **)malloc(sizeof(float *) * row);
  for (int x = 0; x < row; x++) {
    D[x] = new float[column];
    // D[x] = (float (*))malloc(sizeof(float) * column);
    for (int y = 0; y < column; y++) {
      D[x][y] = (A[x][y] / B[x][y]);
    }
  }

  return D;
}

int main() {
  int x = 0, y = 0, row = 20, column = 5;
  float **N;
  // N =(float **)malloc(sizeof(float *) * row);
  N = new float *[row];
  for (x = 0; x < row; x++) {
    N[x] = new float[column];
    // N[x]=(float*)malloc(row*column*sizeof(float));
    for (y = 0; y < (column); y++) {
      N[x][y] = 1;
    }
  }

  N = div(N, N, row, column);
  cout << "Displaying  N  ..." << endl;
  for (int x = 0; x < 10; x++) {
    for (int y = 0; y < 5; y++) {
      cout << N[x][y] << " ";
    }
    cout << endl;
  }
  for (int x = 0; x < 20; x++) {
    delete[] N[x];
  }
  delete[] N;
  return 0;
}

我试图释放所有内存,但它释放了我在main中声明的内存,但函数div也占用了大小输入数组N的相同内存,并且它没有被释放。有没有办法删除占用该功能的内存。

2 个答案:

答案 0 :(得分:0)

请看这里,我的新示例并删除pf->dest_pid,我希望这可以帮助您:

int[m][n]

答案 1 :(得分:0)

使用OrderSend(symbolName, lotsNumber, bid, slip, 0, takeProfit, coment, Red);将删除内存管理:

std::array

Demo