2

C traps and pitfalls 2.1 I thought 0 is always invalid address. How could he put a function in that position?

lzam
  • 21
  • 2

3 Answers3

5

It's architecture dependent.

From the book:

I once talked to someone who was writing a C program that was going to run stand-alone in a small microprocessor (answer right here). When this machine was switched on, the hardware would call the subroutine whose address was stored in location 0. In order to simulate turning power on, we had to devise a C statement that would call this subroutine explicitly. After some thought, we came up with the following:

(*(void(*)())0)();
Karoly Horvath
  • 94,607
  • 11
  • 117
  • 176
  • But 0 is always invalid. I got confused – lzam Mar 07 '14 at 11:23
  • that's not true, *as I said*, it's architecture dependent. for example, it was valid on my C64: http://sta.c64.org/cbm64mem.html – Karoly Horvath Mar 07 '14 at 11:34
  • your *intuition* is right though. it's not a good idea to allow it, as it prevents catching null pointer dereference. – Karoly Horvath Mar 07 '14 at 11:42
  • 0 is not always invalid, just like it is not always the address component (i mean where it points to) of a null pointer. null pointer is something whose address is never equal to address of something else. (like a function or an object. standard explains it much nicer, however i am too lazy to find it now). So there might be some systems where 0 is perfectly valid address and null is just something else (it could be anything). by the way allowing access to address 0 not necessarily prevents catching null pointer dereference. – Hayri Uğur Koltuk Mar 07 '14 at 11:56
  • As an aside, jumping to the restart vector does not, in general, simulate power-on as the peripheral devices do not get reset to their power-on state. It's more usual, with modern controllers, to force a watchdog timer reset which does perform peripheral device initialization. – Martin James Mar 07 '14 at 12:50
1

For microprocessors/microcontrollers, you have raw access to any RAM/Flash Address unless prohibited in hardware. Therefore accessing address 0 in microprocessor is completely vaild.

0xF1
  • 6,046
  • 2
  • 27
  • 50
0

I think that (* (void (*)()) 0) means that it is trying to invoke a function that is located in memory at address 0x00000000(which probably is an invalid address)

A very similar question on stackoverflow What does this C statement mean? may help

Community
  • 1
  • 1
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
  • 2
    There's no invocation, that would require more `()`. – CB Bailey Mar 07 '14 at 11:16
  • @CharlesBailey:- Yes actually Karoly explained it, its C vs C++ strikes again: in C++, an empty parameter list means the function takes no arguments; in C, that's only the case for definitions - in general, an empty parameter list means that the function takes an unspecified number of non-variadic arguments subject to default argument promotion. Kindly do correct me if I am missing something – Rahul Tripathi Mar 07 '14 at 11:23
  • eh..I am so sorry that i missed the ()...my question is that i thought 0 is invalid – lzam Mar 07 '14 at 11:28