Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to read the bytes from a BMP file, and allocate them into an unsigned char array to use it as a texture in an Open GL application.

So far, this is how I define my array:

C++
unsigned char* data = new unsigned char [imageSize];


being imageSize an unsigned int that defines the size of the bitmap, in bytes.

The problem is that when I run the application, data has a length of 4 bytes, instead of the value of imageSize. I made a log to track what the program does, and this is what it gives to me:

LOG: imageSize : 786434

LOG: image width : 512

LOG: image height : 512

ERROR: Failed to allocate Data Buffer (bytes) : 4

ERROR: Required bytes for the buffer : 786434


In other words, data should be a 786434 bytes long array. Instead, I get a 4 bytes long array.

I also tried doing many things like

C++
unsigned char* data;
data = new unsigned char [imageSize];


Also defining it as

C++
data = (unsigned char*)malloc(imageSize);


But none of them work for some reason. Even if I assign to it the length manually, it won't work.

I'm programming this in Visual Studio 2010, for Windows, and it's a 32 bit application.
Posted
Updated 28-May-14 6:38am
v2
Comments
[no name] 28-May-14 12:53pm    
Please post the rest of your code.
Member 10272815 28-May-14 12:54pm    
How (and where) are you setting imageSize?
Richard MacCutchan 28-May-14 14:40pm    
Please show all the code for this function, including the lines where you print the above values. You are obviously doing something wrong, but we cannot guess what.

1 solution

No, 4 bytes is the size of the pointer to data, and actual data has the size of imageSize. You allocated the array on the heap correctly. (Better don't use malloc with C++, use new.) The problem is in the method where you pass this data. Don't look at the log in this case, look at the debug window called "Call stack".

What to do? 1) Learn the API you are using with this data, just read the documentation. 2) Learn C++ pointers and arrays properly, don't be so blind-folded.

—SA
 
Share this answer
 
v2
Comments
[no name] 28-May-14 12:59pm    
> You allocate the array correctly, in one case on stack, in another case on the heap.

I did not see Magnus' code where array is allocated on stack.
Sergey Alexandrovich Kryukov 28-May-14 17:44pm    
Oh, I see. It was just my fantasy :-). Actually the pointer is on stack, the pointed object is created on heap.
Thank you, I'll fix the answer.
—SA
CPallini 28-May-14 13:13pm    
Of course. My 5.
Sergey Alexandrovich Kryukov 28-May-14 17:44pm    
Thank you, Carlo.
—SA
Magnus9998 28-May-14 14:29pm    
Or we could say "sizeof()" was giving me the size of the pointer, which is 4 bytes indeed, instead of the size of the array, that I had stored in imageSize. In other words, I solved the problem by using imageSize instead of sizeof(data).

Also, about your two points...

1) That's what I have been doing from the very beggining. Reading the API documentation for OpenGL. However, it has nothing to do here, since this was a C++ problem. Not an Open GL one. And no, other than the standard Windows API and Open GL, I am not using anything else.

2) Making mistakes implies learning. One is not blind-folded for doing something he doesn't know. One is blind-folded if he doesn't even try, since he will never know how to do it. Sure I did learn that sizeof() only gives you the size of the type you're using. Not the size of an array.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900