VeVeVez VeVeVez - 1 year ago 100
C Question

Error: Conditional jump or move depends on uninitialised value - C valgrind

i'm new to C and i'm stucked with a common problem but really i'm not able to understand the reason in my case.

i have this method whose purpose is to initialize a Map with a specified name and check if it exists in a RED-BLACK Tree.

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;




So i allocate memory for a Node which contains a Map, do the research and in the end free the memory of the temporary objects created. The structures of my objects are the following:

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

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

The method stringCopy and freeMap are:

void freeMap(struct rbNode *node){

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

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

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

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

return copy;

I'm testing the program using valgrind and this is the error i get:

Conditional jump or move depends on uninitialised value(s)
==5215== at 0x8049DF3: freeMap (in /home/ve/Dropbox/progetto2016/Percorsi/percorsi)
==5215== by 0x8048C40: reduceMap (in /home/ve/Dropbox/progetto2016/Percorsi/percorsi)
==5215== by 0x80489DA: main (in /home/ve/Dropbox/progetto2016/Percorsi/percorsi)
==5215== Uninitialised value was created by a heap allocation
==5215== at 0x402C17C: malloc (in /usr/lib/valgrind/
==5215== by 0x8048B0A: reduceMap (in /home/ve/Dropbox/progetto2016/Percorsi/percorsi)
==5215== by 0x80489DA: main (in /home/ve/Dropbox/progetto2016/Percorsi/percorsi)

I understand that the problem is caused by the allocation of tempMap and it seems that there is something not initialized int it but why? I only care about the name and the specification... I really do not understand.

Any help would be appreciate.
Thank you

Answer Source

Simply put, it seems that in your reduceMap, this happens

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

You don't initialize the entire allocated struct. Consider writing a map constructor (in the form of an initMap function), to match your destructor (freeMap).

Side note : Don't cast the result of malloc.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download