Live data from Hacker News

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

youtube.com

91–100 of 199 posts

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

#91
post #76

Earlier quoted context omitted.

> sizeof(char) (== 1 almost everywhere) sizeof char is 1 by definition everywhere. /pedantic

> sizeof char is 1 by definition everywhere. Parentheses are required around char because it's a type. /pedantic

That is incorrect :-).

sizeof is an operator in C, and does not need parenthesis any more than pointer operator *. It is true that programmers frequently think of it as a function and use parenthesis.

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

#92

Earlier quoted context omitted.

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 fi…

I see you've been lucky enough to only work on systems where CHAR_BIT=8. It's not an alignment issue because there may not be a distinct pointer representation for the other bits, rather than alignment issues where a pointer representation exists even if you can't use it.

Adding to the above, in the 1980s and 1990s it was not uncommon to interface and write code for chips that were 12 bit and 24 bit based (instrumentation accumulators for example, "cheapest sufficient" chips for particular jobs, etc).

Today you can still work with (say) TI DSP chips that spit complex FFT pipelines results once per cycle and have absolutely no 8-bit hardware addressing or masking abilities as they're lean mean RISC optimised machines for pure 32 or 64 bit floating point operations.

They have C compilers and CHAR_BIT==32 (or 64).

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

#93

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);

You took me to "Now we are done with RAM, let's play with HDD" moment on 80286 when I was a kid.

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

#94

Not a big fan of the format of the talk. The guy is a good orator, but it takes ages to go to the point. A bit annoying if you're actually interested in the technical part rather than the jokes.

You sound like fun at parties :p I’m glad you liked the oration and technical content though!

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

#95
post #29

Earlier quoted context omitted.

Not quite, I think. Since this is a char pointer being used, only the first byte of the interrupt address would be zeroed. Since in real mode those are far pointers, the lower byte of the segment would be zeroed. So xx00:xxxx. But yes, the interrupt table was my first thought when reading the headline.

Char can be the same size as short or int. You can't assume it is one byte.

Yeah, if you're going to be pedantic, check your facts, see the sibling. Since I'm assuming an 8086 interrupt table, I'm also going to assume 8-bit chars, as that's the x86 addressing model. And dereferencing a null pointer is UB, so you can't count on anything anyway without making further assumptions.

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

#96

Earlier quoted context omitted.

If you use GPT-4 with Browsing turned on and supply it with the title, it will find the page and use the transcript and you can ask it questions. Then you can look at the transcripts to find the spots and verify. Alternatively, if you have an AI-enabled browser you can ask it about the transcript once you open it.

I will do prompt injection in the next talk, just for you

I think that would be fun.

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

#97

Earlier quoted context omitted.

If you use GPT-4 with Browsing turned on and supply it with the title, it will find the page and use the transcript and you can ask it questions. Then you can look at the transcripts to find the spots and verify. Alternatively, if you have an AI-enabled browser you can ask it about the transcript once you open it.

I will do prompt injection in the next talk, just for you

Wait, you’re the presenter!

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

#98

This statement would be technically legal on its own in x86 real mode if the compiler didn't do null pointer checks. However it would set the divide-by-zero IRQ handler to itself 0000:0000, and when the next division by zero happened, the machine run into UB (likely a reset or halt) because it would jump there, do 4x ADD byte ptr [BX + SI], AL (or ADD byte ptr [EAX], AL) followed by running the remaining interrupt ve…

Good thing this was covered in the talk

Having it in text is much nicer than having it in video.

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

#99

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…

> sizeof(char) (== 1 almost everywhere) sizeof char is 1 by definition everywhere. /pedantic

In the mathematical sense of almost, a property that holds everywhere does qualify as holding almost everywhere.

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

#100

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…

It was great when virtually everything was mapped 1:1 into "real" memory, even video memory. In those days, everything was a memory address, just like "everything is a file".

Then came the realization you could alter any memory location, and further, you could write a tiny TSR to do things...

Post reply on HN