inconsistent output of the function sizeof

Hi guys!

I’m new to openframeworks, so in advance please apologize if my question is totally trivial - as it probably is.

Anyway, here’s a code snippet that’s causing me trouble:

in the h file:

  
unsigned char*	depthMatrix;  

in the cpp file:

  
	depthMatrix = new unsigned char[ofGetWidth()*ofGetHeight()];  
	printf("defined size: %d, init size: %d\n", ofGetWidth()*ofGetHeight(), sizeof(depthMatrix));  

and the marvellous output:

defined size: 786432, init size: 4

Does anyone know why this array gets defined to a length of 4, while I’m specifically defining it to over 700000??

Thanks in advance for any pointers!

Gavrilo

Hey Gavrilo,

the sizeof function will give you the number of bytes that the type of variable that you pass in takes, *not* the size of an array. So, for instance:

sizeof(bool) = 1
sizeof(float) = 4
sizeof(int) = 4
sizeof(double) = 8
sizeof(char*) = 8 // signed or unsigned – doesn’t matter. it’s the size of a pointer

AFAIK, there is no way to get the size of an array once you have allocated it. If you absolutely need to have a way to get the size of a container and can’t save the original allocation size, use a vector.

http://www.cplusplus.com/reference/stl/vector/vector/