[英]malloc inside function / call-by-reference / valgrind
I'm not very professional on C and so I get a problem with valgrind. 我在C上不是很专业,因此我遇到了valgrind的问题。
I want to read in an input from command-line. 我想从命令行读取输入。
If I try it this way it works fine. 如果我这样尝试它就可以了。
//call the function
char *command = getUserInput();
//function
char *getUserInput()
{
char *buffer = NULL;
char *temp = NULL;
unsigned int count = 0;
unsigned int lenght = 10;
char character = 0;
buffer = malloc((lenght+1)*sizeof(char));
if(buffer == NULL)
{
// printf(ERROR_OUT_OF_MEM);
// return EXIT_OUT_OF_MEM;
}
while((character = getchar()) != '\n')
{
if(count == lenght)
{
lenght += 10;
temp = realloc(buffer,lenght*sizeof(char));
if(temp != NULL)
{
buffer = temp;
}
else
{
free (buffer);
// printf(ERROR_OUT_OF_MEM);
// return EXIT_OUT_OF_MEM;
}
}
buffer[count] = character;
count++;
}
buffer[count] = '\0';
return buffer;
}
But I cant geht an error-returnvalue. 但我不能提出错误返回值。
If I try it in the way by call-by-reference i get some valgrind error I don't understand. 如果我通过call-by-reference尝试它,我得到一些valgrind错误,我不明白。 And I know, in this example-function-call I don't request the return-value.
我知道,在这个例子中,函数调用我没有请求返回值。
//call function
char *command = NULL;
getUserInput(command);
//function
int getUserInput(char *name)
{
char *temp = NULL;
unsigned int count = 0;
unsigned int lenght = 10;
char character = 0;
name = malloc((lenght+1)*sizeof(char));
checkMemory(name);
while((character = getchar()) != '\n')
{
if(count == lenght)
{
lenght += 10;
temp = realloc(name,lenght*sizeof(char));
if(temp != NULL)
{
name = temp;
}
else
{
free (name);
printf(ERROR_OUT_OF_MEMORY_MESSAGE);
return ERROR_OUT_OF_MEMORY;
}
}
name[count] = character;
count++;
}
name[count] = '\0';
return RETURN_SUCCESS;
}
Commandhandler-Line 1199 is if (strcmp(command, "thing_to_compare") == 0)
Commandhandler-Line 1199是
if (strcmp(command, "thing_to_compare") == 0)
Valgrind: Valgrind的:
==23886== Use of uninitialised value of size 4
==23886== at 0x40256BB: strcmp (mc_replace_strmem.c:426)
==23886== by 0x8049B39: commandHandler (assa.c:1199)
==23886== by 0x8049D6C: main (assa.c:1295)
==23886== Uninitialised value was created by a stack allocation
==23886== at 0x8049B00: commandHandler (assa.c:1189)
==23886==
==23886== Invalid read of size 1
==23886== at 0x40256BB: strcmp (mc_replace_strmem.c:426)
==23886== by 0x8049B39: commandHandler (assa.c:1199)
==23886== by 0x8049D6C: main (assa.c:1295)
==23886== Address 0x0 is not stack'd, malloc'd or (recently) free'd
I totally don't know where the problem is. 我完全不知道问题出在哪里。
kind regards Philipp 亲切的问候Philipp
getUserInput
has no way to change the value of command
, as all functions are pass-by-value in C. Therefore, when you get to all your strcmp
functions, you are dereferencing that NULL
pointer causing all sorts of bad things. getUserInput
无法更改command
的值,因为所有函数都是C中的值传递。因此,当您到达所有strcmp
函数时,您将取消引用该NULL
指针,从而导致各种不良事件。
If you want getUserInput
to be able to change the value of command
, you must pass the address of command
to the function (which means the function must then take a char**
argument). 如果希望
getUserInput
能够更改command
的值,则必须将command
的地址传递给该函数(这意味着该函数必须采用char**
参数)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.