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…
*(char*)0 = 0; – What does the C++ programmer intend with this code? [video]
41–50 of 199 posts
Re: *(char*)0 = 0; – What does the C++ programmer intend with this code? [video]
#42When 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,
Re: *(char*)0 = 0; – What does the C++ programmer intend with this code? [video]
#43Seg fault!
My objective was to test the crash reporter tool.
Re: *(char*)0 = 0; – What does the C++ programmer intend with this code? [video]
#44Test if system is VAX?
Re: *(char*)0 = 0; – What does the C++ programmer intend with this code? [video]
#45To test if the system has a functional MMU
Re: *(char*)0 = 0; – What does the C++ programmer intend with this code? [video]
#46 #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]
#47This 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…
Re: *(char*)0 = 0; – What does the C++ programmer intend with this code? [video]
#48Seg fault!
Not if you map it first.
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]
#49Earlier 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…
Re: *(char*)0 = 0; – What does the C++ programmer intend with this code? [video]
#50When 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…
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.