Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Dynamic Arrays C++ (Builder 6)

Status
Not open for further replies.

dsmart

Newbie level 1
Joined
Dec 7, 2011
Messages
1
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,297
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.

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
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top