【问题标题】:c++ valgrind: Conditional jump or move depends on uninitialised value(s) don't know what to initializec ++ valgrind:条件跳转或移动取决于未初始化的值不知道要初始化什么
【发布时间】:2015-01-01 02:11:37
【问题描述】:

我一直试图了解我在这段代码中没有初始化什么,我完全(?)理解什么是未初始化的,但我不知道如何初始化它。

我收到错误:

==11931== Conditional jump or move depends on uninitialised value(s)
==11931==    at 0x804ABA6: Hashtable<int>::put(int, int) (hash_table.h:169)
==11931==    by 0x8048F80: test_put() (hash_table_test.cpp:27)
==11931==    by 0x804A551: main (hash_table_test.cpp:52)
==11931==  Uninitialised value was created by a heap allocation
==11931==    at 0x402ADFC: operator new[](unsigned int) (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==11931==    by 0x804A9AE: Hashtable<int>::Hashtable() (hash_table.h:64)
==11931==    by 0x8048F62: test_put() (hash_table_test.cpp:26)
==11931==    by 0x804A551: main (hash_table_test.cpp:52)

从 valgrind 看来,我没有正确初始化 Hashtable 类的 c'tor:

    Hashtable() :
            ht_keys(2), ht_size(0), dynamicArray(NULL) {
        dynamicArray = new Node[ht_keys];
        for (int i = 0; i < ht_keys; i++) {
            dynamicArray[i].delete_val = false;
            dynamicArray[i].key=0;
            dynamicArray[i].default_node = false;
        }
    }

动态数组是 Node* 类型,它的私有字段是:

    bool delete_val;
    T element;
    int key;
    bool default_node;

类 Node 位于类 Hashtable 中。 如何初始化 dynamicArray?

这是完整的代码:

#include <string>
#include <iostream>
#include "library2.h"
#include <iterator>

using namespace std;

#ifndef HASH_TABLE_HPP_
#define HASH_TABLE_HPP_
#define DIV 2
//type T must have c'tor, operator !=

template<class T>
class Hashtable {
public:
    class Node {
    public:
        Node(const T t) :
                delete_val(false), element(t), key(0), default_node(true) {

        }
        Node(bool v, const Node& n) :
                delete_val(v), element(n.element), key(0), default_node(
                        n.default_node) {
        }
        Node(const Node& n) :
                delete_val(false), element(n.element), key(n.key), default_node(
                        n.default_node) {
        }
        Node() :
                delete_val(false), key(0), default_node(true) {
        }

        bool operator==(const Node* n) {
            if (n) {
                if (element != n->element || default_node != n->default_node) {
                    return false;
                }
                return true;
            }
            return false;
        }
        bool operator!=(const Node n) {
            if (!(*this == n)) {
                return false;
            }
            return true;
        }
        bool delete_val;
        T element;
        int key;
        bool default_node;
    };

    Hashtable() :
            ht_keys(2), ht_size(0), dynamicArray(NULL) {
        dynamicArray = new Node[ht_keys];
        for (int i = 0; i < ht_keys; i++) {
            dynamicArray[i].delete_val = false;
            dynamicArray[i].key=0;
            dynamicArray[i].default_node = false;
        }
    }

    //seriously damaged programming...
    Hashtable(Node* array, int HT_keys, int HT_size) :
            ht_keys(HT_keys), ht_size(HT_size) {
        dynamicArray = new Node[ht_keys];
        if (array != NULL) {
            for (int i = 0; i < ht_keys; i++) {
                dynamicArray[i] = array[i];
            }
        }
    }
    Hashtable(const Hashtable& ht) {
        if (&ht == this) {
            return;
        }
        ht_keys = ht.ht_keys;
        ht_size = ht.ht_size;
        dynamicArray = new Node[ht_keys];
        for (int i = 0; i < ht.ht_keys; i++) {
            this->dynamicArray[i] = ht.dynamicArray[i];
        }
    }
    ~Hashtable() {
        delete[] this->dynamicArray;
    }
    Hashtable operator=(const Hashtable& ht) {
        Hashtable<T> newHT = ht;
        return newHT;
    }

    //Returns true if some value equal to value exists within the hash table.
    bool contains(Node n, int i) {
        if (i < 0 || i > ht_keys || !n) {
            return false;
        }
        if (i == ht_keys) {
            return false;
        }
        //make sure that n.delete_val is not set as true.
        if (dynamicArray[i]->element == n.element
                && !dynamicArray[i]->delete_val) {
            return true;
        }
        if (dynamicArray[i]->delete_val) {
            return contains(n, i + 1);
        }
        return false;
        return true;
    }
    //Returns true if some key equal to key exists within the hash table.
    bool containsKey(int i) {
        if (i < 0 || i > ht_keys) {
            return false;
        }
        if (dynamicArray[i]->element && !dynamicArray[i]->delete_val) {
            return true;
        }
        return false;
    }
    //Returns true if some value equal to value exists within the hash table.
    bool containsValue(T e) {

        return true;
    }
    //Returns an enumeration of the values contained in the hash table.
    int enumeration() {
        return ht_size;
    }
    //Returns the object that contains the value associated with key.
    //If key is not in the hash table, a null object is returned.
    Node get(int i) {
        if (i >= 0) {
            return dynamicArray[i % ht_keys];
        }
        Node n;
        return n;
    }
    //Returns true if the hash table is empty;
    //returns false if it contains at least one key.
    bool isEmpty() {
        if (ht_size) {
            return false;
        }
        return true;
    }
    //Returns an enumeration of the keys contained in the hash table.
    int keys();
    //Inserts a key and a value into the hash table.
    //Returns false if key isn't already in the hash table;
    //returns true if key is already in the hash table.
    bool put(T e, int i) {
        if (e && i > 0) {
            Node n;
            n.default_node = false;
            n.delete_val = false;
            n.key = i;
            n.element = e;
            //line 168
            for (int j = (i % ht_keys); j < ht_keys; j = ((j + 1) % ht_keys)) { //line 169
                if (!dynamicArray[j % ht_keys].element
                        || dynamicArray[j % ht_keys].delete_val) {
                    dynamicArray[j % ht_keys] = n;
                    ht_size++;
                    return true;
                }else if (i == (j + 1) % ht_keys) {
                    rehash();
                    return put(e, i);
                }
            }
            return false;
        }
        return false;
    }

    bool put_aux(Node n, int i, Node* Array, int HT_keys) {

        for (int j = (i % HT_keys); j < HT_keys; j = ((j + 1) % HT_keys)) {
            if (!Array[j % HT_keys].element || Array[j % HT_keys].delete_val) {
                Array[j % HT_keys] = n;
                return true;
            } else if (Array[j % HT_keys].element == n.element) {
                return true;
            }
        }
        return false;
    }

//Increases the size of the hash table and rehashes all of its keys.
    void rehash() {
        int old_ht_keys = ht_keys;
        ht_keys = DIV * ht_keys;
        Node* newArray = new Node[ht_keys];
        if (ht_keys > DIV) {
            for (int j = 0; j < old_ht_keys; j++) {
                put_aux(dynamicArray[j],dynamicArray[j].key,newArray,ht_keys);
            }
        }
        delete[] dynamicArray;
        dynamicArray = newArray;
    }
//Removes key and its value.
//Returns the value associated with key.
//If key is not in the hash table, a null objecht_sizet is returned.
    T remove(int i) {
        if (i >= 0 && i < ht_keys) {
            Node deleted_node(true, dynamicArray[i % ht_keys]);
            dynamicArray[i % ht_keys] = deleted_node;
            ht_size--;
            return deleted_node.element;
        }
        return NULL;
    }
//Returns the number of entries in the hash table.
    int size() {
        return this->ht_size;
    }

private:
    int ht_keys;
    int ht_size;
    Node* dynamicArray;
};

#endif /* HASH_TABLE_HPP_ */

【问题讨论】:

  • 您列出的完整代码是 hash_table.h 正确吗?它与您的 valgrind 输出完全相同 hash_table.h 吗?我问的原因是 valgrind 提到的行似乎是一个空行和一个 rehash() 调用。
  • 是的,尽管我使用 cpp 文件从那里调用函数(一个测试文件)。这就是为什么你只能看到 valgrind 中提到的一半功能
  • 了解测试,但发布的代码似乎与 hast_table.h 的 valgrind 输出不匹配。 169买什么线?
  • 我看到了问题,第 169 行是函数 put 中的 for 循环。我会编辑那个

标签: c++ initialization hashtable valgrind


【解决方案1】:

它似乎在抱怨 !dynamicArray[j % ht_keys].element 行(在您发布的代码的第 163 行;如果您发布的代码与 valgrind 使用的代码相匹配,这会容易得多;现在您发布的代码有几个行比 valgrind 使用的代码短)。

当您在构造函数中分配内存时,您永远不会初始化 element 成员。然后,您尝试在条件语句中使用它。 valgrind 正确地警告您该问题。

【讨论】:

  • 感谢您的评论,很抱歉浪费时间将代码与 valgrind 匹配,尽管我最近在代码本身中使用正确的行作为评论编辑了帖子。任何人,您如何建议我初始化 T 类型的模板?我不知道它是如何构建的,这就是为什么我让 new[] 运算符的默认初始化程序默认初始化它,因此出现错误。如果有更好的方法,请告诉...
  • 好吧,我使用了强大的 google 工具并在这里找到了答案:link 成功了!多谢! :D
猜你喜欢
  • 2019-08-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-18
  • 2016-07-24
  • 2014-11-30
  • 1970-01-01
相关资源
最近更新 更多