错误:条件跳转或移动取决于未初始化的值 - C valgrind

时间:2016-09-04 11:15:34

标签: c valgrind

我是C的新手,我遇到了一个常见的问题,但实际上我无法理解我的理由。

我有这个方法,其目的是初始化具有指定名称的Map并检查它是否存在于RED-BLACK树中。

Hit:1 http://bg.archive.ubuntu.com/ubuntu xenial InRelease
Hit:3 http://bg.archive.ubuntu.com/ubuntu xenial-backports InRelease           
Get:2 http://bg.archive.ubuntu.com/ubuntu xenial-updates InRelease [95,7 kB]   
Ign:4 http://bg.archive.ubuntu.com/ubuntu xenial/main armhf Packages           
Ign:5 http://bg.archive.ubuntu.com/ubuntu xenial/restricted armhf Packages
Ign:6 http://bg.archive.ubuntu.com/ubuntu xenial/universe armhf Packages
Ign:7 http://bg.archive.ubuntu.com/ubuntu xenial/multiverse armhf Packages
Ign:4 http://bg.archive.ubuntu.com/ubuntu xenial/main armhf Packages
Ign:5 http://bg.archive.ubuntu.com/ubuntu xenial/restricted armhf Packages
Ign:6 http://bg.archive.ubuntu.com/ubuntu xenial/universe armhf Packages
Ign:7 http://bg.archive.ubuntu.com/ubuntu xenial/multiverse armhf Packages
Ign:4 http://bg.archive.ubuntu.com/ubuntu xenial/main armhf Packages     
Ign:5 http://bg.archive.ubuntu.com/ubuntu xenial/restricted armhf Packages
Ign:6 http://bg.archive.ubuntu.com/ubuntu xenial/universe armhf Packages 
Hit:8 http://security.ubuntu.com/ubuntu xenial-security InRelease        
Ign:7 http://bg.archive.ubuntu.com/ubuntu xenial/multiverse armhf Packages
Ign:9 http://bg.archive.ubuntu.com/ubuntu xenial-backports/main armhf Packages
Ign:10 http://bg.archive.ubuntu.com/ubuntu xenial-backports/universe armhf Packages
Err:4 http://bg.archive.ubuntu.com/ubuntu xenial/main armhf Packages
  404  Not Found [IP: 195.85.215.252 80]
Ign:5 http://bg.archive.ubuntu.com/ubuntu xenial/restricted armhf Packages
Ign:6 http://bg.archive.ubuntu.com/ubuntu xenial/universe armhf Packages
Ign:7 http://bg.archive.ubuntu.com/ubuntu xenial/multiverse armhf Packages
Ign:9 http://bg.archive.ubuntu.com/ubuntu xenial-backports/main armhf Packages
Ign:10 http://bg.archive.ubuntu.com/ubuntu xenial-backports/universe armhf Packages
Ign:9 http://bg.archive.ubuntu.com/ubuntu xenial-backports/main armhf Packages
Ign:10 http://bg.archive.ubuntu.com/ubuntu xenial-backports/universe armhf Packages
Get:11 http://bg.archive.ubuntu.com/ubuntu xenial-updates/main amd64 Packages [383 kB]
Get:12 http://bg.archive.ubuntu.com/ubuntu xenial-updates/main i386 Packages [378 kB]
Ign:13 http://bg.archive.ubuntu.com/ubuntu xenial-updates/main armhf Packages  
Get:14 http://bg.archive.ubuntu.com/ubuntu xenial-updates/universe amd64 Packages [323 kB]
Get:15 http://bg.archive.ubuntu.com/ubuntu xenial-updates/universe i386 Packages [320 kB]
Ign:16 http://bg.archive.ubuntu.com/ubuntu xenial-updates/universe armhf Packages
Ign:17 http://bg.archive.ubuntu.com/ubuntu xenial-updates/multiverse armhf Packages
Err:9 http://bg.archive.ubuntu.com/ubuntu xenial-backports/main armhf Packages
  404  Not Found [IP: 195.85.215.252 80]
Ign:10 http://bg.archive.ubuntu.com/ubuntu xenial-backports/universe armhf Packages
Ign:13 http://bg.archive.ubuntu.com/ubuntu xenial-updates/main armhf Packages
Ign:16 http://bg.archive.ubuntu.com/ubuntu xenial-updates/universe armhf Packages
Ign:17 http://bg.archive.ubuntu.com/ubuntu xenial-updates/multiverse armhf Packages
Ign:13 http://bg.archive.ubuntu.com/ubuntu xenial-updates/main armhf Packages
Ign:16 http://bg.archive.ubuntu.com/ubuntu xenial-updates/universe armhf Packages
Ign:17 http://bg.archive.ubuntu.com/ubuntu xenial-updates/multiverse armhf Packages
Err:13 http://bg.archive.ubuntu.com/ubuntu xenial-updates/main armhf Packages
  404  Not Found [IP: 195.85.215.252 80]
Ign:16 http://bg.archive.ubuntu.com/ubuntu xenial-updates/universe armhf Packages
Ign:17 http://bg.archive.ubuntu.com/ubuntu xenial-updates/multiverse armhf Packages
Ign:18 http://security.ubuntu.com/ubuntu xenial-security/main armhf Packages
Ign:19 http://security.ubuntu.com/ubuntu xenial-security/universe armhf Packages
Ign:18 http://security.ubuntu.com/ubuntu xenial-security/main armhf Packages
Ign:19 http://security.ubuntu.com/ubuntu xenial-security/universe armhf Packages
Ign:18 http://security.ubuntu.com/ubuntu xenial-security/main armhf Packages
Ign:19 http://security.ubuntu.com/ubuntu xenial-security/universe armhf Packages
Err:18 http://security.ubuntu.com/ubuntu xenial-security/main armhf Packages
  404  Not Found [IP: 91.189.91.26 80]
Ign:19 http://security.ubuntu.com/ubuntu xenial-security/universe armhf Packages
Fetched 95,7 kB in 1s (59,8 kB/s)
Reading package lists... Done
E: Failed to fetch http://bg.archive.ubuntu.com/ubuntu/dists/xenial/main/binary-armhf/Packages  404  Not Found [IP: 195.85.215.252 80]
E: Failed to fetch http://bg.archive.ubuntu.com/ubuntu/dists/xenial-backports/main/binary-armhf/Packages  404  Not Found [IP: 195.85.215.252 80]
E: Failed to fetch http://bg.archive.ubuntu.com/ubuntu/dists/xenial-updates/main/binary-armhf/Packages  404  Not Found [IP: 195.85.215.252 80]
E: Failed to fetch http://security.ubuntu.com/ubuntu/dists/xenial-security/main/binary-armhf/Packages  404  Not Found [IP: 91.189.91.26 80]
E: Some index files failed to download. They have been ignored, or old ones used instead.

所以我为包含Map的Node分配内存,进行研究,最后释放创建的临时对象的内存。我的对象的结构如下:

void reduceMap(char *mapName){

    Map *tempMap = (Map *) malloc(sizeof(Map));
    struct rbNode *tempNode = (struct rbNode *) malloc(sizeof(struct rbNode));

    copyName = stringCopy(mapName);

    tempMap->name = copyName;
    tempNode->data = tempMap;

    ...DO SEARCH HERE...

    freeMap(tempNode);

}

方法stringCopy和freeMap是:

typedef struct{
    char *name;
    char *specification;
    Point *start, *end;
}Map;

struct rbNode{
    Color color;
    void *data;
    struct rbNode *parent, *leftChild, *rightChild;
};

我正在使用valgrind测试程序,这是我得到的错误:

void freeMap(struct rbNode *node){
    if(node){

         free( ((Map *)node->data)->name );

         if(((Map *)node->data)->specification != NULL) 
             free( ((Map *)node->data)->specification );        

         free( (Map *)node->data );
         free(node);
     }
 }


 char *stringCopy(const char *source){
     char *copy = (char *)malloc(strlen(source) + 1);

     strcpy(copy,source);
     return copy;
 }

据我所知,问题是由tempMap的分配引起的,似乎有些内容没有初始化,但为什么呢?我只关心名称和规格......我真的不明白。

任何帮助都会受到赞赏。 谢谢

2 个答案:

答案 0 :(得分:2)

简单地说,似乎在你的reduceMap,这种情况发生了

Map *tempMap = (Map *) malloc(sizeof(Map));
tempMap->name = copyName;
tempMap->specification = /* what!?? */;

您没有初始化整个分配的struct。考虑编写一个地图构造函数(以initMap函数的形式),以匹配析构函数(freeMap)。

旁注Don't cast the result of malloc

答案 1 :(得分:1)

您没有初始化地图中的规范成员

typedef struct{
    char *name;
    char *specification;
    Point *start, *end;
} Map;

但稍后你使用这个未初始化的值来有条件地跳转(if语句):

if(((Map *)node->data)->specification != NULL)

在使用之前初始化Map的所有成员。