0

I declared a linked list implemented in C as follows:

struct node_List {
    int i;
    char * name;
    struct node_List* next;
};

typedef struct node_List nodeList;

Then I declared the list head globally as:

nodeList list;        // head of the list - does not contain relevant data

Finally, I have a function id(char * s) with a string s as th only argument.

nodeType id(char *s)
{
    nodeType *p;         // another List type

    if ((p = malloc(sizeof(nodeType))) == NULL) {
        // error: out of memory;
    }
    nodeList * node = &list;

    // printf(" ");

    while (node->next != NULL){
        node = node->next;
        if (strcmp(node->name, s) == 0){
            // printf(" ");
            // assign node to an attribute in p
            return p;
       }
    }
    // error: not found;
}

The problem is, when i run this program and call foo("somestring") the program executes the error: not found part and aborts execution, despite the string somestring being in the list. I tried executing the very same program by inserting some printf() for debugging purposes, and it works perfectly, except it prints additional characters along with the output.

This happens each time I add some print lines, e.g. if I uncomment the two printf()s which I wrote in the example above (one of them or both, i get the same successful result). It doesn't work though if the printf is called with no arguments or with an empty string "".

I can't figure out what's happening, I double-checked the list creation and population functions and I am totally sure they work correctly. I tried changing the while break condition, but that didn't work, too. I have observed a similar behaviour on both Linux (with gcc) and Windows (using CodeBlocks editor's integrated compiler)

How could a printf directive affect a program so much?

EDIT: This code is part of a syntax analyzer written in Yacc. The whole code can be found below. It's a long read, and it is not completed, but the code above was tested and used to work as explained.

lexer: http://pastebin.com/1TEzzHie

parser: http://pastebin.com/vwCtMhX4

  • It looks like you skip first element of your list. In first step of while you use: node = node->next; – Oo.oO Feb 6 '17 at 19:38
  • The behavior is probably also dependent on what you initialize list to, and how you construct the rest of the linked list. You are not showing that – infixed Feb 6 '17 at 19:45
  • 2
    We can't debug partial code. Please provide a minimal reproducible example. – kaylum Feb 6 '17 at 19:51
  • 1
    Just to clarify - are you saying that: With the posted code the string can not be found but If the two printf are uncommented, the string is found? – 4386427 Feb 6 '17 at 20:11
  • 1
    You've got memory corruption issues, most likely. Or something seriously astray. Use a machine that has Valgrind on it and run with that. You've done enough work, it sounds like, to know that it is weird, and that the way you're currently trying to debug it doesn't work. So, you need to go into a different mode of debugging. Make the code testable; test it in isolation. Make sure it is clean with Valgrind in isolation. Then think about plugging it back into your more complex scenario. Undefined behaviour includes the option of 'appearing to work, mostly'. – Jonathan Leffler Feb 6 '17 at 21:00
0

When looking in the provided source code, the algorithm to explore the linked list has two ways to miss node in the while-loop comparison.

Way 1 - starting only from the second node of the list.

Placing node = node->next; before the comparison will force the first comparison to be &(list)->next instead of &(list).

To start from the first node, simply place node = node->next; after the comparison.

Way 2 - never ending to the last node of the list.

Using (node->next != NULL) in the while condition will force to exit from the loop before comparing the last node => node->next = NULL;.

To end by the last node, simply change the while condition to (node != NULL).

Solution:

while (node != NULL){ // end from the last node
    if (strcmp(node->name, s) == 0){
        // printf(" ");
        // assign node to an attribute in p
        return p;
    }
    node = node->next; // explore link after comparison
}
  • 1
    As written in a comment and in its declaration, list is an empty head, whose value is meant to be ignored. By placing node = node->next; right after the while I obtain the same result you describe. The question here is why a single non-empty printf statement could change the behaviour of the code so much, to which probably @JonathanLeffler answered in the best way so far in the comments above – Salvioner Feb 6 '17 at 22:11
0

The actual error is a wrong type declaration of a variable returned by the function:

nodeType* createPoint(char* l){
    nodeList* p;

    if((p=malloc(sizeof(nodeList))) == NULL){
         yyerror("out of memory");
    } else {
        // do stuff with p
    }

    return p;
}

The function return value was a nodeType* and p was instantiated as nodeList*. The declaration of those two types was pretty simple, that's why the program could work.

the working code can be found here.

The strange behaviour with printf() was probably caused by the heap space needed for printf's arguments: since this function accepts an arbitrary number of parameters, it saves them in a list. This list is instantiated in the heap, there overwriting the old data left there from the wrong implementation of createPoint.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.