简体   繁体   English

在结构中循环分配结构的内存

[英]Allocating memory for struct within a struct in cycle

I'm working on INI-style configuration parser for some project, and I gets next trouble. 我正在为某些项目使用INI样式的配置解析器,但遇到下一个麻烦。 I have 3 structures: 我有3种结构:

typedef struct {
    const char* name;
    unsigned tract;
    int channel;
    const char* imitation_type;
} module_config;

typedef struct {
    int channel_number;
    int isWorking;
    int frequency;
    int moduleCount;
} channel_config;

typedef struct {
    int mode;
    module_config* module;
    channel_config* channel;
} settings; 

And I have function for handling data in my INI-file (I working under inih parser): [pasted to pastebin cause too long]. 而且我具有处理INI文件中的数据的功能(我在inih解析器下工作):[粘贴到pastebin原因太长]。 Finally, in main(), I did the next: 最后,在main()中,我做了下一个:

settings* main_settings;
main_settings = (settings*)malloc(sizeof(settings));
main_settings->module = (module_config*)malloc(sizeof(module_config));
main_settings->channel = (channel_config*)malloc(sizeof(channel_config));
if (ini_parse("test.ini", handler, &main_settings) < 0) {
    printf("Can't load 'test.ini'\n");
    return 1;
}

In result, binary crashes with memory fault. 结果,二进制崩溃并发生内存故障。 I think (no, I KNOW), what I'm incorrectly allocating the memory in handler(), but I does not understand, where I do it wrong. 我认为(不,我知道),我在handler()中错误地分配了内存,但是我不明白我在哪里做错了。 I spent all night long trying to understand memory allocating, and I'm very tired, but now me simply interestingly, what I'm doing wrong, and HOW to force this working fine. 我花了一整夜的时间试图了解内存分配,但我很累,但现在很有趣的是,我做错了什么,以及如何使此工作正常进行。 PS Sorry for ugly english PS对不起英语不好

The problem seems to be related to the reallocation of your structs: 问题似乎与您的结构的重新分配有关:

pconfig = (settings *) realloc(pconfig, (module_count + channel_count) * sizeof(channel_config));
pconfig->module = (module_config *) realloc(pconfig->module, module_count * sizeof(module_config));
pconfig->channel = (channel_config *) realloc(pconfig->channel, channel_count * sizeof(channel_config));

First of all, you must not reallocate the main settings struct. 首先,您不得重新分配主要设置结构。 Since your handler will always be called with the original pconfig value, the reallocation of the module and channel arrays has no effect, and you'll access freed memory. 由于将始终使用原始pconfig值调用处理程序,因此modulechannel数组的重新分配无效,并且您将访问释放的内存。

Also when reallocating the module and channel arrays you should allocate count + 1 elements, since the next invocation of handler might assign to the [count] slot. 同样,在重新分配modulechannel数组时,应该分配count + 1元素,因为handler的下一次调用可能会分配给[count]插槽。

So try to replace the three lines above with: 因此,尝试将以上三行替换为:

pconfig->module  = (module_config *)  realloc(pconfig->module,  (module_count + 1)  * sizeof(module_config));
pconfig->channel = (channel_config *) realloc(pconfig->channel, (channel_count + 1) * sizeof(channel_config));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM