dsmart
Newbie level 1
Hi all. Help me please to write some code.
I have 2-dimension and 3-dimension arrays. Firs part of code just allocating memory for them and delete both arrays immediately. All OK, there is no memory leak effect.
Now I’m trying to copy 2-dim array into one of elements of 3-dim array. SO, need to delete "delete[]" operator from ending cycles to avoid EAccess violation error (non existing element). And i’m using delete[] Ar2[1]; before copying address of 2-dim array into it, but still have memory leak.
Tell me, where i miss. Program is allocating about 500 kB of RAM after each button press, when executing second code
I have 2-dimension and 3-dimension arrays. Firs part of code just allocating memory for them and delete both arrays immediately. All OK, there is no memory leak effect.
Code C++ - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 int n = 10; double ***Ar2, **B; void __fastcall TForm1::Button3Click(TObject *Sender) { Ar2 = new double**[n]; B = new double *[n]; for (int k=0; k<n; k++) { Ar2[k] = new double*[n]; B[k] = new double[n]; } for (int k=0; k<n; k++) for (int j=0; j<n; j++) { Ar2[k][j] = new double[n]; B[k][j] = j; } //delete arrays for (int k=0; k<n; k++) for (int j=0; j<n; j++) delete[] Ar2[k][j]; for (int k=0; k<n; k++) { delete[] Ar2[k]; delete[] B[k]; } delete[] Ar2; delete[] B; }
Now I’m trying to copy 2-dim array into one of elements of 3-dim array. SO, need to delete "delete[]" operator from ending cycles to avoid EAccess violation error (non existing element). And i’m using delete[] Ar2[1]; before copying address of 2-dim array into it, but still have memory leak.
Code C++ - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 int n = 10; double ***Ar2, **B; void __fastcall TForm1::Button3Click(TObject *Sender) { Ar2 = new double**[n]; B = new double *[n]; for (int k=0; k<n; k++) { Ar2[k] = new double*[n]; B[k] = new double[n]; } for (int k=0; k<n; k++) for (int j=0; j<n; j++) { Ar2[k][j] = new double[n]; B[k][j] = j; //fill array by some data } delete[] Ar2[1]; //delete content of Ar2[1] Ar2[1] = B; //delete arrays for (int k=0; k<n; k++) for (int j=0; j<n; j++) delete[] Ar2[k][j]; for (int k=0; k<n; k++) { delete[] Ar2[k]; // delete[] B[k]; ******EAccess violation if not commented******* } delete[] Ar2; delete[] B; }
Tell me, where i miss. Program is allocating about 500 kB of RAM after each button press, when executing second code