[英]Realloc and uninitialized variables (valgrind)
For the life of me, I can't figure out why Valgrind reports the following warnings: 对于我一生,我无法弄清楚为什么Valgrind报告以下警告:
==4988== Use of uninitialised value of size 8
==4988== at 0x4E62C3F: set_library (mainroutines.c:67)
==4988== by 0x400E81: main (in /media/src/bin/driver)
==4988== Uninitialised value was created by a heap allocation
==4988== at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==4988== by 0x4E6301F: create_input_data (supportroutines.c:43)
==4988== by 0x400DAA: main (in /media/src/bin/driver)
==4988==
==4988== Use of uninitialised value of size 8
==4988== at 0x4E62C61: set_library (mainroutines.c:68)
==4988== by 0x400E81: main (in /media/src/bin/driver)
==4988== Uninitialised value was created by a heap allocation
==4988== at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==4988== by 0x4E6301F: create_input_data (supportroutines.c:43)
==4988== by 0x400DAA: main (in /media/src/bin/driver)
==4988==
Meanwhile, the set_library
function appears like so: 同时, set_library
函数如下所示:
void set_library (Foo* in_data, const int nrows, char* in_string) {
const int str_length = strlen(in_string);
char** new_string_array = NULL;
if (in_data->stored_string==NULL) {
in_data->stored_string = malloc(sizeof(char*)*1);
} else {
new_string_array = realloc(in_data->stored_string, sizeof(char*)*(nrows+1));
in_data->stored_string = new_string_array;
};
in_data->stored_string[nrows] = malloc(sizeof(char)*str_length); // first uninitialized warning
strcpy(in_data->stored_string[nrows], in_string); // second uninitialized warning
};
The declaration for in_data->stored_string
is char**
. in_data->stored_string
的声明为char**
。 I've also checked to make sure stored_string = NULL
is done before the set_library
function is called. 我还检查了确保在调用set_library
函数之前完成了set_library
stored_string = NULL
。 When realloc
is not called, I don't seem to get the error. 当不调用realloc
,我似乎没有收到错误。 Anyone have ideas what is causing the problem? 任何人都知道导致问题的原因是什么?
EDIT--------------------- 编辑 - - - - - - - - - - -
D'oh! 天哪! Opening up the debugger solved this. 打开调试器解决了这个问题。 Actually, the snippet in question had a few problems. 实际上,该代码段存在一些问题。 I placed the function to initialize values inside the wrong if
bracket. 我将函数初始化为在错误的if
括号内的值。 Anyways, valid points were raised in the comments, so.... 无论如何,评论中提出了有效的观点,所以...
void set_library (Foo* in_data, const int nrows, char* in_string) {
const int str_length = strlen(in_string)+1;
char** temp_string_array = NULL;
temp_string_array = realloc(in_data->stored_string, sizeof(*in_data->stored_string)*(nrows+1));
in_data->stored_string = temp_string_array;
in_data->stored_string[nrows] = malloc(sizeof(char)*str_length);
strcpy(in_data->stored_string[nrows], in_string);
}
D'oh! 天哪! Opening up the debugger solved this. 打开调试器解决了这个问题。 Actually, the snippet in question had a few problems. 实际上,该代码段存在一些问题。 I placed the function to initialize values inside the wrong if
bracket. 我将函数初始化为在错误的if
括号内的值。 Anyways, valid points were raised in the comments, so.... 无论如何,评论中提出了有效的观点,所以...
void set_library (Foo* in_data, const int nrows, char* in_string) {
const int str_length = strlen(in_string)+1;
char** temp_string_array = NULL;
temp_string_array = realloc(in_data->stored_string, sizeof(*in_data->stored_string)*(nrows+1));
in_data->stored_string = temp_string_array;
in_data->stored_string[nrows] = malloc(sizeof(char)*str_length);
strcpy(in_data->stored_string[nrows], in_string);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.