I'm writing a Mac app as my hobby, and I'm trying to understand a few basic things about pointers (I use objective-C). I couldn't find answers on the web.
I need to parse binary files that are structured like this:
I have defined the 28-bit structure in question like so:
I ingest a file into some NSData object named "fileData", then I do:
This works, but what garanties that the second instruction will never cause a segmentation fault? I mean, the content of fileData is not guaranteed to constitute a continuous block of memory, right? So what if bytes 206 to 233 are not in the same memory block?
Is this a valid concern, am I doing things that may cause crashes under certain conditions?
Note: I know that there are alternative (for instance using NSData's getBytes).
Thanks.
I need to parse binary files that are structured like this:
- bytes 0 to 3: some chars
- bytes 4 and 5: a 16-bit short
- ...
- bytes 206 to 233: a 28-byte structure
- bytes 234 to 261: another 28-byte structure
- ...
I have defined the 28-bit structure in question like so:
Code:
typedef struct DirEntry {
... # some shorts and ints
} DirEntry;
I ingest a file into some NSData object named "fileData", then I do:
Code:
const char *fileBytes = fileData.bytes;
DirEntry someEntry = *(const DirEntry *)(fileBytes + 206);
This works, but what garanties that the second instruction will never cause a segmentation fault? I mean, the content of fileData is not guaranteed to constitute a continuous block of memory, right? So what if bytes 206 to 233 are not in the same memory block?
Is this a valid concern, am I doing things that may cause crashes under certain conditions?
Note: I know that there are alternative (for instance using NSData's getBytes).
Thanks.