Live data from Hacker News

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

youtube.com

51–60 of 199 posts

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

#51
post #42
post #31

Earlier quoted context omitted.

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!

use a malloc that has the extra saftey features, run under an interpreted C, and several other ways to handle it (sandbox program, emulator, etc). Changing the underlying C language should NOT be one of them.

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

#52
post #23

Earlier quoted context omitted.

If I'm understanding what you're saying correctly, the memory location with address 0 is actually a writable address, but with the value being used semantically to handle division by zero? It's kind of wild to me that would even something that's even allowed to be done manually, let alone required by a certain mode. Is this something provided for compatibility reasons that you'd have to opt into, or is it just someth…

Back in the day there were no protections. You could write to any address whether it was used by the CPU for interrupt vectors, part of the OS, hardware addresses, anything.

pre-MMU, unless using DEC box.

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

#54
post #14

Earlier quoted context omitted.

Not quite. (char *) 0 is the null pointer. The null pointer is not necessarily a binary all-zero. On some compilers in x86, the null pointer intentionally points to something which will cause a crash when written to.

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…

Regarding your PS, I used Borland's Turbo C++ 1.0, and I think you've forgotten that memory models existed. Honestly, that's a good nightmare to forget.

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

#55
post #42
post #31

Earlier quoted context omitted.

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!

I'd agree with that, to the extent that understanding how badly your tools can hurt you is an important thing to learn.

Consider how a surgeon would respond if told not to use a scalpel because of the risk of accidental injury when using a sharp tool.

We learn from our mistakes - to which I'd add, sometimes we can afford to make mistakes (home programs) and other times we can't (safety-critical code).

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

#56
post #13

Earlier quoted context omitted.

This is C code. Coder needs specifity hwo to align the char pointer to HW alignment expectations for memory address to prevent a segmentation fault.

0 is the only pointer value that's guaranteed to be aligned on all possible alignment boundaries, which is why it's so useful as a pointer in C.

Yes, but will still segfault if you try to load bits 2 - 9 with a load bits 1-8 command.

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

#57
post #13

Earlier quoted context omitted.

This is C code. Coder needs specifity hwo to align the char pointer to HW alignment expectations for memory address to prevent a segmentation fault.

0 is the only pointer value that's guaranteed to be aligned on all possible alignment boundaries, which is why it's so useful as a pointer in C.

Yes, but will still segfault if you try to load bits 2 - 9 with a load bits 1-8 command. something along the lines of halt catch fire command

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

#59
post #55
post #42

Earlier quoted context omitted.

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!

I'd agree with that, to the extent that understanding how badly your tools can hurt you is an important thing to learn. Consider how a surgeon would respond if told not to use a scalpel because of the risk of accidental injury when using a sharp tool. We learn from our mistakes - to which I'd add, sometimes we can afford to make mistakes (home programs) and other times we can't (safety-critical code).

Surgeon would absolutely use a tool that tells them when they're about to make something that's 99% of the time a mistake.

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

#60
post #54

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…

Regarding your PS, I used Borland's Turbo C++ 1.0, and I think you've forgotten that memory models existed. Honestly, that's a good nightmare to forget.

I hated that about DOS, real-mode and BC++. After about 6-8 months of that misery, installing linux and learning to write C code with GCC was the best thing that ever happened to me. I felt like an animal being released from a cage and into the wild.
Post reply on HN