Thread: Dynamic Allocation

  1. #1
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    Posts
    404

    Dynamic Allocation

    I am trying to find the best and most efficient way to do this. What I am doing will be loading an image, like a bitmap, into memory. I don't know the size of it, so I would like to dynamically allocate the space, no hard coding array sizes.

    From there, I will take the data (i assume probably from a char** buffer?) and will put it into some structs.

    The main thing is, I will be loading the bitmap into memory, and I don't want to use any more space than needed. I will be running some operations on the pixel data and such.

    I am guessing with the use of malloc and a char**, I will be able to make a char array that will be exactly the size of the byte content of the graphic to be imported.

    If you could post either some small snippets, or if you want me to write some pseudocode (not sure if I explained myself well enough), just let me know.

    any help appreciated!

  2. #2
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    Posts
    404
    Well, sorry to double post, but this post contain info different than the first post.

    I ended up figuring a way to do this. Please critique this. I want to be sure I am using things correctly and didn't just end up getting lucky. I do know that later on I will use free() on my pixd struct. By the way, I'm banking on this being a 24 bit bitmap, so if your trying to analyze pixellen, there shouldn't be a reserved byte.

    Code:
    typedef struct PIXELDATA 
    {
    	BYTE*		data; 
    } PIXELDATA; 
    
    struct PIXELDATA		pixd = {0};
    
    int bmp_load()
    {
    	int i;
    	size_t result;
    
    	result = fread(&bmfh, sizeof(bmfh), 1, fp);
    	if (result != 1)
    		return 0;
    
    	result = fread(&bmih, sizeof(bmih), 1, fp);
    	if (result != 1)
    		return 0;
    
    	pixellen = (bmih.biHeight * bmih.biWidth * 3);
    
    	pixd.data = malloc(sizeof(pixd) * pixellen);
    
    	for (i = 0; i < pixellen; i++)
    	{
    		pixd.data[i] = (BYTE)malloc(sizeof(BYTE));
    		pixd.data[i] = (BYTE)NULL;
    		fread(&pixd.data[i], 1, 1, fp);
    	}
    
    	return 1;
    }

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > pixd.data = malloc(sizeof(pixd) * pixellen);
    This would probably work, but to be correct it should be more like:
    pixd.data = malloc(pixellen * sizeof *pixd.data);

    Code:
    >	for (i = 0; i < pixellen; i++)
    >	{
    >		pixd.data[i] = (BYTE)malloc(sizeof(BYTE));
    >		pixd.data[i] = (BYTE)NULL;
    >		fread(&pixd.data[i], 1, 1, fp);
    >	}
    You don't need the malloc() statement in this loop. You've already allocated space for a one dimensional array above. And if you wanted, you could actually dispense with the loop, and simply do:
    Code:
    num_read = fread(&pixd.data, sizeof *pixd.data, pixellen, fp)
    num_read will tell you how many BYTEs were actually read, which should match pixellen.
    Last edited by swoopy; 04-18-2008 at 10:31 PM.

  4. #4
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    I'm not sure what the file structure looks like for a bitmap, so I can't tell you if you're reading the file data properly, but this part looks wrong to me:
    Code:
    pixd.data[i] = (BYTE)malloc(sizeof(BYTE));
    pixd.data[i] = (BYTE)NULL;
    pixd.data is a BYTE*, not a BYTE**. You already malloc() memory for it above the for-loop, so I'm not sure what you're trying to do here, but it's almost certainly wrong.
    Rather than reading the data in 1 byte at a time, why not call fread() once and read all the data in?

  5. #5
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    Posts
    404
    OK, thank you for your replies.

    I understand the logic in a lot of your code, and that is what I had first. Originally I didn't loop through all of the variables. But if I don't, for some reason it only reads 4042 bytes. No idea why. If I printf pixellen for a test, I get the correct value. But when I output num_read, i get 4042. Any ideas why? This only happens when I bulk read, and not byte by byte.

    Edit: Arghh, when i was reading in, I pointed to &pixd.data instead of &pixd.data[0]. Thanks guys. Help much appreciated!
    Last edited by carrotcake1029; 04-18-2008 at 11:42 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. dynamic allocation from 1 instead of zero
    By cfdprogrammer in forum C Programming
    Replies: 27
    Last Post: 04-28-2009, 08:21 AM
  2. pointer to array with dynamic allocation
    By cfdprogrammer in forum C Programming
    Replies: 22
    Last Post: 04-07-2009, 09:56 AM
  3. Difference between straight and dynamic allocation?
    By darsunt in forum C++ Programming
    Replies: 10
    Last Post: 06-04-2008, 05:47 PM
  4. Dynamic memory allocation.
    By HAssan in forum C Programming
    Replies: 3
    Last Post: 09-07-2006, 05:04 PM
  5. Dynamic allocation (I thought it would crash)
    By Baaaah! in forum C Programming
    Replies: 16
    Last Post: 11-30-2005, 05:10 PM