Thread: What this exactly means?

  1. #1
    Registered User
    Join Date
    Sep 2013
    Posts
    13

    What this exactly means?

    Hello, I'm new here

    I have a question about this statement:

    *(int *)0 = 0;

    Could anybody explain what this snippets exactly does? And why it compiles but gives a segfault when executing?

    Thanks a lot in advance!

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    it is equivalent to the following code
    Code:
    int * pTemp = NULL;
    *pTemp = 0;
    Could you see why it segfaults?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Sep 2013
    Posts
    13
    Becouse you are trying so set the value 0 in an unknown memory address?

  4. #4
    Registered User
    Join Date
    Sep 2013
    Posts
    13
    But I've never seen that snippet before so i thought it was malformed too..but C compiler get it right :/ I thought it wouldn't either compile, why that statement is "syntatically" correct by the way?

    EDIT:

    Oh...0 is seen as NULL by C compilers?! (i'm using gcc)

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    0 is a null pointer constant.
    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

  6. #6
    Registered User
    Join Date
    Sep 2013
    Posts
    13
    Quote Originally Posted by laserlight View Post
    0 is a null pointer constant.
    so 0 is equal to:

    int *ptr = NULL

    ?

    and if I do:

    int *ptr = (int *)0;

    is perfectly correct?
    Last edited by riemann; 09-08-2013 at 11:37 AM.

  7. #7
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    It is correct but it means the address stored is the NULL address. If you want to actually set the value then you allocate and dreference. But yes, what you wrote is logically equivalent.

  8. #8
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Next time post your code in [code]/*your program*/[/code].
    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

  9. #9
    Registered User
    Join Date
    Sep 2013
    Posts
    13
    Quote Originally Posted by std10093 View Post
    Next time post your code in [code]/*your program*/[/code].
    Sure, sorry for that!

  10. #10
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by riemann View Post
    Sure, sorry for that!
    No problem, since you are new.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what does it means?
    By enakta13 in forum C Programming
    Replies: 1
    Last Post: 09-27-2012, 12:55 PM
  2. what does ROL means
    By bubblegum in forum C Programming
    Replies: 0
    Last Post: 03-08-2008, 09:20 AM
  3. What does the following means?
    By EeeK in forum C Programming
    Replies: 2
    Last Post: 10-27-2003, 10:15 PM
  4. Anyone know what this means?
    By bigblack in forum C++ Programming
    Replies: 1
    Last Post: 04-15-2002, 07:19 PM
  5. Does anyone know what this means
    By killerasp in forum C++ Programming
    Replies: 3
    Last Post: 02-08-2002, 01:41 AM