簡體   English   中英

Valgrind C:如何從stdio輸入字符串

[英]Valgrind C: How to input string from stdio

這是代碼:

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.

這里的原始問題: 使valgrind能夠在c ++需要時讀取用戶輸入

編輯:

因此,對於您的情況,您可以嘗試:

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

&在另一個終端中,使用

cat > /tmp/abcd

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM