Crash on NULL

This is a discussion on Crash on NULL within the C Programming forums, part of the General Programming Boards category; Hi, can someone explain to me why de following code does crash the program after it reached the NULL pointer???? ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    10

    Crash on NULL

    Hi, can someone explain to me why de following code does crash the program after it reached the NULL pointer????

    the max value of Searchvalue = 5 but when i make it 6 it crashes my program.

    Code:
    struct parkinglot* FindParkingLot (struct parkinglot* StartPtr, int SearchValue)
    {		                            // start at head of the list
         struct parkinglot* HuidigePtr = StartPtr;
         while (HuidigePtr != NULL && HuidigePtr->Vak != SearchValue)  {
                HuidigePtr = HuidigePtr->VolgendePtr;
         }
         if (HuidigePtr->Vak != SearchValue)
            return (NULL);
         else 
            return (HuidigePtr);
    };

    regards Fred
    Last edited by TL62; 01-13-2008 at 03:22 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    If your while loop stops because HuidigePtr == NULL, then the if comparison that follows will explode (you can't try to follow NULL->Vak, because it doesn't exist). Try something like
    Code:
    if (HuidigePtr == NULL || HuidigePtr->Vak != SearchValue)
    instead; the NULL check has to be done first, and if it's true, the dereference on the right will get short-circuited.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,031
    EDIT: Oops, wrong code. Perhaps tabstop's or anon's suggestion is better.
    Last edited by Elysia; 01-14-2008 at 01:23 PM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    This should also work. After the loop HuidigePtr will either be NULL or point to the found node.

    Code:
    struct parkinglot* FindParkingLot (struct parkinglot* StartPtr, int SearchValue)
    {		                            // start at head of the list
         struct parkinglot* HuidigePtr = StartPtr;
         while (HuidigePtr != NULL && HuidigePtr->Vak != SearchValue)  {
                HuidigePtr = HuidigePtr->VolgendePtr;
         }
         return (HuidigePtr);
    };
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    Registered User
    Join Date
    Jan 2008
    Posts
    10

    Worked out

    Hi guy's Thx for your input.

    Worked it out

    The code as supplied by tabstop worked for me...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked List Not Saving Value as Int
    By bar338 in forum C Programming
    Replies: 4
    Last Post: 05-04-2009, 08:53 PM
  2. New string functions
    By Elysia in forum C Programming
    Replies: 11
    Last Post: 03-28-2009, 06:03 AM
  3. 20q game problems
    By Nexus-ZERO in forum C Programming
    Replies: 24
    Last Post: 12-17-2008, 05:48 PM
  4. Global Variables
    By Taka in forum C Programming
    Replies: 34
    Last Post: 11-02-2007, 04:25 AM
  5. . . . . . . - . . . - -
    By The Brain in forum C++ Programming
    Replies: 17
    Last Post: 05-17-2005, 05:01 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21