me estoy rompiendo la cabeza durante horas con el siguiente problema: he pegado 2 funciones, aunque hay muchas más. corro valgrind en mi programa y me sale 32 errores similares a esta:El salto o movimiento condicional depende de los valores sin inicializar
==4214== 6 errors in context 8 of 10:
==4214== Conditional jump or move depends on uninitialised value(s)
==4214== at 0x40088F: getNextFreeCell (in /a/fr-01/vol/home/stud/ashers03/c/ex4/test)
==4214== by 0x400C7A: InsertObject (in /a/fr-01/vol/home/stud/ashers03/c/ex4/test)
==4214== by 0x401137: main (in /a/fr-01/vol/home/stud/ashers03/c/ex4/test)
me sale más errores en otras funciones, sin embargo, es el mismo error. No puedo entender por qué no está inicializado. Gracias a todos por su ayuda.
esta es la función principal:
int main(int argc, char* argv[]) {
size_t tableSize = (size_t)atoi(*(argv+1));
TableP table = CreateTable(tableSize,IntFcn, IntPrint,IntCompare);
int i;
for (i=FIRST; i<=LAST; i++) {
int *key = (int*)malloc(sizeof(int));
*key = i;
ObjectP obj = CreateObject(key);
InsertObject(table,obj);
}
PrintTable(table);
FreeTable(table);
return 0;
}
estos defs están en un archivo de cabecera:
typedef struct Object* ObjectP;
typedef struct Table* TableP;
typedef const struct Table* ConstTableP;
typedef enum {FALSE, TRUE} Boolean;
este código está en otro archivo:
typedef struct Table {
ObjectP* _table;
int _firstTableSize;
int _currentTableSize;
int _increaseFactor;
HashFcn _hfun;
PrintFcn _pfun;
ComparisonFcn _fcomp;
} Table;
typedef struct Object {
ObjectP _next;
void* _key;
int _numInChain;
} Object;
esta función inserta una clave para una tabla hash 3 llaves si ya están encadenados en la celda a continuación, el tamaño de la mesa se dobla y lo estoy haciendo algunas otras cosas en doubleTable() ...
Boolean InsertObject(TableP table, ObjectP object) {
int index=table->_increaseFactor*table->_hfun(object->_key,table->_firstTableSize);
if (table->_table[index] != NULL) {
if (table->_table[index]->_numInChain == MAX_CHAIN) { //search for next cell
int nextFreeCell = getNextFreeCell(table,index+1);
if (nextFreeCell == FAILED) { //double table size
if(doubleTable(table)) {
InsertObject(table,object);
return TRUE;
}
else {
ReportError(MEM_OUT);
return FALSE;
}
}
else {
table->_table[nextFreeCell] = chainObject(table->_table[nextFreeCell],object);
return TRUE;
}
}
else { //place object in chain:
table->_table[index] = chainObject(table->_table[index],object);
return TRUE;
}
}
else { //empty cell, place object
table->_table[index] = chainObject(table->_table[index],object);
return TRUE;
}
}
static int getNextFreeCell(TableP table, int index) {
int tableSize = table->_currentTableSize;
while ((index < tableSize) && (index % table->_increaseFactor != 0)) {
if (table->_table[index] == NULL || table->_table[index]->_numInChain < MAX_CHAIN) {
return index;
}
index++;
}
return FAILED;
}
EDIT:
me encontré valgrind como bien dice y me dieron:
==4563== Conditional jump or move depends on uninitialised value(s)
==4563== at 0x40088F: getNextFreeCell (GenericHashTable.c:75)
==4563== by 0x400C7A: InsertObject (GenericHashTable.c:222)
==4563== by 0x401137: main (HashIntMain.c:34)
==4563== Uninitialised value was created by a heap allocation
==4563== at 0x4C241A7: malloc (vg_replace_malloc.c:195)
==4563== by 0x4007AF: allocateArray (GenericHashTable.c:41)
==4563== by 0x400924: doubleTable (GenericHashTable.c:90)
==4563== by 0x400C8F: InsertObject (GenericHashTable.c:225)
==4563== by 0x401137: main (HashIntMain.c:34)
tengo este método:
static ObjectP* allocateArray(int tableSize) {
objectP* arr = (ObjectP*)malloc(tableSize * sizeof(ObjectP));
return arr;
}
esto crea una matriz de punteros, WHI ch Nunca me inicié. ¿Podría ser este el problema? y cómo debería inicializar una matriz de punteros? a NULL?
¿Ha compilado su código con la información de depuración "-g"? Si es así, ¿te dice la línea o variable exacta por la que está gimiendo? – bramp
La alineación de '{}' en la función 'InsertObject' es engañosa. El '{' after 'if (doubleTable (table)) {' no tiene par "alineado". No sé si esta fue la intención, pero es difícil ver algo en un código tan terriblemente desalineado. – AnT
me he editado la publicación al final – Mike