Search code examples
cmemorydynamicallocationrealloc

Structures with dynamic memory allocation in C


I need to write data into a structure where the length of the data depends on the command I want to send to a device. For that I have defined the following structure:

typedef struct {
    uint8 len;          // Command length (cmd ... crc)
    uint8 cmd;          // Command code
    uint8 data_length;  // Data length
    uint8 data[12];     // Data: max 12 Byte
    uint8 crc_h;        // CRC value MSB
    uint8 crc_l;        // CRC value LSB
}CMD_TYPE;

Note: the members cmd, *data_length* and crc that are always present, instead member data can be empty or contains up to 12 Bytes.

I have created a function that returns a initialized command according to the parameters passed to the function:

CMD_TYPE Device::get_cmd(uint8 cmd, uint8 data_len, uint8 *data)
{
    CMD_TYPE cmd;

    cmd.len = (4 + data_len) * sizeof(uint8);
    cmd.cmd = cmd;
    cmd.data_length = data_len;
    cmd.data = (uint8 *)realloc(cmd.data, data_len*sizeof(uint8));
    if(data_len > 0)    memcpy(cmd.data, data, data_len);

    add_crc16((uint8*)&cmd);

    return cmd;
}

The function get_cmd() is used like this:

uint8 cmd_code = 0x01;
uint8 data[2] = {0xAB, 0xCD};

CMD_TYPE cmd = local_device->get_cmd(cmd_code, 2, data);
retVal = local_device->send(cmd);

When I try to compile this code I get an error from the compiler for that line:

cmd.data = (uint8 *)realloc(cmd.data, data_len*sizeof(uint8));

and the compiler error is:

error: lvalue required as left operand of assignment

The aim of using realloc() is to re-size the array data or to remove it at all from my new command structure. What is wrong in my code? Is that the right way to initialize structures with dynamic memory allocation?


Solution

  • What you want is the infamous struct hack:

    typedef struct
    {
        uint8   len;          // Command length (cmd ... crc)
        uint8   cmd;          // Command code
        uint8   data_length;  // Data length
        uint8   crc_h;        // CRC value MSB
        uint8   crc_l;        // CRC value LSB
        uint8   data[1];      // Data: max 12 Byte
    } CMD_TYPE;
    

    The trick is to allocate enough room for all of the members of the struct up to data[], then add enough bytes for the data[] member:

    CMD_TYPE * allocCmd(int dataSize)
    {
        int         len;
        CMD_TYPE *  p;
    
        len = sizeof(CMD_TYPE) + (dataSize-1)*sizeof(uint8);
        p = (CMD_TYPE *) malloc(len);
        memset(p, 0, len);
        p->data_length = dataSize;
        return p;
    }
    

    Here, len is calculated to be the size of the struct, minus the size of the empty data member, plus however many elements dataSize specifies for the data array.

    The catch is that you have to be careful never to access any elements of p->data[] beyond what is actually allocated in it (inside the struct).