views:

1376

answers:

5

I have this array:

unsigned char* data = CGBitmapContextGetData(cgctx);

then I tried to get the size with sizeof(data), but that will return me a nonsense-value of 4. data holds a big amount of information. That can't be just 4 ;)

I even get information at data[8293] ... so ... not 4 elements at all.

+4  A: 

The value of 4 is not nonsense, it's the size of the pointer you asked for. There is no way in C of finding out then size of the array that a pointer points to.

anon
A: 

You're confusing a pointer and the object it points to.

Stefan Thyberg
+7  A: 

sizeof returns 4 because your variable is declared as a pointer, as opposed to a C array (char data[1024]).

To get the size you need to use CGBitmapContextGetHeight and CGBitmapContextGetWidth.

pgb
You need more than those two functions. See Mykola's answer.
Jason Coco
+1  A: 

You're taking the size of the pointer, not of the array. Using sizeof() for arrays only work if you actually have an array to measure ;)

Christoffer
+6  A: 

Try to use

CGBitmapContextGetBitsPerPixel
CGBitmapContextGetHeight
CGBitmapContextGetWidth

or

CGBitmapContextGetBytesPerRow
CGBitmapContextGetHeight
Mykola Golubyev
The latter is the better one; depending on the color space, alignment, etc, there may be extra unused bytes at the end of each row. I've been bitten by this before (albeit many years ago, playing with Linux framebuffer direct rendering).
Jim Dovey