我无法解决“有条件的跳跃或移动取决于未初始化的价值”

时间:2015-12-02 09:05:01

标签: c++ valgrind

我读了很多关于这方面的问题,但我无法理解为什么它会在我的代码中发生。这是代码的一部分,我只留下了重要的部分:

class DiccString{
private:
map<char,DiccString<T>*> siguiente;
DiccString* padre;
string palabra;
char letra;
bool es_final;
T* significado;
int cant;

void _definir(const string &clave, T &_significado, Nat index)
{
    cant++;
    if(index == clave.size())
    {
        es_final = true;
        significado = new T(_significado);
    }
    else
    {
        if(siguiente.find(clave[index])==siguiente.end())
        {
            siguiente[clave[index]] = new DiccString<T>;
            siguiente[clave[index]]->padre = this;
            siguiente[clave[index]]->letra = clave[index];
            siguiente[clave[index]]->palabra = palabra+clave[index];
        }
        siguiente[clave[index]]->_definir(clave,_significado,index+1);
    }
}

void _borrar(const string &clave, Nat index)
{
    cant--;
    if(index == clave.size())
    {
        es_final = false;
        significado = NULL;
    }
    else
    {
        if(siguiente[clave[index]]->cant == 1)
        {
            delete siguiente[clave[index]];
            siguiente.erase(clave[index]);
        }
    }
}

public:
DiccString()
{
    siguiente.clear();
    padre = NULL;
    es_final = false;
    significado = NULL;
    letra = '\0'; ///El equivalente a NULL porque la raiz no representa ninguna letra
    palabra = "";
}

DiccString(DiccString<T> &otro)
{
    siguiente.clear();
    padre = NULL;
    es_final = false;
    significado = NULL;
    letra = '\0'; ///El equivalente a NULL porque la raiz no representa ninguna letra
    palabra = "";
    vector<string> keys = otro.claves();
    for(Nat i=0;i<keys.size();i++)
        definir(keys[i],*otro.obtener(keys[i]));
}

// DiccString operator=(const DiccString<T> &otro)            //  le saque el const D:
/*DiccString operator=(DiccString<T> &otro)
{
    for(typename map<char,DiccString<T>*>::iterator it = siguiente.begin(); it != siguiente.end(); it++)
        delete (*it).second;
    delete significado;
    siguiente.clear();
    padre = NULL;
    es_final = false;
    significado = NULL;
    letra = '\0'; ///El equivalente a NULL porque la raiz no representa ninguna letra
    palabra = "";
    vector<string> keys = otro.claves();
    for(int i=0;i<keys.size();i++)
        definir(keys[i],otro.obtener(keys[i]));
    return *this;
}*/

DiccString operator=(const DiccString<T> &otro)
{
    for(typename map<char,DiccString<T>*>::iterator it = siguiente.begin(); it != siguiente.end(); it++)
        delete (*it).second;
    delete significado;
    siguiente.clear();
    padre = NULL;
    es_final = false;
    significado = NULL;
    letra = '\0'; ///El equivalente a NULL porque la raiz no representa ninguna letra
    palabra = "";
    vector<string> keys = otro.claves();
    for(Nat i=0;i<keys.size();i++)
        definir(keys[i],*otro.obtener(keys[i]));
    return *this;
}

~DiccString()
{
    for(typename map<char,DiccString<T>*>::iterator it = siguiente.begin(); it != siguiente.end(); it++)
        delete (*it).second;
    delete significado;
}

void definir(const string &clave, T &_significado)
{
    _definir(clave,_significado,0);
}

当我执行valgrind时,我得到了这个:

==13607== 1 errors in context 5 of 5:
==13607== Conditional jump or move depends on uninitialised value(s)
==13607==    at 0x40C397: DiccString<InfoEH>::_borrar(std::string const&, unsigned int) (in /base/test)
==13607==    by 0x409F03: DiccString<InfoEH>::borrar(std::string const&) (in /base/test)
==13607==    by 0x406F34: Seguro::borrarNombre(std::string) (in /base/test)
==13607==    by 0x405D56: Seguro::mover(std::string, Direccion) (in /base/test)
==13607==    by 0x4120AA: aed2::Driver::mover(std::string, Direccion) (in /base/test)
==13607==    by 0x413F95: test_mover() (in /base/test)
==13607==    by 0x4173FC: main (in /base/test)
==13607==  Uninitialised value was created by a heap allocation
==13607==    at 0x4C2B0E0: operator new(unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==13607==    by 0x40C57F: DiccString<InfoEH>::_definir(std::string const&, InfoEH&, unsigned int) (in /base/test)
==13607==    by 0x409FA6: DiccString<InfoEH>::definir(std::string const&, InfoEH&) (in /base/test)
==13607==    by 0x407136: Seguro::definirNombre(std::string, EH, Posicion) (in /base/test)
==13607==    by 0x405606: Seguro::ingresarEstudiante(std::string, Posicion) (in /base/test)
==13607==    by 0x411FC8: aed2::Driver::ingresarEstudiante(std::string, Posicion) (in /base/test)
==13607==    by 0x413F4F: test_mover_estudiante() (in /base/test)
==13607==    by 0x4173FC: main (in /base/test)
==13607== 
==13607== ERROR SUMMARY: 5 errors from 5 contexts (suppressed: 0 from 0)

我认为问题出在siguiente[clave[index]] = new DiccString<T>;行,但我无法找到初始化的方法。

0 个答案:

没有答案