C-array to NSData and back
Posted
by Thor Frølich
on Stack Overflow
See other posts from Stack Overflow
or by Thor Frølich
Published on 2010-03-11T21:08:49Z
Indexed on
2010/03/11
21:09 UTC
Read the original article
Hit count: 464
I'm attempting to save a c-style array of Vertex3D structs to an NSData object and get them back when reloading the app:
NSData *vert = [NSData dataWithBytes:&vertices length:(sizeof(Vertex3D) * NUM_OF_VERTICES)];
This data is then saved and attempted to be read back into my c-array thusly:
vertices = malloc(sizeof(Vertex3D) * NUM_OF_VERTICES);
[vert getBytes:&vertices length:(sizeof(Vertex3D) * NUM_OF_VERTICES)];
The above results in “EXC_BAD_ACCESS” followed by:
malloc: * error for object 0x48423c0: pointer being freed was not allocated
I'm very new to programming so there's probably some fundamental memory management principle I'm unaware of. I have verified that the loaded NSData is identical to the saved one, but it's clear that the transition from c-array to NSData (and back) is not as I intended.
© Stack Overflow or respective owner