while loop and printf


S

ssantamariagarcia

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
 
Ad

Advertisements

M

Michael Mair

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
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
 
T

Tydr Schnubbis

The matter is that if the printf statement is removed, the loop break
and send a NULL exception message.
What do you mean by a NULL exception message? There are no exceptions
in C. If you get an error message, please post it.
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.
This doesnt' make much sense. Can you post the complete while loop?
 
A

Artie Gold

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.
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
 
B

Barry Schwarz

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.

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>>
 
K

Keith Thompson

Barry Schwarz said:
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.

Either that, or one of the arguments to printf() has a side effect
that alters the execution of the loop.
 
Ad

Advertisements

O

Old Wolf

Keith said:
Either that, or one of the arguments to printf() has a side effect
that alters the execution of the loop.

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.
 
Ad

Advertisements

N

Neil Ferguson

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);
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.
 

Top