gpt4 book ai didi

c - 瓦尔格林德 C : How to input string from stdio

转载 作者:行者123 更新时间:2023-11-30 17:56:15 27 4
gpt4 key购买 nike

这是代码:

char* inputString(){
int n = 5;
int size = n;
char* const_str = (char*)malloc((n+1)*sizeof(char));
char* substring = (char*)malloc((n+n)*sizeof(char)); /*here*/
char*p;
while((fgets(const_str,n,stdin)!=NULL)&&(strchr(const_str,'\n')==NULL)){
strcat(substring,const_str);
size += n;
substring = (char*)realloc(substring,size*sizeof(char)); /*here*/
}
strcat(substring,const_str);
size += n;
substring = (char*)realloc(substring,size*sizeof(char)); /*here*/
/*
printf("<%s> is \n",const_str);
printf("%s is \n",substring);
printf("%d is \n",size);
*/
if ((p=strchr(substring,'\n'))!=NULL){
p[0]='\0';
}
if(feof(stdin)){
changeToFull();
}
return substring;
}

并且它在 valgrind 上不起作用。

我猜,我这里有内存泄漏,但是,我看不到任何好的解决方案来为 valgrind 重写这个函数。

请帮忙!

最佳答案

我还没有尝试过,但我在 SO 的一个问题上发现了这个:

--input-fd=<number> [default: 0, stdin]
Specify the file descriptor to use for reading input from the
user. This is used whenever valgrind needs to prompt the user
for a decision.

原始问题在这里:making valgrind able to read user input when c++ needs it

编辑:

因此,对于您的情况,您可以尝试:

mkfifo /tmp/abcd
exec 3</tmp/abcd
valgrind_command...... --input-fd=3

&在另一个终端中,使用

cat > /tmp/abcd

关于c - 瓦尔格林德 C : How to input string from stdio,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13718167/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com