Link to home
Start Free TrialLog in
Avatar of panos2009
panos2009

asked on

3 dimension array memory problem in C++

I want to have a 3 dimentional matrix of chars as matrix[i][j][k]
i is defined as MAXVAR_A, j and k are not known. There must be problem whit hte free() code because the first time I call the function and allocate memory is ok. But when I call the function for second or third time, i get memory error.

Here is the code:

char **matrix[MAXVAR_A];      


makearray(int MAXVAR_J,int MAXVAR_K)
{
int i,j,k;
      
      for (i=0;i<MAXVAR_A;i++)
            for (j=0;j<=MAXVAR_J;j++) matrix[i]=new char*;
      for (k=0;k<MAXVAR_K;k++)
            for (i=0;i<MAXVAR_A;i++)
                  for (j=0;j<=MAXVAR_J;j++) matrix[i][j]=new char;

.......
code that is working using matrix array
.......

      //free matrix
      for (i=0;i<MAXVAR_A;i++)
            for (j=0;j<=MAXVAR_J;j++) free(matrix[i][j]);
      for (x=0;x<=MAXVAR_J;x++)
            free(matrix[x]);
}

Avatar of Zoppo
Zoppo
Flag of Germany image

Hi  panos2009,

IMO the problem is you mix up C and C++ allocation/release functions.

Instead of 'free(...)' you have to use 'delete [] ...'

Hope that helps,

ZOPPO
BTW, the allocation you use doesn't do what you expect I guess - i.e. in the first loop you allocate MAXVAR_J times a 'char*' for each 'matrix[i]' - this doesn't make sense since only the last one will be left in 'matrix[i]'.
ASKER CERTIFIED SOLUTION
Avatar of Infinity08
Infinity08
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
To add to the last comments:

Instead of allocating memory for each "cell" of the matrix, you also could allocate one big piece of memory and point to that memory from your "cells".

And some code:


char **matrix[MAXVAR_A]; 
char * all = NULL;

void makearray(int MAXVAR_J,int MAXVAR_K)
{
    int i,j;
    all = new char[MAXVAR_A * MAXVAR_J * MAXVAR_K];
    char * p   = all;

    for ( i = 0; i < MAXVAR_A; i++ )
    {
       matrix[i]=new char*[MAXVAR_J];
       for ( j = 0; j < MAXVAR_J; j++ )
       {
          matrix[i][j] = p;
          p += MAXVAR_K;
       }
    }
}

void deletearray()
{
    int i = 0;
    for ( i = 0; i < MAXVAR_A; i++ )
        delete []matrix[i];
    delete [] all;
    all = NULL;
}

Open in new window

Avatar of phyderous
phyderous

What you guys think about this

int i =10,j=10,k=10;

char *a = new char [i*j*k];
char ***b = (char ***)a;

b[5][7][8] = 'c';

delete[] a //I think delete a should also work on most compillers
Well, that's quite similar to the last posted one:
> all = new char[MAXVAR_A * MAXVAR_J * MAXVAR_K];
> ...
>  delete [] all;
>> char ***b = (char ***)a;

dangerous cast.

>> b[5][7][8] = 'c';

won't work ;)
work more then fine you may try and compile it to see for yourself

no reason for it not to work because there is no difference between b[5][7][8] and b[5*7*8]
this is what the compiler is doing the rest is only in the programmer head :)

[x] equals sizeof(type)*5 ...

I suggest you go back to the basics ...
typeo

type a[x] equals sizeof(type)*x ...
"there is no difference between b[5][7][8] and b[5*7*8]"

There is, actually...

b[x][y][z] means b + (x * (max_z) * (max_y)) + (y * (max_z)) + (z)

... where max_z is the size of the third dimension, and max_y is the size of the second dimension. You have to take the size of each dimension into account when calculating the memory offset, not just multiply the indexes.

However, the original author isn't looking for that level of detail, he's just trying to solve his memory allocation, which looks like was done nicely by Infinity08 and Zoppo. Itsmeandnobo also provides a suggestion of simplifying the allocation.

Arrays of this size start getting pretty hairy, though, sometimes it's worth revisiting why you need it.
Avatar of panos2009

ASKER

t
>> work more then fine you may try and compile it to see for yourself

Did you ? You might be surprised ... :)