while loop and printf

Discussion in 'C Programming' started by ssantamariagarcia, Jul 14, 2005.

  1. I have found a problem while using a while statement and a linked list.
    I had never met this matter before....and I am wondering that if you
    have , please tell me what it is wrong.

    I am traversing the linked list using a pointer to cycle through.

    int Function(...){

    node * p;

    p=linkedlist; // p is a pointer to the list first node .

    while (p!=NULL){

    if(....){

    // do something with p

    }

    printf("\nIf I place a printf here , the while loop woks out;
    it doesn't if not);
    p=p->siguiente;

    }

    }

    The matter is that if the printf statement is removed, the loop break
    and send a NULL exception message.

    The printf can be placed after advancing linked list, it can be the
    first statement of the loop.... even it can be before this function is
    called , and all works right.
    But not without it.

    Any idea?

    Thanks a lot, SONIA
     
    ssantamariagarcia, Jul 14, 2005
    #1
    1. Advertisements

  2. ssantamariagarcia

    Michael Mair Guest

    Please do not post pseudo-code which cannot be compiled but rather
    a minimal example.
    How to get a minimal example: Start throwing out all functions you
    do not need to reproduce the error or undesired/strange behaviour.
    Then throw out all code in the functions you need until you cannot
    remove any line, statement or whatever without destroying what you
    want to show us.
    By then, you probably will have found the problem -- if not, you
    will have made it easy for us to help you.

    Without that, we have to revert to crystal balls.

    Some guesses:
    - you might access a deallocated node because your cleanup at
    deallocation does not work (i.e. the preceding node's link is
    not set to NULL). The deallocated memory contains different
    things depending on the printf() placement.
    - you might use a pointer which is uninitialised and points
    _somewhere_, maybe not even a valid storage location (or contains
    a null pointer representation). In order to avoid that, initialise
    each and every pointer to NULL at declaration.


    Cheers
    Michael
     
    Michael Mair, Jul 14, 2005
    #2
    1. Advertisements

  3. What do you mean by a NULL exception message? There are no exceptions
    in C. If you get an error message, please post it.
    This doesnt' make much sense. Can you post the complete while loop?
     
    Tydr Schnubbis, Jul 14, 2005
    #3
  4. ssantamariagarcia

    Artie Gold Guest

    Unfortunately, it's impossible to say what's going on here from what
    you've posted. Please post the smallest compilable (appropriately
    formatted) code snippet that exhibits this behavior; that will greatly
    enhance your chances of getting the help you need.

    HTH,
    --ag
     
    Artie Gold, Jul 14, 2005
    #4
  5. As others have indicated, we need more data. As a general rule of
    thumb, if adding a printf statement like yours alters the behavior of
    the program this drastically, you probably have some form of undefined
    behavior, usually overrunning an array or an allocated area.


    <<Remove the del for email>>
     
    Barry Schwarz, Jul 15, 2005
    #5
  6. Either that, or one of the arguments to printf() has a side effect
    that alters the execution of the loop.
     
    Keith Thompson, Jul 15, 2005
    #6
  7. ssantamariagarcia

    Old Wolf Guest

    Another possibility is that the statement before the printf()
    ended with a comma instead of a semicolon, or perhaps it was
    an if(), for() etc.
     
    Old Wolf, Jul 15, 2005
    #7
  8. In the unlikely event that this is a verbatim copy of your code, note the
    missing end quote. Also, I suggest as a rule you put the newline character
    at the end of the line unless the logic of the function requires it the way
    you have it.
     
    Neil Ferguson, Jul 15, 2005
    #8
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.