2-d dynamic arrays - what is the recommended method ?

R

raphfrk

While I am here, I just thought I'd ask if there is a recommended way
of handling 2d dynamic arrays.

I implement them using the following kind of code.

------------------------------------------------

int fname( int sizex, int sizey, <other inputs> )
{

int *(*bitmap);

int cntx;

bitmap = malloc( sizeof( *bitmap ) * sizex );
if( bitmap == NULL )
{
printf("Unable to allocate enough RAM in fname\n");
exit(0);
}

for( cntx=0;cntx<sizex;cntx++)
{
bitmap[cntx] = malloc( sizeof( *(bitmap[cntx]) ) * sizey );
if( bitmap[cntx] == NULL )
{
printf("Unable to allocate enough RAM in fname\n");
exit(0);
}
}

<code>

for( cntx=0;cntx<sizex;cntx++)
{
free(bitmap[cntx]);
}

free(bitmap);

}

-----------------------------------------

This seems to require that the following code be used to access each
element

(bitmap[xpos])[ypos]

Is there a neater way of doing it ?
 
D

David Resnick

While I am here, I just thought I'd ask if there is a recommended way
of handling 2d dynamic arrays.

I implement them using the following kind of code.

------------------------------------------------

int fname(  int sizex, int sizey, <other inputs> )
{

int *(*bitmap);

int cntx;

bitmap = malloc( sizeof( *bitmap ) * sizex );
if( bitmap == NULL )
 {
 printf("Unable to allocate enough RAM in fname\n");
 exit(0);
 }

for( cntx=0;cntx<sizex;cntx++)
 {
 bitmap[cntx] = malloc( sizeof( *(bitmap[cntx]) ) * sizey );
 if( bitmap[cntx] == NULL )
  {
  printf("Unable to allocate enough RAM in fname\n");
  exit(0);
  }
 }

<code>

for( cntx=0;cntx<sizex;cntx++)
 {
 free(bitmap[cntx]);
 }

free(bitmap);

}

-----------------------------------------

This seems to require that the following code be used to access each
element

(bitmap[xpos])[ypos]

Is there a neater way of doing it ?

Have a look here:

http://c-faq.com/aryptr/dynmuldimary.html

-David
 
R

raphfrk

That is what I thought I did the first time, but had to put in the
brackets to get it to work (which is why I always use brackets now).

I guess I must have fixed the actual coding error by accident.
 
R

rahul

That is what I thought I did the first time, but had to put in the
brackets to get it to work (which is why I always use brackets now).

I guess I must have fixed the actual coding error by accident.

You mean:
int *(*bitmap);
The brackets are not required and are making the code unreadable. What
kind of errors you were getting without them?
This seems to require that the following code be used to access each
element
(bitmap[xpos])[ypos]

Is there a neater way of doing it ?

How is this dirty? This seems to be the neatest way to access
individual elements in a 2-d array.
 
B

Barry Schwarz

While I am here, I just thought I'd ask if there is a recommended way
of handling 2d dynamic arrays.

I implement them using the following kind of code.

------------------------------------------------

int fname( int sizex, int sizey, <other inputs> )
{

int *(*bitmap);

These parentheses are superfluous. int **bitmap is fine.
int cntx;

bitmap = malloc( sizeof( *bitmap ) * sizex );
if( bitmap == NULL )
{
printf("Unable to allocate enough RAM in fname\n");
exit(0);
}

for( cntx=0;cntx<sizex;cntx++)
{
bitmap[cntx] = malloc( sizeof( *(bitmap[cntx]) ) * sizey );
if( bitmap[cntx] == NULL )
{
printf("Unable to allocate enough RAM in fname\n");
exit(0);
}
}

<code>

for( cntx=0;cntx<sizex;cntx++)
{
free(bitmap[cntx]);
}

free(bitmap);

}

-----------------------------------------

This seems to require that the following code be used to access each
element

(bitmap[xpos])[ypos]

The same for these parentheses. bitmap[xpos][ypos] works.
Is there a neater way of doing it ?

There is an alternative but I don't consider it neater.

int *bitmap = malloc(sizex * sizey * sizeof *bitmap);

Everywhere you would like to use bitmap[x][y] you would need to use
bitmap[x*sizey+y]. Some even go so far as to create a macro
#define BITMAP(x,y) bitmap[x*sizey+y]
for this purpose.


Remove del for email
 
R

raphfrk

(bitmap[xpos])[ypos]

The same for these parentheses. bitmap[xpos][ypos] works.

Yeah, that is what I wanted. Not sure what I must have done wrong,
but it was giving segmentation faults. I then included the brackets
from then on, I must have fixed 2 things at once.
 
B

Ben Bacarisse

raphfrk said:
While I am here, I just thought I'd ask if there is a recommended way
of handling 2d dynamic arrays.
Is there a neater way of doing it ?

There is if C99 is an option. You can just declare:

int bitmap[sizex][sizey];

or, if you prefer the safety of malloced arrays:

int (*bitmap)[sizey] = malloc(sizex * sizeof *bitmap);

and in both cases you keep the bitmap[x][y] access syntax.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top