I need to create binary data packets in C code. However, I am missing some fundamental point. Currently, there are already structs defined and I will need to use them (at least for reading packet data). The same structure will be used both for received packets and for sent packets. Here are packet structs:
typedef struct {
uint8_t packetType;
uint16_t packetBody;
} MyStruct;
and another struct:
typedef union {
const uint8_t *bytes;
MyStruct *packet;
} MyPacket;
Here's the function that should take a pointer to the struct, fill it with data and return back:
void packetWithBytes(MyPacket *packet)
{
packet->packet->packetType = 1; // crashed with EXC_BAD_ACCESS
packet->packet->packetBody = 3;
}
Here's a call to my function that should return a pointer to binary data:
MyPacket *packetRef;
packetWithBytes(packetRef);
NSData *data = [NSData dataWithBytes:packetRef->bytes length:sizeof(packetRef->bytes)];
I feel I should allocate some space somewhere (with malloc) but not sure where and how to do that. The calling part code should not know any details about the size of the packet. Tried to do allocation but getting the same error:
void packetWithBytes(MyPacket *packet)
{
packet->bytes = malloc(sizeof(uint8_t) + sizeof(uint16_t)); //crash
packet->packet->packetType = 1;
packet->packet->packetBody = 3;
}
Tried to remove const from MyStruct member bytes but getting the same error. I'm not not very familiar with C, so help would be very appreciated.
structmembers is at best platform dependent if not compiler specific. - Simon Richter