# pset4 malloc() approach. Help with size of buffer

I'm having some trouble with the malloc function. I want to make a buffer the size of the new row I'm going to write in the resized file, and this is my approach.

I must say that bi.biWidth is already updated and specifies the value of the new row succesfully.

``````RGBTRIPLE* row = malloc(bi.biWidth*sizeof(RGBTRIPLE)); //Allocate memory the size of a bmp row excluding padding

//For debugging purposes
int sizeRow = sizeof(row);
printf("%d\n", sizeRow);
int sizeCalc= bi.biWidth*sizeof(RGBTRIPLE);
printf("%d\n", sizeCalc);
``````

if for example I want to resize smiley.bmp by a factor of 2, the results I get are:

sizeRow = 8 bytes.

sizeCalc = 48 bytes.

the calculation does output the correct number of bytes my row should have (without padding), but the size of the buffer is completely different.

I'm pretty sure is because I'm not implementing the malloc() function well, but I can't seem to find the problem myself. Any help?

It has nothing to do with the malloc call itself. The problem lies in what you think you are checking. Look at the following:

``````RGBTRIPLE* row = ...
int sizeRow = sizeof(row);
``````

`row` is declared as a pointer. sizeRow contains the size of the pointer `row`, not the actual size of the space allocated by the malloc that `row` points at. A good analogy is this: My hand points at a house, how big is the pointer, my hand?

To convince yourself, try changing the malloc to `malloc(1024)`. It should produce the same result of 8. To further convince yourself, think about this. A pointer in a 64 bit architecture actually has 64 bits, or 8 bytes * 8 bits per byte.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

• Thank you! It does explain why I was getting that size for row. Just to make sure then, am I allocating the space correctly? I tried seeing what happens with row using dgb and it appears to store only one RGBTRIPLE.. whenever I try to store more, it overwrites the las information. I'm not near my pc so I don't have easy access to the code, but once I can I will post here the lines of code that are causing the problem. May 19, 2016 at 13:21
• Silly me, I thought something was wrong by checking through debugger... I just didn'te understand what the debugger shows with a pointer. Everything is fine now, and you solved my question. Thanks! May 28, 2016 at 20:10