Live data from Hacker News

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

youtube.com

111–120 of 199 posts

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

#111

Kind of a tangent, but I've never seen that signature for main with char *apple at the end after char *envp. What's passed in there? Some googling let me know it's used on macOS, but every result was too generic to be helpful.

https://stackoverflow.com/questions/16765545/how-to-list-all...

https://stackoverflow.com/questions/29909115/are-there-any-o...

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

#112
post #106

To me this talk was rather confusing. This statement cannot occur in a well formed standard C or C++ program, and I don’t think that the speaker did a good job conveying this. They also took way too much time to get to the actual point. If the intention was to discuss the particularities of memory mapping on some popular systems, they should have used bitcast from the start.

> they should have used bitcast from the start.

There are hundreds of instances of (char)0=0; in github, and there are none of the bit_cast variant, so if your goal is to inform people how to read code, starting with the one humans might actually encounter in the wild makes sense.

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

#113

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.

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.

What’s the point of the LLM if you have to read the transcript anyway to check it did not hallucinate? Just read the damn transcript.

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

#114

I once used a (never executed) *(char*)0=0; to force GCC to generate async unwind tables for an otherwise leaf function that could throw from an inline asm statement. It was just an experiment never meant for production, but it was quite effective at doing exactly what I needed.

That sounds very interesting but i don't quite understand :-(

What were you trying to do and how did this work? I am interested in understanding the interplay between -funwind-tables, -fasyncronous-unwind-tables and -fexceptions.

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

#115

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…

That's c++

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

#116

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.

What’s the point of the LLM if you have to read the transcript anyway to check it did not hallucinate? Just read the damn transcript.

I ask it to quote the section and it helps to find. But if you prefer reading the transcript that’s fine too. It’s not a commandment. It’s my sharing what works for me.

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

#117

A good "under the hood" presentation of going down the rabbit-hole of layers in a Computer System from Language -> Compiler -> OS -> Hardware.

http://www.gnu.org/fun/jokes/paging.game.html

That's not just a beautiful high-level description, it's even therapeutic: the next time my HDD is thrashing I'll sit patiently and think that the Page King is too busy ruling his kingdom to grant my humble request right now.

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

#118
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…

You're mostly correct but to be pedantic, in C the literal value 0 does not actually mean address 0. The literal value of 0 is an implementation defined value that represents a null pointer, but that value does not have to be address 0, it could be some other address.

This is significant because the following two snippets of code are not required to be equal in C.

char* c = (char*)(0);

char* d;

memset(&d, 0, sizeof(d));

c == d; // this does not have to be true

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

#119
post #91
post #76

Earlier quoted context omitted.

> 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.

Hilariously I've been down voted, even though you absolutely need sizeof (char) because it's a type. Given char x; sizeof x is fine. I know sizeof is an operator. I've been using C for 40 years.

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

#120
post #118
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…

You're mostly correct but to be pedantic, in C the literal value 0 does not actually mean address 0. The literal value of 0 is an implementation defined value that represents a null pointer, but that value does not have to be address 0, it could be some other address. This is significant because the following two snippets of code are not required to be equal in C. char* c = (char*)(0); char* d; memset(&d, 0, sizeof(d…

> that value does not have to be address 0, it could be some other address.

That's what the spec says, but are there any computers around anymore where nullptr is not zero? The spec should really go with the times IMHO, old hardware can be supported via platform-specific language extensions.

Post reply on HN