Thread: Dynamically allocating 3D array

  1. #1
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732

    Dynamically allocating 3D array

    Hi, Am I doing this right? the following code is suppose to allocate memory of type GLubyte which used to hold all the values which are read from ppm file. But for some reason the image donst get displayed i suspect that I am not allocating the memory properly. Just to clarify is there any problem with the way I am allocating 3D array. Any comments on this code would be appreciated as well

    Code:
    int AllocateMemory( void )
    {
        unsigned int Count, Count1;
        
        if( ( IndiaMapPPM = malloc( sizeof(GLubyte **) * Width ) ) != NULL )
        {
            for( Count = 0; Count < Width; Count++ )
            {
                 if( ( IndiaMapPPM[Count] = malloc( sizeof( GLubyte *) * Height ) ) != NULL )
                 {
                     for( Count1 = 0; Count1 < Height; Count1++ )
                     {
                          if( ( IndiaMapPPM[Count][Count1] = malloc( sizeof(GLubyte) * RGB ) ) == NULL)
                          {
                              printf("Error: Malloc failed\n");
                              return 1;
                          }
                     }
                 }
                 else
                 { 
                     printf("Error: Malloc failed\n");
                     return 1;
                 }
            }
        }
        else
        {
            printf("Error: Malloc failed\n");
            return 1;
        }
        return 0;
    }
    Thank you

    ssharish

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    There's a global GLubyte ***IndiaMapPPM and success is when AllocateMemory() returns 0?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Hi Dave, yes there is a globla variable GLubyte ***IndiaMapPPM and yes it return 0 when it is success.

    Is it something to do with the GLubyte datatype. I suppose there is no difference between a char and GLubyte. And the following is a snap fo code where it show the way i am readin value from ppm file.

    Code:
     for( RowIndex = 0; RowIndex < Row; RowIndex++ )
              for( ColumnIndex = 0; ColumnIndex < Column; ColumnIndex++ )
                   fscanf( ppmFile, "&#37;d%d%d", &IndiaMapPPM[RowIndex][ColumnIndex][0],
                                              &IndiaMapPPM[RowIndex][ColumnIndex][1],
                                              &IndiaMapPPM[RowIndex][ColumnIndex][2] );
    Assuming that i have read the width and height of the ppm file. And

    Code:
    glDrawPixels( Width, Height, GL_RGB, GL_UNSIGNED_BYTE, IndiaMapPPM );
    ssharish
    Last edited by ssharish2005; 02-25-2008 at 10:46 PM.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    There doesn't appear (to me, late at night) anything wrong here; although we're just allocating memory here ... there's many more chances to get it wrong elsewhere.

    As noted, make sure you're not hiding this global variable with a local one elsewhere, that you fill it in at some point, etc.

    I hope you're consistently using IndiaMapPPM[h][w][r] as a GLubyte. You shouldn't try to guess unless you have a good reason.

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    fscanf( ppmFile, "&#37;d%d%d",

    hmmm - what is the format of the file?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Hi Vart, here is a snap of my ppm file.

    Code:
    P3
    # LEAD Tech.
    628 658
    255
    255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 
    255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 
    255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 
    255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 
    255 255 255 255 255 255 255 255 255 255 255 255 255 255....
    Blue - States, what type of file
    DarkOrange - Information which is been commented out
    Magenta - Row Column
    Lime - max color

    The interesting part here is, when I delcare the array straight away like

    Code:
    GLubyte IndiaMapPPM[628][658][3];
    It just works fine. But when I allocate memory dynamically it gives me problem. If there was some problem with memory allocation or not allocated enough memory, i should have got sef fault when reading value into array. I font even get that. When I draw the pixel values, it just drawa call black, which i pretty wired. But this donst happen when I array is statically declared.

    Here is some more bit of code of my PPMread function.

    Code:
    void ReadPPMFile( void )
    {
         FILE *ppmFile;
         unsigned char Buffer[BUFSIZ];
         unsigned int Row, Column, MaxColor;
         int RowIndex, ColumnIndex, Flag = 0;
         
         if( ( ppmFile = fopen( PPMFILENAME, "r") ) == NULL )
         {
             printf("Error: PPM file cannot be opened\n");
             getchar();
             exit( 1 );
         }
         
         if( fgets( Buffer, sizeof(Buffer), ppmFile ) != NULL )
         {
             if( strncmp( Buffer, "P3", 3 ) == 0)
             {
                 printf("Error: File is not an ppm format\n");
                 getchar();
                 exit( 1 );
             }
         }
         else
         {
             printf("Error: File is empty\n");
             getchar();
             exit ( 1 );
         }
         
         while( fgets( Buffer, sizeof( Buffer ), ppmFile) != NULL )
         {
             if( Buffer[0] == '#' )
                 continue;
             
             if( sscanf( Buffer, "&#37;d %d %d", &Row, &Column, &MaxColor ) != 3 )
             {
                 if( sscanf( Buffer, "%d %d", &Row, &Column ) != 2 )
                 {
                     printf("Error: Reading row and column failed\n");
                     getchar();
                     exit( 1 );
                 }
                 else
                 {
                     if( fgets( Buffer, sizeof( Buffer ), ppmFile) != NULL )
                     {
                         if( sscanf( Buffer, "%d", &MaxColor) != 1)
                         {
                             printf("Error: reading max color failed\n");
                             getchar();
                             exit( 1 );
                         }
                         break;
                     }
                 }
             }
             else
                 break;
         }
         
         Width = Row;
         Height = Column;
         
         /* I have just commented this part since it wasn't working. */
         /* if( AllocateMemory() )
             exit(1);          */
    
         for( RowIndex = 0; RowIndex < Row; RowIndex++ )
              for( ColumnIndex = 0; ColumnIndex < Column; ColumnIndex++ )
                   fscanf( ppmFile, "%d%d%d", &IndiaMapPPM[RowIndex][ColumnIndex][0],
                                              &IndiaMapPPM[RowIndex][ColumnIndex][1],
                                              &IndiaMapPPM[RowIndex][ColumnIndex][2] );
         
         fclose( ppmFile );
    }
    Thank you

    ssharish

  7. #7
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    it seems - you are messing to much with globals
    you allocation function insted of to receive dimentions as parameters uses some Width and Height values

    wile your function reading from file initializes Row and Column values.

    Stop messing with globals where you can just pass parameters to function
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  8. #8
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    May be, I will try with sending parameter and see if that one works .

    Thank you

    ssharish

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Note -- you don't always get segmentation faults when you read or write memory you shouldn't be meddling with. It could be your memory; or it could be within the page (is that the right term?) that your program's memory lives in -- most operating systems cannot check the bounds of your accessing exactly. I think I saw 4MB as a value for Windows. In other words, depending on where your memory started, you might have to overwrite as much as 4MB beyond the end of an array before Windows will notice.

    For perfect bounds checking, I recommend Valgrind -- it's really good. Unfortunately, it only runs on Linux, so if you're using Windows, you'll have to find something else. There are lots of these sorts of tools around, however. http://en.wikipedia.org/wiki/Memory_debugger

    I think it's possible that you have some sort of buffer overrun elsewhere . . . .

    Code:
             if( Buffer[0] == '#' )
                 continue;
    Suggestion:
    Code:
    for(p = Buffer; isspace(*p); p ++);
    if(*p == '#') continue;
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. Build an array of strings dynamically
    By Nazgulled in forum C Programming
    Replies: 29
    Last Post: 04-07-2007, 09:35 PM
  3. How To Declare and Dynamically Size a Global 2D Array?
    By groberts1980 in forum C Programming
    Replies: 26
    Last Post: 11-15-2006, 09:07 AM
  4. Initializing a 3D array w/o a loop.
    By funkydude9 in forum C++ Programming
    Replies: 5
    Last Post: 03-04-2004, 11:20 PM
  5. Replies: 4
    Last Post: 09-12-2001, 02:05 PM