Assigning address to array from heap
- by Schaltfehler
I want to save the state of my structs as a binary file and load them again.
My structs look like this:
typedef struct
{
uint8_t pointerLength;
uint8_t *pointer;
uint8_t NumBla;
uinT16 Bla[MAX_NUM_Bla];
...
}
BAR_STRUCT, *BAR;
typedef struct
{
int numBar;
BAR bars[MAX_NUM_BAR];
}
FOO_STRUCT, *FOO;
Saving is no problem, but restoring the state.
Iam at the point where the bytestring from the file is on the heap and a pointer is pointing to the first adress of this string.
And I do as follows:
const void* dataPointer //points to adress in heap
unsigned char* bytePointer = (unsigned char*)dataPointer;
FOO foo = (FOO_STRUCT*)bytePointer;
bytePointer += sizeof(FOO_STRUCT);
for (int i=0; i < MAX_NUM_BAR; i++) {
foo->bars[i] = (BAR_STRUCT*)bytePointer;
}
The last assignment doesn't work and I get an EXC_BAD_ACCESS.
Because bars is an array of pointers i need to correct the adresses of each element is pointing to. Because they are not valid anymore. So I try to assign the adress of the object I saved in the bytesteam to foo-bars[i];
But I can not change foo-bars[i] at all. Accessing works but but assigning a new adress doesn't.
I wonder why.