繁体   English   中英

即使我的变量已初始化,条件跳转或移动也取决于未初始化的值?

[英]Conditional jump or move depends on unitialised values(s) even though my variable is initialised?

根据 Valgrind 的说法,变量“holder”在我的函数 spaceMove() 中未初始化。 值得注意的是,它说每当我尝试操纵该变量时。 我也尝试在进入循环之前将其初始化为 NULL,但它仍然给我相同的消息。 如果有人能解释为什么持有者被认为是未初始化的,我们将不胜感激,谢谢。

void spaceMove(char *str, char *delim){
    int i, spPlus=0;
    char *holder;

    holder = malloc(strlen(str)+1);

    for(i=0; i<strlen(str); i++, spPlus++){
        if(str[i] == ' ' && str[i+1] == ' '){
            strcat(holder, " "); //line 11
            //spPlus += 1;
            i += 1;
        }
        else if(str[i] == '\t' && str[i+1] == '\t'){
            strcat(holder, "\t"); //line 16
            i += 1;
        }
        holder[spPlus] = str[i];
    }
    strcpy(str, holder);    //line 21
}

void processStrings(struct mainList *header){
    int i;
    struct stringList *temp;

    temp = header->next;

    while(temp != NULL){
        for(i=0; i<strlen(temp->string); i++){
            if(temp->string[i] == ' ' || temp->string[i] == '\t'){
                spaceMove(temp->string, " "); //line 101
            }
        }           
        temp = temp->next;
    }
}

这是 Valgrind 消息:

==3102== Conditional jump or move depends on uninitialised value(s)
==3102==    at 0x4C2F0A8: strcpy (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==3102==    by 0x400B38: spaceMove (listHelper.c:21)
==3102==    by 0x4010CF: Strings (listio.c:101)
==3102==    by 0x4009A4: main (tempMain.c:27)
==3102== 
==3102== Conditional jump or move depends on uninitialised value(s)
==3102==    at 0x400A69: spaceMove (listHelper.c:11)
==3102==    by 0x4010CF: Strings (listio.c:101)
==3102==    by 0x4009A4: main (tempMain.c:27)
==3102== 
==3102== Conditional jump or move depends on uninitialised value(s)
==3102==    at 0x400AC8: spaceMove (listHelper.c:16)
==3102==    by 0x4010CF: Strings (listio.c:101)
==3102==    by 0x4009A4: main (tempMain.c:27)

您不能将strcat与非'\\0'终止的字符串一起使用

holder = malloc(strlen(str)+1);
holder[0] = '\0'; /* Add this line */

或者使用calloc而不是malloc

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM