C 中具有动态内存分配的结构

发布于 2024-12-03 19:16:26 字数 1421 浏览 0 评论 0原文

我需要将数据写入一个结构,其中数据的长度取决于我要发送到设备的命令。为此,我定义了以下结构:

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;

注意:成员cmd、*data_length*和crc始终存在,而不是成员 >data 可以为空或最多包含 12 个字节。

我创建了一个函数,它根据传递给函数的参数返回初始化的命令:

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;
}

函数 get_cmd() 的使用方式如下:

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);

当我尝试编译此代码时,我从编译器中收到该行的错误:

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

编译器错误是:

error: lvalue required as left operand of assignment

使用realloc()的目的是重新调整数组数据的大小或将其从我的新命令结构中删除。我的代码有什么问题?这是使用动态内存分配初始化结构的正确方法吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

停滞 2024-12-10 19:16:26

您想要的是臭名昭著的 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;

技巧是为结构体的所有成员分配足够的空间,直到data[],然后为data[]添加足够的字节 成员:

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;
}

在这里, len 计算为结构体的大小,减去空 data 成员的大小,再加上 dataSizedataSize 指定的元素数量。代码>数据数组。

问题是您必须小心永远访问p->data[]中实际分配的元素(结构内部)之外的任何元素。

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).

别念他 2024-12-10 19:16:26

您的CMD_TYPE.data是一个数组,而不是指针。由于您希望它跟踪动态分配的内存,因此它必须是一个指针:

uint8_t * data;

只是不要忘记使用 malloc() 对其进行初始化(或者在 realloc() 之前将其设置为零))并自行清理

顺便说一下,不要强制转换 malloc() 和 co 的结果。

Your CMD_TYPE.data is an array, not a pointer. Since you want it to track dynamically allocated memory, it has to be a pointer:

uint8_t * data;

Just don't forget to initialize it with malloc() (or by setting it to zero before realloc()) and to clean up after yourself

By the way, do not cast the result of malloc() and co.

初懵 2024-12-10 19:16:26

定义为 [..] 的数组是不可变的,你不能给它们分配任何东西。相反,您应该使用指针。

array defined as a[..] are immutable, you can't assign anything to them. Instead you should use pointers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文