Live data from Hacker News

*(char*)0 = 0; – What does the C++ programmer intend with this code? [video]

youtube.com

41–50 of 199 posts

Re: *(char*)0 = 0; – What does the C++ programmer intend with this code? [video]

#41

Old school comment... back in the day, there were computers on which dereferencing the null pointer would in fact return zero. And given that a typical linked list is terminated by a null, mistakes could creep in where "points to a 0" was checked when "pointer is 0" should have been. Needless to say, code like this would crash in mysterious ways when compiled on a machine where location 0, while readable, had nonzero…

IBM AIX is a Unix-like system on which dereferencing a null pointer for a read is guaranteed to return zero. But writes will crash. It’s pretty weird.

Re: *(char*)0 = 0; – What does the C++ programmer intend with this code? [video]

#42
post #31
post #25

When I see that code my head immediately translates to "store a byte with value 0 at memory address zero". 0 the integer needs to be cast to a char * to treat it as a byte address (rather than a word) and the * means "assign to the memory location given by the pointer. I would probably use something like this when hacking on an Apple II with a zero page. I have also written programs that ran on VMS which dumped memor…

Exactly so [0]. However, these days something or someone somewhere might object "to keep us safe from ourselves" Turbo-C used to check the zero location to see if it had changed and would issue a warning if so (in the real mode days). [0] My other computer is a PDP-11,

You say that as if that's a bad thing – I absolutely need to kept safe from myself, because I need all the help I need to make sure I'm doing things right!

Re: *(char*)0 = 0; – What does the C++ programmer intend with this code? [video]

#44
post #33

Test if system is VAX?

It wouldn't work, because in the early days of 4.xbsd, whether page 0 was mapped or not depended on linker options. Originally page 0 was mapped, this led to problems with people being sloppy with null pointers, so the default was changed (so that dereferencing a null pointer, whether reading or writing, would trap). But there was an option o map page 0 to get crappy/broken programs to work.

Re: *(char*)0 = 0; – What does the C++ programmer intend with this code? [video]

#46
This was a lot of fun. Reminded me of one of the earliest C programs I wrote when I was learning to code on Win98, before I really knew how computers worked. I was curious: "What does the rest of RAM look like?"

  #include 
  int main() {
     for (int x = 0;;x++) {
        putc(*(char*)x);
     }
     return 0;
   }
I was mezmerized by the strings from the uncompressed BIOS ROM being dumped back at me, "American Megatrends".. etc. Eventually this process crashes of course, when it runs past the end of mapped memory locations.

Re: *(char*)0 = 0; – What does the C++ programmer intend with this code? [video]

#47

This was a lot of fun. Reminded me of one of the earliest C programs I wrote when I was learning to code on Win98, before I really knew how computers worked. I was curious: "What does the rest of RAM look like?" #include int main() { for (int x = 0;;x++) { putc(*(char*)x); } return 0; } I was mezmerized by the strings from the uncompressed BIOS ROM being dumped back at me, "American Megatrends".. etc. Eventually this…

What's even more fun is the opposite! memset(0, 0, 1 << 20);

Re: *(char*)0 = 0; – What does the C++ programmer intend with this code? [video]

#48
post #15

Seg fault!

Not if you map it first.

I believe Linux (only in later versions; and a few other OSes) make this page unmappable[1].

That, of course, would be part of the talk: that this is perhaps an OS-specific feature that the original coder was maybe trying to trigger, and that it is still OS-dependent.

[1]: https://en.wikipedia.org/wiki/Zero_page states this to be the case, but mmap(2) seems to disagree / suggests 0 is mappable.

Re: *(char*)0 = 0; – What does the C++ programmer intend with this code? [video]

#49

Earlier quoted context omitted.

Find me one contemporary example (ANSI C) with a disassembled screenshot. This is writing sizeof(char) (== 1 almost everywhere) zero to address zero. It is not using a NULL macro or other predefined symbol. In the real world, this would generally write a byte to address 0000:0000, leading to UB because it would fuck up the divide-by-zero IV. PS: I used Borland C++ 3.1, Microsoft C++ 3.x and 4.5x, Watcom, and early GN…

> Find me one contemporary example (ANSI C) with a disassembled screenshot. Here in godbolt, clang compiling C simply deletes the code in the function past and including the null pointer dereference. https://godbolt.org/z/9aqWPazsP > This is writing sizeof(char) (== 1 almost everywhere) 1 everywhere. sizeof's unit is "how many chars". For instance there was a cray machine that could only access 64bit words. sizeof(ch…

make it a '*(volatile char*)0 = 0' to force the store.

Re: *(char*)0 = 0; – What does the C++ programmer intend with this code? [video]

#50
post #25

When I see that code my head immediately translates to "store a byte with value 0 at memory address zero". 0 the integer needs to be cast to a char * to treat it as a byte address (rather than a word) and the * means "assign to the memory location given by the pointer. I would probably use something like this when hacking on an Apple II with a zero page. I have also written programs that ran on VMS which dumped memor…

yes, compiler will align integers on word boundaries. casting to char, just uses the first 8 bits of integer word. Char was originaly just sematic macro for integer. pointer to char is 8 sequenial bits somewhere in the group of integer spaced bits. if you're lucky, the 8 bits are at the start of the integer group of bits, otherwise, segfault.

Originaly using anonymous unions & placing largest bit count variable as first item in union was the context needed to align the "char" correctly -- vs. casting.

Technically, can point at anything in an unaligned manner, just need to align access to machines address boundary to use a "fetch the value of a complete valid memory alignment address". One can hand roll the appropriate shifts/masking to get at the set of bits loaded (which usually let compiler do). Awk and settable end of line marker good way to safely visualize this.

Post reply on HN