Thread: malloc issues

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    22

    malloc issues

    Hi all,

    Got a problem with this code allocating memory. In the "//read the data" part of the code, the image->data =(char*)malloc(size) refuses to allocate memory. is this a problem with my code or is this a problem with something else? I have been trying for ages to get this sorted.
    Any one got any suggestions? I would be most greatful!



    Code:
    struct Image {
        unsigned long sizeX;
        unsigned long sizeY;
        unsigned char *data;
    };
    typedef struct Image Image;
    
    int ImageLoad(char *filename, Image *image) {
        FILE *file;
        unsigned long size;                 // size of the image in bytes.
        unsigned long i;                    // standard counter.
        unsigned short int planes;          // number of planes in image (must be 1) 
        unsigned short int bpp;             // number of bits per pixel (must be 24)
        char temp;                          // temporary color storage for bgr-rgb conversion.
    
        // make sure the file is there.
        if ((file = fopen(filename, "rb"))==NULL)
        {
    	printf("File Not Found : %s\n",filename);
    	return 0;
        }
        
        // seek through the bmp header, up to the width/height:
        fseek(file, 18, SEEK_CUR);
    
        // read the width
        if ((i = fread(&image->sizeX, 4, 1, file)) != 1) {
    	printf("Error reading width from %s.\n", filename);
    	return 0;
        }
        printf("Width of %s: %lu\n", filename, image->sizeX);
        
        // read the height 
        if ((i = fread(&image->sizeY, 4, 1, file)) != 1) {
    	printf("Error reading height from %s.\n", filename);
    	return 0;
        }
        printf("Height of %s: %lu\n", filename, image->sizeY);
        
        // calculate the size (assuming 24 bits or 3 bytes per pixel).
        size = image->sizeX * image->sizeY * 3;
        printf("\nsize: %d", size);
    
        // read the planes
    
        if ((fread(&planes, 2, 1, file)) != 1) {
    	printf("Error reading planes from %s.\n", filename);
    	return 0;
        }
            	    			printf("\nplanes:%d", planes);
        if (planes != 1) {
    	printf("Planes from %s is not 1: %u\n", filename, planes);
    	return 0;
        }
    
        // read the bpp
        			
        if ((i = fread(&bpp, 2, 1, file)) != 1) {
    	printf("Error reading bpp from %s.\n", filename);
    	return 0;
    
        }
        printf("\nbpp:%d\n", bpp);
        if (bpp != 24) {
    	printf("Bpp from %s is not 24: %u\n", filename, bpp);
    	return 0;
        }
    	
        // seek past the rest of the bitmap header.
        fseek(file, 24, SEEK_CUR);
    
        //read the data. 
        	printf("\nimage1: %d\n", image->data);
        	
        	image->data = (char *) malloc(size);
        if (image->data == NULL) {
    	printf("Error allocating memory for color-corrected image data\n");
    	return 0;	
        }
    
        if ((i = fread(image->data, size, 1, file)) != 1) {
    	printf("Error reading image data from %s.\n", filename);
    	return 0;
        }
    
        for (i=0;i<size;i+=3) { // reverse all of the colors. (bgr -> rgb)
    	temp = image->data[i];
    	image->data[i] = image->data[i+2];
    	image->data[i+2] = temp;
        }
        
        return 1;

  2. #2
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    how would u say that the memory is not been allocated. Does malloc returns NULL. How many bytes are u allocating?

    ssharish
    Last edited by ssharish2005; 11-11-2007 at 01:21 PM.

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    22
    ye malloc is returning null but i cant work out why. Im trying to allocate the same number of bits as the size of the image data. IE the size of the file from the current position in the file to the end of the image data.

    To put it another way, the size of the bitmap file minus the file header and file info header. Just the size of the pixel data in the image.

    Hope that helps

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Print the "size" variable when it fails to allocated. My suspicion would be that it's "wrong".

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Nov 2007
    Posts
    22
    Code:
    Width of treados2.bmp: 256
    Height of treados2.bmp: 219043332352
    
    size: 196608
    planes:1
    bpp:24
    
    image1: 0
    thats the output from the code. Strangly now i run the code and i get the odd result for the height of the image too. The width is correct. the image is a 256x256 bitmap called treados2.bmp. the file size if i d a right click properties is 192.1kb so im guessing size is right?

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    are you by any chance running this on a 64-bit system?

    The "height" is 0x3300000100 - which is 0x33 in the upper 32 bits of a 64-bit word - a tad bigger than I'd expect it to be. The lower part is 256, which is "sensible" for a bitmap image [although I have of course no idea what the actual size really is].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    The height is a signed quantity, not unsigned.

    Normally, the height is positive, and the bitmap loads from bottom to top.
    If the height is negative, the bitmap is loaded from top to bottom.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Salem View Post
    The height is a signed quantity, not unsigned.

    Normally, the height is positive, and the bitmap loads from bottom to top.
    If the height is negative, the bitmap is loaded from top to bottom.
    Yes, but if you take that number and insert it into the Windows calculator, it turns into 0x33_0000_0100 [my underscores to show groups of 4 digits - it's hard to tell how many digits are in a really large number]. So it's not a negative number. And even it it was a negative number [e.g. it was a few digits shorter] it would actually need to be quite a HUGE negative number. A 32-bit "small" negative number starts with a 4... [Yes, I thought it was a negative number first too]

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Also, if we assume that I'm correct in saying that "unsigned long" is a 64-bit quantity, then reading a 4-byte word would only set half the "long". You'd need to sign-extend it to make it a 64-bit integer [as it can be negative].

    It would probably be better to use a "sized type", such as a "INT32" or some other "user defined type" that can be adjusted to the right size according to what compiler is being used.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Registered User
    Join Date
    Nov 2007
    Posts
    22

    Talking

    Ok lots of talk there thats a little over my head. Thanks for the responses so far. Sounds like u guys know your stuff. Ok ye i am running 64-bit fedora 7 Linux.



    I changed my height type from the unsigned long and changed to a int. And thank god! it worked! I was using that code to load textures in OpenGL. Been trying since Thursday constantly trying to find something wrong with it!

    But before i get too carried away is there anything i should be careful of when changing the code? I cant see there being anymore problems but just incase? ;-)

    Oh god thanks so much for your help!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. malloc + segmentation fault
    By ch4 in forum C Programming
    Replies: 5
    Last Post: 04-07-2009, 03:46 PM
  2. Is there a limit on the number of malloc calls ?
    By krissy in forum Windows Programming
    Replies: 3
    Last Post: 03-19-2006, 12:26 PM
  3. Malloc and calloc problem!!
    By xxhimanshu in forum C Programming
    Replies: 19
    Last Post: 08-10-2005, 05:37 AM
  4. malloc() & address allocation
    By santechz in forum C Programming
    Replies: 6
    Last Post: 03-21-2005, 09:08 AM