结构体中的可变长度数组 [英] Variable length arrays in struct

查看:109
本文介绍了结构体中的可变长度数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用 C 编写一个应用程序(作为初学者),我正在努力解决在包含可变长度数组的结构中获取损坏的数据的问题.我在 cprogramming.com 和 cert.og/secure-coding 上的论坛帖子中发现了类似的问题.我以为我找到了正确的解决方案,但似乎没有.

I'm writing an application in C (as a beginner) and I'm struggling with getting corrupted data inside a struct that contains a variable length array. I found similar issues described in forum posts on cprogramming.com and also on cert.og/secure-coding. I thought I'd had found the right solution, but it seems not.

结构看起来像这样;

typedef struct {
    int a;
    int b;
} pair;

typedef struct {
    CommandType name;
    pair class;
    pair instr;
    pair p1;
    pair p2;
    pair p3;
    CommandType expected_next;
    char* desc;
    int size;
    pair sw1;
    pair sw2;
    pair* data;
} command;

有问题的是命令".对于命令"的任何给定实例(或任何正确的短语),将设置不同的字段,尽管在大多数情况下,尽管在不同的实例中设置了相同的字段.

With the problematic one being "command". For any given instance (or whatever the correct phrase would be) of "command" different fields would be set, although in most cases the same fields are set albeit in different instances.

我遇到的问题是在尝试设置 expected_next、name、sw1、sw2、size 和 data 字段时.数据字段正在损坏.我正在为这样的结构分配内存;

The problem I have is when trying to set the expected_next, name, sw1, sw2, size and data fields. And it's the data field that's getting corrupt. I'm allocating memory for the struct like this;

void *command_malloc(int desc_size,int data_size)
{
    return malloc(sizeof(command) +
                  desc_size*sizeof(char) +
                  data_size*sizeof(pair));
}

command *cmd;
cmd = command_malloc(0, file_size);

但是当我(漂亮地)打印生成的 cmd 时,数据字段的中间似乎是随机垃圾.我已经通过 gdb 逐步完成,可以看到正确的数据正在加载到该字段中.似乎只有当命令被传递给不同的函数时它才会被破坏.这段代码在一个函数内被调用,例如;

But when I (pretty) print the resulting cmd, the middle of the data field appears to be random garbage. I've stepped through with gdb and can see that the correct data is getting loaded into the the field. It appears that it's only when the command gets passed to a different function that it gets corrupted. This code is called inside a function such as;

command* parse(char *line, command *context)

漂亮的打印发生在另一个函数中;

And the pretty-print happens in another function;

void pretty_print(char* line, command* cmd)

我以为我做的事情是正确的,但显然不是.据我所知,我构造了结构的其他实例(我为这个实例复制了这些方法),但它们中不包含任何可变长度的数组,而且它们的漂亮打印看起来不错 - 这让我担心,因为它们也有可能破损,但破损不太明显.

I had thought I was doing things correctly, but apparently not. As far as I can tell, I construct other instances of the struct okay (and I duplicated those approaches for this one) but they don't contain any variable length array in them and their pretty-prints looks fine - which concerns me because they might also be broken, but the breakage is less obvious.

我写的实际上是一个解析器,所以一个命令被传递到解析函数(它描述当前状态,提示解析器接下来会发生什么)和下一个命令(派生自输入行"") 返回.context" 在 parse 函数的末尾是 free-d,新命令将返回该命令 - 然后将其与下一个line"输入一起传递回parse".

What I'm writing is actually a parser, so a command gets passed into the parse function (which describes the current state, giving hints to the parser what to expect next) and the next command (derived from the input "line") is returned. "context" is free-d at the end of the parse function, which the new command getting returned - which would then be passed back into "parse" with the next "line" of input.

任何人都可以就为什么会发生这种情况提出任何建议吗?

Can anyone suggest anything as to why this might be happening?

非常感谢.

推荐答案

必须分别分配desc和data.

You have to allocate desc and data separately.

当您分配结构命令 *cmd 时,会为您的 decs 和数据指针分配内存.Desc 和数据必须分开分配.

When you allocate your struct command *cmd, memory is allocated for your pointers of decs and data. Desc and data have to be malloced separately.

所以分配你的命令

command *cmd =  malloc(sizeof(command));

然后为 data 或 desc 分配内存
描述示例:

then allocate memory for data or desc
example for desc:

cmd->desc = malloc( sizeof(char )*100);

这篇关于结构体中的可变长度数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆