Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 3 of 3

Thread: 2d arrays

  1. #1
    Junior Member Newbie
    Join Date
    Apr 2004
    Posts
    13

    2d arrays

    I need to implement a 2D array of a variable size but i dont know how to malloc 2d arrays anyone know how??

    thanks for any help.

  2. #2
    Intern Contributor
    Join Date
    Mar 2004
    Location
    UK, Portsmouth
    Posts
    71

    Re: 2d arrays

    try this link, not sure if it will help havent looked in a long time:

    http://www.iota-six.co.uk/c/f7_dynam...allocation.asp

  3. #3
    Junior Member Regular Contributor
    Join Date
    Apr 2002
    Location
    kremnica, slovakia, europe, earth, sol :-)
    Posts
    102

    Re: 2d arrays

    for example you want array of floats

    Code :
    int arrayheight = 666;
    int arraywidth = 5;
    int i;
    float **array;
     
    array = (float**)malloc(sizeof(float*) * arrayheight);
    if(*array == NULL) {
      fprintf(stderr,"not enought memory! buy a better machine!\n");
      exit(1);
    }
     
    for(i=0;i<arrayheight;i++) {
      array[i] = (float*)malloc(sizeof(float) * arraywidth);
      //TODO: check if malloc was succesful;-)
    }
     
    /* ...do something with array... */
     
    for(i=0;i<arrayheight;i++)
      free(array[i]);
     
    free(array);
     
    /* we are done!!! let's buy another beer :)*/
    should work...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •