# Dynamic Allocation

This is a discussion on Dynamic Allocation within the C Programming forums, part of the General Programming Boards category; I am trying to find the best and most efficient way to do this. What I am doing will be ...

1. ## 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. 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 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;
}

return 1;
}```

3. > 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;
>	}```
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.

4. 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. 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!