C 中具有动态内存分配的结构
我需要将数据写入一个结构,其中数据的长度取决于我要发送到设备的命令。为此,我定义了以下结构:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您想要的是臭名昭著的 struct hack:
技巧是为结构体的所有成员分配足够的空间,直到
data[]
,然后为data[]添加足够的字节
成员:在这里,
len
计算为结构体的大小,减去空data
成员的大小,再加上dataSize
为dataSize
指定的元素数量。代码>数据数组。问题是您必须小心永远访问
p->data[]
中实际分配的元素(结构内部)之外的任何元素。What you want is the infamous struct hack:
The trick is to allocate enough room for all of the members of the struct up to
data[]
, then add enough bytes for thedata[]
member:Here,
len
is calculated to be the size of the struct, minus the size of the emptydata
member, plus however many elementsdataSize
specifies for thedata
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).您的
CMD_TYPE.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:Just don't forget to initialize it with
malloc()
(or by setting it to zero beforerealloc()
) and to clean up after yourselfBy the way, do not cast the result of
malloc()
and co.定义为 [..] 的数组是不可变的,你不能给它们分配任何东西。相反,您应该使用指针。
array defined as a[..] are immutable, you can't assign anything to them. Instead you should use pointers.