Home > Mobile >  Can't find uninitialised value (valgrind)
Can't find uninitialised value (valgrind)

Time:12-04

So I'm making a queue list and I'm trying to get rid of memory leaks and uninitialised values. But when running with valgrind I keep getting Conditional jump or move depends on uninitialised value(s). I tried to debug the code and find the error but I can't.

Here is the code I'm running:

#include <fstream>
#include <iostream>
#include <string>
using namespace std;

struct Node{
    string item;
    Node* next;
    Node* prev;
};
struct Queue{
    int size;
    Node* head = NULL;
    Node* tail = NULL;

};
//Makes queue
Queue* createQueue(){
    Queue* n = new Queue;
    n->head = NULL;
    n->tail = NULL;
    n->size = 0;
    return n;
}
//checks if empty
bool isEmpty(Queue* List){
    if( List->size == 0){
        return true;
    }else{
        return false;
    }
}
// add item to queue
bool enqueue(Queue* List, string added){
    Node* newy= new Node;
    if(List->tail == NULL){
        List->head = List->tail = newy;
        return true;
    }
    List->tail->next = newy;
    List->tail = newy;
    List->size  ;
    return true;
}
//remove item from queue
string dequeue(Queue* List){
    Node* tempo = List->head;
    if(List->head == NULL){
        return "ERROR";
    }
    else if (tempo->next !=NULL){
        tempo = tempo->next;
        return List->head->item;
        free(List->head);
        List->head = tempo;
    }else{
        return List->head->item;
        free(List->head);
        List->head= NULL;
        List->tail = NULL;
    }
}

// display the queue
void print(Queue* List){
    Node* yuuur = List->head;

    while(yuuur != NULL){
        cout<<(yuuur->item)<<endl;
        yuuur = yuuur->next;
    }
}
// destroy queue
void destroyQueue(Queue* List){
    while(List->head !=NULL){
        Node *tempo = List->head;
        List->head= List->head->next;
        delete tempo;
    }
    List->tail = NULL;
    List->head = NULL;
    delete List;

}

My main to test the code:

//test code
int main(){
    Queue* q = createQueue();
    cout << boolalpha << isEmpty(q) << endl;
    cout << dequeue(q) << endl;
    enqueue(q, "Jos");
    enqueue(q ,"An");
    enqueue(q, "Peter");
    print(q);             //Jos, An en Peter worden op drie regels geprint
    string first = dequeue(q);
    cout << first << endl;  //Jos wordt geprint
    print(q);           //An en Peter worden geprint
    destroyQueue(q);
    return 0;
}

Valgrind error:

==77== Memcheck, a memory error detector
==77== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==77== Using Valgrind-3.14.0 and LibVEX; rerun with -h for copyright info
==77== Command: student/labo13
==77==
==77== Conditional jump or move depends on uninitialised value(s)
==77==    at 0x400DDA: print(Queue*) (in /task/student/labo13)
==77==    by 0x401002: main (in /task/student/labo13)
==77==
==77== Conditional jump or move depends on uninitialised value(s)
==77==    at 0x400DDA: print(Queue*) (in /task/student/labo13)
==77==    by 0x40103F: main (in /task/student/labo13)
==77==
==77== Conditional jump or move depends on uninitialised value(s)
==77==    at 0x400DDA: print(Queue*) (in /task/student/labo13)
==77==    by 0x401097: main (in /task/student/labo13)
==77==
==77== Conditional jump or move depends on uninitialised value(s)
==77==    at 0x400E23: destroyQueue(Queue*) (in /task/student/labo13)
==77==    by 0x4010A3: main (in /task/student/labo13)
==77==
==77==
==77== HEAP SUMMARY:
==77==     in use at exit: 0 bytes in 0 blocks
==77==   total heap usage: 10 allocs, 10 frees, 263 bytes allocated
==77==
==77== All heap blocks were freed -- no leaks are possible
==77==
==77== For counts of detected and suppressed errors, rerun with: -v
==77== Use --track-origins=yes to see where uninitialised values come from
==77== ERROR SUMMARY: 4 errors from 4 contexts (suppressed: 0 from 0)

CodePudding user response:

In enqueue function, you do:

    Node* newy= new Node;
    if(List->tail == NULL){
        List->head = List->tail = newy;
        return true;
    }

At this point, newy is a newly initialized Node object; however, none of its members have been set. You also don't adjust the size of the Queue.

Secondly, let's say that tail is not NULL, then you do:

    List->tail->next = newy;
    List->tail = newy;
    List->size  ;
    return true;

Again, at this point, newy does not have any of its members set.

What you probably want to do is (I didn't try it):

bool enqueue(Queue* List, const string &added){
    Node* newy= new Node;
    newy->item = added;
    newy->next = nullptr;
    newy->prev = List->tail;
    List->size  ;
    if(List->tail == nullptr){
        List->head = List->tail = newy;
        return true;
    }
    List->tail->next = newy;
    List->tail = newy;
    return true;
}

On another note, it seems like you are trying to treat C like it' C. You should use proper classes and member functions.

  • Related