Thread: NULL caseses

  1. #1
    Banned
    Join Date
    Apr 2015
    Posts
    596

    NULL caseses

    Hi guys, here I'm not asking what's NULL pointer, otherwise I'm asking why I need to consider NULL case once I solve a problem?

    lets assume I have tree and I want to traverse through it, so why in my code I should consider the case that the "root" == "NULL" which mean in the case that I don't have a tree .. but we logically assume that we have tree, otherwise I wouldn't write a code ...

    so any help to understand why I need to take care and consider the case of NULL in my cases to solve the problem?

    another question, when they said to solve question, is it means to solve it with all its cases? or can I assume assumption while solving?!

    thanks alot

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,083
    Quote Originally Posted by RyanC
    lets assume I have tree and I want to traverse through it, so why in my code I should consider the case that the "root" == "NULL" which mean in the case that I don't have a tree .. but we logically assume that we have tree, otherwise I wouldn't write a code ...
    You do have a tree: the empty tree.

    As for why: it tends to be convenient and elegant to consider the empty tree as the base case in recursion. You don't have to do it that way if you really don't want to (and if the requirements don't insist on it), but then you'll have to consider the empty tree as an error, which can be rather unnecessary, and also have to write a somewhat more complex condition to check for the tree consisting of just a root node (in addition to the error check for an empty tree).

    Quote Originally Posted by RyanC
    another question, when they said to solve question, is it means to solve it with all its cases? or can I assume assumption while solving?!
    You should ask "them".
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    547
    allocated roots, all I'm saying

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,424
    Having an empty tree, say tree == NULL
    Should be no more strange or unusual than having an empty string, say strlen(str) == 0

    Once you recognise that data structures with zero content are useful things, then things flow naturally.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Apr 2019
    Posts
    662
    an example a friend told me about as a reason why you should always check something is null .... there was a banking system that dealt with high frequency trading i a message came in it had to stop what it was doing and deal with the message and decide what to do. unfortunately though the designers hadn't thought about testing if there was actually a message ie it wasn't a zero length message if it was it crashed the whole system

  6. #6
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,859
    The same reason that you should check to see if a chair is there before sitting down.

  7. #7
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,859
    The same reason you check to see if your glass has drink in it before you take a sip

  8. #8
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,859
    The same reason that you check that your shoe is not a watermelon before putting it on...

    Should I go on?

  9. #9
    Registered User
    Join Date
    Apr 2019
    Posts
    662
    i was waiting for one about a hot poker...

  10. #10
    Registered User
    Join Date
    May 2016
    Posts
    101
    @Click_here LMAO
    printf("I'm a %s.\n", strrev("Dren"));

  11. #11
    Registered User catacombs's Avatar
    Join Date
    May 2019
    Location
    /home/
    Posts
    72
    Quote Originally Posted by Click_here View Post
    The same reason that you check that your shoe is not a watermelon before putting it on...

    Should I go on?
    One more, and I think we'll be good

  12. #12
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    547
    Quote Originally Posted by catacombs View Post
    One more, and I think we'll be good
    Here's it is:
    Same reason you check your clothes are on before you leave the house

  13. #13
    Registered User catacombs's Avatar
    Join Date
    May 2019
    Location
    /home/
    Posts
    72
    Same reason you check if your foot isn't on the gas before shifting into reverse.

  14. #14
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,859
    Making sure that your hat isn't full of spaghetti before putting it on.

    We've all been there...
    Fact - Beethoven wrote his first symphony in C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. assign null value to a struct and check if it is null
    By ymc1g11 in forum C Programming
    Replies: 10
    Last Post: 11-01-2012, 03:58 PM
  2. NULL vs. 0
    By Aisthesis in forum C++ Programming
    Replies: 21
    Last Post: 12-20-2009, 11:53 PM
  3. Replies: 9
    Last Post: 10-20-2007, 01:05 AM
  4. accept(ListenSocket, NULL, NULL); cause program to hang?
    By draggy in forum Networking/Device Communication
    Replies: 11
    Last Post: 06-16-2006, 03:40 PM
  5. null?
    By hostensteffa in forum C++ Programming
    Replies: 2
    Last Post: 06-16-2002, 12:46 AM

Tags for this Thread