I did extensive research before asking this question here, but I couldn't find an answer.
I have a simple struct that contains an array of another struct and the total:
typedef struct {
int total;
struct Struct1 arrayOfStructs[];
} Struct2;
Struct2 _myVar; // the variable I need
arrayOfStructs[]should be a C array containing structs of typeStruct1(this syntax is not valid, because you need a constant value)totalis the count of thearrayOfStructs- (
Struct1is not relevant, it just contains some primitive data)
The problem is that I want this array to be dynamic. I want to be able to do this:
for (int i = 0; i < _myVar.total; i++) {
NSLog(@"%d", _myVar.arrayOfStructs[i].index);
}
I tried the following:
typedef struct {
int total;
struct Struct1 *arrayOfStructs; // USING A POINTER HERE
} Struct2;
Struct2 _myVar; // the variable I need
and for adding values:
int total = ++_myVar.total; // increase total count
_myVar.arrayOfStructs = realloc(_myVar.arrayOfStructs, total * sizeof(Struct1));
_myVar.arrayOfStructs[total - 1] = {1, 5.3}; // appending a new value in the array
The above code adds the value correctly, but after passing it into NSData and retrieving it, the array items are lost, and I end up with some bad pointers:
[NSData dataWithBytes:&_myVar length:sizeof(_myVar) + sizeof(Struct1) * total];
I suspect this is because I need to do sizeof(_myVar) + sizeof(Struct1) * total instead of just sizeof(_myVar), so because of the pointer addresses that are different I end up with bad data. I think I'm not copying the data correctly into my structure.
Any help would be greatly appreciated!
struct Struct1 *arrayOfStructs;, you don't know where the payload resides. You can be fairly sure that it won't be directly after theStruct2 _myVar;. So if[NSData dataWithBytes:&_myVar length:sizeof(_myVar) + sizeof(Struct1) * total];takessizeof _myVar + total*sizeof(Struct1)bytes starting from&_myVar; that will take anint, a pointer, and a lot of garbage you have no right to.