在循环中的结构中为结构分配内存
Allocating memory for struct within a struct in cycle
我正在为某个项目开发 INI 风格的配置解析器,我遇到了下一个麻烦。
我有 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;
而且我有处理 INI 文件中数据的功能(我在 inih parser): [pasted to pastebin 下工作导致时间太长了)。最后,在 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;
}
结果,二进制文件因内存错误而崩溃。我想(不,我知道),我在 handler() 中错误地分配了内存,但我不明白,我在哪里做错了。我整晚都在试图理解内存分配,我很累,但现在我很有趣,我做错了什么,以及如何强制它正常工作。
P.S。对不起丑陋的英语
问题似乎与结构的重新分配有关:
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));
首先,您不能重新分配主设置结构。由于您的处理程序将始终使用原始 pconfig
值调用,因此 module
和 channel
数组的重新分配没有任何效果,您将访问释放的内存。
另外,在重新分配 module
和 channel
数组时,您应该分配 count + 1
元素,因为 handler
的下一次调用可能会分配给 [count]
插槽。
所以尝试将上面的三行替换为:
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));
我正在为某个项目开发 INI 风格的配置解析器,我遇到了下一个麻烦。 我有 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;
而且我有处理 INI 文件中数据的功能(我在 inih parser): [pasted to pastebin 下工作导致时间太长了)。最后,在 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;
}
结果,二进制文件因内存错误而崩溃。我想(不,我知道),我在 handler() 中错误地分配了内存,但我不明白,我在哪里做错了。我整晚都在试图理解内存分配,我很累,但现在我很有趣,我做错了什么,以及如何强制它正常工作。 P.S。对不起丑陋的英语
问题似乎与结构的重新分配有关:
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));
首先,您不能重新分配主设置结构。由于您的处理程序将始终使用原始 pconfig
值调用,因此 module
和 channel
数组的重新分配没有任何效果,您将访问释放的内存。
另外,在重新分配 module
和 channel
数组时,您应该分配 count + 1
元素,因为 handler
的下一次调用可能会分配给 [count]
插槽。
所以尝试将上面的三行替换为:
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));