Thread: Pointer Mock Exam Question

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    93

    Pointer Mock Exam Question

    Hi,

    Ive been given this code:

    Code:
    int i=0, j=0, *k=0, *l=0;
    k = l;
    l = &i;
    j = *k;
    Ive also been told that this code crashes. The question is, explain the cause of the crash, and correct the program by rearranging the istructions without modifiying them.

    Can someone help me to understand why it is crashing. Ive been told that line 4 is the problem, but I dont really understand why.

  2. #2
    Registered User Sharke's Avatar
    Join Date
    Jun 2008
    Location
    NYC
    Posts
    303
    Why not compile it and see what happens?

  3. #3
    and the hat of copycat stevesmithx's Avatar
    Join Date
    Sep 2007
    Posts
    587
    Ok. here's a hint. Try exchanging instructions 2 and 3 and see if it crashes now.
    Not everything that can be counted counts, and not everything that counts can be counted
    - Albert Einstein.


    No programming language is perfect. There is not even a single best language; there are only languages well suited or perhaps poorly suited for particular purposes.
    - Herbert Mayer

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    93
    It doesnt crash, but I still cant see the problem. Im assuming it is due to the order in which things are being defined? Can k not be pointed to address 0?

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    39
    I would think that pointer can only hold addresses or NUll which is 0 so...I think that this

    statement

    j = *k;

    would be wrong...

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    39
    Sorry , I was wrong...above

    You get the error here

    Code:
    k = l;
    because the pointers have not been initialized yet...

    then as mentioned switching the two lines...


    Code:
    l = &i;
    k = l;
    Now initialization has happened thus is valid...

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    No, the error is in dereferencing a null pointer. There's nothing wrong with assigning NULL to a pointer.


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    Feb 2009
    Posts
    93
    Ive been looking at this again and I still dont understand why the error is occuring and its starting to bug me a bit.

    Is it that since k is set equal to l on line 2, and at that point l is equal to 0 (setting k to 0 too), even when l is set equal to the address of i on the next line down, k is still set to 0 from line 2 and isnt affected by line 3.

    Therefore when on line 4 when j is set equal to the pointer k, k is still being set equal to 0 memory address from line 2 and not infact being pointed to i?

  9. #9
    and the hat of copycat stevesmithx's Avatar
    Join Date
    Sep 2007
    Posts
    587
    Therefore when on line 4 when j is set equal to the pointer k, k is still being set equal to 0 memory address from line 2 and not infact being pointed to i?
    Yes. You observed correctly.
    As quzah noted, the crash is caused because of line 4 where you dereference a null pointer.
    Dereferencing a null pointer, results in undefined behavior which causes a segmentation fault and hence the crash.
    Pointer (computing) - Wikipedia, the free encyclopedia
    Not everything that can be counted counts, and not everything that counts can be counted
    - Albert Einstein.


    No programming language is perfect. There is not even a single best language; there are only languages well suited or perhaps poorly suited for particular purposes.
    - Herbert Mayer

  10. #10
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    477
    When the value of k is assigned to j, it contains zero. When you are dereferencing a pointer, you are getting the value at the address it contains. 0 is not valid address(Not in protected mode, at least) and that's your error.
    "What's up, Doc?"
    "'Up' is a relative concept. It has no intrinsic value."

  11. #11
    Registered User
    Join Date
    Feb 2009
    Posts
    93
    In the same way, is this why reversing line two and three fixes the problem, because then l is pointing to location i before equating k to l, therefore giving this:

    j = *k = l = i = 0

    (0 = integer 0 not address 0)

  12. #12
    and the hat of copycat stevesmithx's Avatar
    Join Date
    Sep 2007
    Posts
    587
    Quote Originally Posted by spadez View Post
    In the same way, is this why reversing line two and three fixes the problem, because then l is pointing to location i before equating k to l, therefore giving this:

    j = *k = l = i = 0

    (0 = integer 0 not address 0)
    Yes. I suggest using a debugger and trace out and see this for yourself.
    Not everything that can be counted counts, and not everything that counts can be counted
    - Albert Einstein.


    No programming language is perfect. There is not even a single best language; there are only languages well suited or perhaps poorly suited for particular purposes.
    - Herbert Mayer

  13. #13
    Registered User
    Join Date
    Feb 2009
    Posts
    93
    Thank you very much for the help. Ive tried it in the debugger and it seems to confirm these results.

    Would anyone mind explaining what the term "dereferencing a null pointer" actually means?

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by spadez
    Would anyone mind explaining what the term "dereferencing a null pointer" actually means?
    Suppose you have a pointer named p. *p is one way to dereference the pointer. So, if p is a null pointer, we can say that *p dereferences a null pointer. Another way would be to write say, p->foo, where foo is some member of the type that p is a pointer to.
    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

  15. #15
    Registered User
    Join Date
    Feb 2009
    Posts
    93
    I find this slightly confusing, isnt *p defining p as a pointer. In which case how is *p "dereferencing the pointer". I might need this dumbing down a little :s

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. a pointer to a function question..
    By transgalactic2 in forum C Programming
    Replies: 17
    Last Post: 10-21-2008, 11:47 AM
  2. Question about pointer arithmetic and types
    By brooksbp in forum C Programming
    Replies: 4
    Last Post: 08-22-2008, 01:53 PM
  3. Exam Question - Possible Mistake?
    By Richie T in forum C++ Programming
    Replies: 15
    Last Post: 05-08-2006, 03:44 PM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  5. general pointer & malloc question
    By jstn in forum C Programming
    Replies: 2
    Last Post: 05-14-2002, 09:51 AM