在结构变长数组 [英] Variable length arrays in struct

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

问题描述

我在C语言编写的应用程序(作为一个初学者),我与得到一个包含可变长度数组结构内损坏的数据挣扎。我发现在cprogramming.com论坛帖子描述的类似问题,也cert.og /安全编码。我想我已经找到了正确的解决方案,但似乎并非如此。

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,名称,SW1,SW2,大小和数据字段时。而且它是变得越来越腐败的数据字段。我为这样的结构分配内存;

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

但是,当I(pretty)打印所得CMD,该数据字段的中间似乎是随机的垃圾。我使用gdb通过加强和可以看到正确的数据是越来越加载到该领域。看来,只有当命令被传递到它被损坏不同的功能。这code被称为函数内部,如;

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)

而pretty打印发生在另一个函数;

And the pretty-print happens in another function;

void pretty_print(char* line, command* cmd)

我原以为我是做正确的事情,但显然不是。至于我可以告诉大家,我构建了结构没事的其他实例(我重复这一个这些方法),但它们不包含在他们和他们的pretty印痕任何可变长度数组看起来很好 - 这担忧我,因为他们也可能被打破,但破损就不太明显了。

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.

我正在写其实就是一个解析器,所以命令被传递到解析功能(它描述了当前的状态,给予提示,解析器预期接下来会),并下一个命令(从输入行衍生返回)。 背景是免费的-D的解析函数,它被退回的新命令的结尾 - 这将被传递回解析与输入的下一个行

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?

非常感谢。

推荐答案

您必须单独分配递减和数据。

You have to allocate desc and data separately.

当你分配你的结构命令* CMD,内存分配给您的DECS和数据的指针。说明和数据需要单独malloced。

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

然后分配内存的数据或递减结果
示例说明:

then allocate memory for data or desc
example for desc:

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

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

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