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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • raphfrk

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

    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<siz ex;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<siz ex;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 ?
  • David Resnick

    #2
    Re: 2-d dynamic arrays - what is the recommended method ?

    On Jul 3, 1:30 pm, raphfrk <raph...@netsca pe.netwrote:
    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<siz ex;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<siz ex;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:



    -David

    Comment

    • raphfrk

      #3
      Re: 2-d dynamic arrays - what is the recommended method ?

      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.

      Comment

      • rahul

        #4
        Re: 2-d dynamic arrays - what is the recommended method ?

        On Jul 4, 4:50 am, raphfrk <raph...@netsca pe.netwrote:
        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.

        Comment

        • Barry Schwarz

          #5
          Re: 2-d dynamic arrays - what is the recommended method ?

          On Thu, 3 Jul 2008 10:30:00 -0700 (PDT), raphfrk
          <raphfrk@netsca pe.netwrote:
          >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<siz ex;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<siz ex;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

          Comment

          • raphfrk

            #6
            Re: 2-d dynamic arrays - what is the recommended method ?

            On Jul 4, 10:11 am, Barry Schwarz <schwa...@dqel. comwrote:
            >
            (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.

            Comment

            • Ben Bacarisse

              #7
              Re: 2-d dynamic arrays - what is the recommended method ?

              raphfrk <raphfrk@netsca pe.netwrites:
              While I am here, I just thought I'd ask if there is a recommended way
              of handling 2d dynamic arrays.
              <snip malloc array of malloc 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.

              --
              Ben.

              Comment

              Working...