Thread: Is this safe to do?

  1. #1
    Registered User
    Join Date
    Nov 2012
    Location
    Heraklion, Greece, Greece
    Posts
    26

    Is this safe to do?

    Hello.A quick question
    Code:
    while (currentP->next!=NULL && currentP->next->pid < tempP->pid)
                {
                    currentP = currentP->next;
                }
    Considering the fact that currentP->next may be null,isnt it dangerous to dig up currentP->next->pid.Or the while condition becomes false as soon as it hits currentP->next!=NULL?Thanks.

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    I would say no. I had the question myself too in the past.

    Code:
    #include <stdio.h>
    
    int main(void)
    {
            if(0 && (printf("Executed\n")))
                    printf("Won't be executed\n");
            return 0;
    }
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  3. #3
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,791
    std10093 is correct. Because of C's "short-circuit evalutation" if the left hand expression is false then the right hand expression is never evaluated.

  4. #4
    Registered User
    Join Date
    Nov 2012
    Location
    Heraklion, Greece, Greece
    Posts
    26
    Thanks!

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    To expand on Hodor's explanation, what he said is true for && (and) statements: if the left-hand expression is false, the right-hand expression is not evaluated.

    For || (or) statements, if the left-hand expression is true, the right-hand expression is omitted.

    Basically, think about how && and || work, what their truth tables look like. If you can determine the value of the whole statement by only evaluating the left-hand operator, then the right-hand expression will not be evaluated.

  6. #6
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by std10093 View Post
    Code:
    #include <stdio.h>
    
    int main(void)
    {
            if(0 && (printf("Executed\n")))
                    printf("Won't be executed\n");
            return 0;
    }
    Shouldn't that be..
    Code:
    #include <stdio.h>
    
     
    int main(void)
    {
    
            if(0 && (printf("Won't be executed\n")))
    
                    printf("Won't be executed\n");
    
            return 0;
    
    }
    ?

  7. #7
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Bravo to anduril, who mentioned what I forgot to!

    Well, I intented to let the OP test the code himself and test if it would be executed or not.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  8. #8
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,791
    Quote Originally Posted by std10093 View Post
    Well, I intented to let the OP test the code himself and test if it would be executed or not.
    I... believe you!

  9. #9
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Since I am telling the truth, it's good for someone to believe the facts.

    If someone is not, here is a logical proof.
    I said in my 1st post that "I would say no", which means that I answer no, it won't be executed!

    However India guy has a right ro complain. It would be better to write "executed?"

    EDIT: A point to remember, is that when dealing with logic conditions with AND operations, the order of the "parts of the condition" matter. Not careful order, may result to logical errors. For example, if we change the order in the code of Konstantinos, we do not do well!
    Last edited by std10093; 11-20-2013 at 06:25 PM.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  10. #10
    Stoned Witch Barney McGrew's Avatar
    Join Date
    Oct 2012
    Location
    astaylea
    Posts
    420
    What you have is fine as it is; it's an error to dereference currentP->next if currentP->next stores a null pointer. Your code will ensure that currentP->next isn't dereferenced in such a case.

    With that said, your code will skip the first element of your linked list -- and it will produce the error noted above on an empty list. A better choice would be the following:

    Code:
    while (currentP != NULL && currentP->pid < tempP->pid)
            currentP = currentP->next;
    i dont believe in competition in da field of cboard posts, u see, i believe in a collection of posts each 2 be admired for der own personal statement. when in doubt, ask Willy!

  11. #11
    Registered User
    Join Date
    Nov 2012
    Location
    Heraklion, Greece, Greece
    Posts
    26
    I am using the while loop to insert in a sorted linked list and i have already taken care of the case that my list is empty.

    Quote Originally Posted by std10093 View Post
    Bravo to anduril, who mentioned what I forgot to!

    Well, I intented to let the OP test the code himself and test if it would be executed or not.
    The code that i used for my question was already part of a program i am working on,so no need to test your code,since i have mine.Thanks for the help guys.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Safe get
    By zhangz64 in forum C Programming
    Replies: 7
    Last Post: 05-12-2013, 06:00 PM
  2. Is this safe?
    By Shokwav in forum C++ Programming
    Replies: 6
    Last Post: 03-05-2012, 03:36 PM
  3. Is this safe?
    By pheres in forum C++ Programming
    Replies: 22
    Last Post: 04-01-2008, 03:02 PM
  4. Is this safe?
    By caduardo21 in forum C Programming
    Replies: 55
    Last Post: 06-30-2005, 09:01 AM
  5. How Safe is your pc
    By GSLR in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 09-16-2003, 02:53 AM