0

I saw a joke today that went like this:

The following line of code will make every C Program run faster:

*((int *)0) = 5;

I don't really get what's going on here. It looks like they're casting 0 to be an int pointer, then making the pointer to that 5...?

What does this line of code do?

Anubian Noob
  • 13,426
  • 6
  • 53
  • 75
  • 1
    This would produce a segmentation fault I'm pretty sure. – squiguy May 16 '15 at 22:30
  • 7
    It's true that this will make any program *finish sooner*. But faster? Not really. – Greg Hewgill May 16 '15 at 22:31
  • How does this cause a segfault? – Anubian Noob May 16 '15 at 22:32
  • Dereferencing 0. Usually not a good idea. – Benjy Kessler May 16 '15 at 22:32
  • 3
    I've been a C programmer for something like 30 years and I can't see how that's humorous in any way... Are you sure that's the entire joke? – David Hoelzer May 16 '15 at 22:37
  • **Noob** humor -- no pun intended... – David C. Rankin May 16 '15 at 22:52
  • Although 0 is intended as NULL, so that a lot of people uses the notation `!ptr` instead of `ptr==NULL` (that in some older compilers for MCU - which I used - is an error because NULL is different from 0), is not forbidden to access the address 0. You have to imagine that you are programming an MCU without OS ... But I don't know that exist an environment where if you write 5 in the memory cell 0 you speed up the code, unless this memory cell is a hardwired clock divisor acting on the CPU and normally its value is greater than 5 ... ;) – Sir Jo Black May 16 '15 at 22:53
  • 1
    Undefined behavior ... attempting to write the value 5 into memory at address 0 (NULL) and this virtual address 0 may not be mapped to any physical address. – iammowgli May 16 '15 at 22:55
  • @iammowgli That'd be a great answer. – Anubian Noob May 16 '15 at 22:56
  • 1
    The other half of the joke is that if you switch the machine off at the wall, the failure is OS-independent. – Weather Vane May 16 '15 at 22:58
  • 1
    It depends from the CPU and from the SO; if you writes 5 in the memory cell 0 of an 8086 in REAL MODE you modify the contents of the interrupt 0 vector! ;) – Sir Jo Black May 16 '15 at 23:01
  • 1
    The question that I want to emphasize is that using the memory cell 0 is not claimable as undefined behavior if the programmer knows what he's doing! :) – Sir Jo Black May 17 '15 at 02:41

2 Answers2

2

This is undefined behavior, most likely causing the program to crash. Hence, terminating the program, and making it "faster".

Evdzhan Mustafa
  • 3,645
  • 1
  • 24
  • 40
  • @AnubianNoob The memory block, with address "0", is probably not a valid memory address. The piece of code is trying to dereference it, and then write 5 to it. – Evdzhan Mustafa May 16 '15 at 22:35
  • Why should be an error writing the cell memory 0? It depends from the CPU and from the SO; if you writes 5 in the memory cell 0 of an 8086 in REAL MODE you modify the contents of the interrupt 0 vector! ;) – Sir Jo Black May 16 '15 at 23:05
  • @SergioFormiggini That's why I used the words "probably" and "most likely". – Evdzhan Mustafa May 16 '15 at 23:23
  • Ok, but there's somebody that is downvoting my opinion ... That's the classical behaviour of the "Computer" programmer who knows only system with protection and Operating Systems, but they never feel the smell of MCUs or BIOS programming. – Sir Jo Black May 17 '15 at 00:29
  • The problem that I want to emphasize is that using the memory cell 0 is not claimable as undefined behavior if the programmer knows what he's doing! :) – Sir Jo Black May 17 '15 at 02:40
  • Only if you don't understand what undefined behaviour is in C Sergio. The C standard is, among other things, is *the* authoritative dictionary of terms for describing anything to do with C. The C standard defines "undefined" as (essentially) "this standard specifies no constraints on what happens as a result" and specifies that dereferencing a NULL pointer gives undefined behaviour. Period. A programmer might know what s/he is doing (for a specific compiler, host system, and phase of the moon) but the behaviour is still undefined according to the C standard. – Peter May 17 '15 at 04:47
  • @SergioFormiggini "there's somebody that is downvoting my opinion" - what do you mean? Comments cannot be downvoted afaik. – user4520 May 17 '15 at 15:12
  • It's strange, but I had a downvote ... may be another reason, but I never answered this question!!! ... Don't worry ... be happy :) – Sir Jo Black May 17 '15 at 15:15
  • @Peter, I state that what you said is correct. But stating such a definition is an error to claim that a code containing an undefined behaviour is a bad code. That is what I mean, I feel that a lot of people indicates as an error the use of some undefined behaviour where, in some cases, is perfectly legal to use such code ... That of NULL is a specific and important case, because nobody has to state that is an error to read or to write the memory cell 0. My feeling is that a bug is a code such as `*(int *)(NULL)=5;` because it's a paradox!!! – Sir Jo Black May 17 '15 at 15:36
0

I think it's because address 0 is assigned to null value, it could be helpful to check Why is address zero used for the null pointer?.

Community
  • 1
  • 1
Dawid Bugajewski
  • 365
  • 1
  • 11
  • Comment as you downvote, as I'm new to stackoverflow I'd like to know why my replies are wrong. Thanks. – Dawid Bugajewski May 16 '15 at 22:52
  • Welcome to Stack Overflow! I'm assuming the downvote is because you didn't really address the question. You have the first step, but the next would be what the rest of the expression does. And then how that "speeds it up." – Anubian Noob May 16 '15 at 23:21
  • It's a little bit more then zero, `NULL` is defined like this - `#define NULL ((void*)0)` – James May 16 '15 at 23:25
  • 1
    @James If i understand correctly pointer is an address in a memory and the type just specifies the size of the expected unit therefore ((void*)0)==((int*)0) would return true; – Dawid Bugajewski May 16 '15 at 23:31
  • @DawidBugajewski So it does indeed return true, just pay no attention to me. – James May 16 '15 at 23:36