October 25, 2024
Chicago 12, Melborne City, USA
C#

How can I define a struct which has a member with unknown size?


I defined a struct following table 17 in PLDM Base spec MultipartReceive response. This struct is used for receiving data from the other component. Following the spec, we can’t know the sizing of data and below it have another uint32_t crc checksum. So how can I define this response struct?

MultipartReceive response

I decided to define it like below and CRC checksum will be a part of data[].

typedef struct {
    uint8_t completion_code;        // completion code
    uint8_t transfer_flag;
    uint32_t next_transfer_handle;
    uint32_t data_length_bytes;
    uint8_t data[];
} __attribute__((packed)) MultipartReceiveResp_T;

But my teammate said I should define this struct like below for safety:

typedef struct {
    uint8_t completion_code;        // completion code
    uint8_t transfer_flag;
    uint32_t next_transfer_handle;
    uint32_t data_length_bytes;
    uint8_t data[1];
} __attribute__((packed)) MultipartReceiveResp_T;

Which option is better? and Why? I am confused about why we must define a single array for this.



You need to sign in to view this answers

Leave feedback about this

  • Quality
  • Price
  • Service

PROS

+
Add Field

CONS

+
Add Field
Choose Image
Choose Video