Live data from Hacker News

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

youtube.com

141–150 of 199 posts

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

#141
post #92

Earlier quoted context omitted.

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

Out of curiosity, how do you deal with I/O on such systems? ISO C simultaneously requires that blocks of memory roundtrip through binary I/O (so it can’t truncate chars to octets) and that fgetc() and friends return unsigned chars cast to an ints or EOF (so you really want the range of int to include the range of unsigned char, even if you could technically depend on the implementation-defined overflow behaviour). Th…

It depends entirely on the system as a whole, and it might help to start by forgetting about "ISO C" and start thinking about C the triangle.

* C preprocessor - it's a text substition operation that can be non standard

* C "the language" - just the syntax folks, no library functions here.

* C "the standard library" - for many of us the K&R stdlib was just a proof of concept example of how to code a library, feel free to ditch it and write up your own string handling, for example.

Which brings us to, say, a multi channel marine seismic processing system that has an IBM PC type design with a custom motherboard that has six TI DSP boards slotted in and you're writing code for the user interface to a real time signal aquisition and processing system and writing onboard code for the numerical processing on the DSP boards.

Now, each board handles a streamer cable, each cable has a number of microphones, an external boomer in the water is triggered and the reflective soundwaves from the ocean floor, and the soundwaves that penetrate the seafloor and later also partially reflected by density change layers, are all captured by the microphones.

You have keyboard+mouse I/O between the real time window manager user interface, shared memory I/O between the PC memory and memory on the DSP cards, analogue soundwaves going to sample ports on the DSP cards, block memory I/O going from the PC to a SEG-9 tape recorder, prepared lines of memory going to a plotter to build an image ...

There's a lot going on.

But, as far as the code compiled for the DSP boards, that mostly handles I/O by linking reaction code to interrupt triggers - when the sampling hardware interrupts to signal another bit of soundwave from microphone[i] is ready, that's stashed in a FiFo queue to be pushed into the DSP handling pipe and to be saved in a raw sample buffer.

When a raw sample buffer is full an interrupt is triggered to take the entire buffer via DMA transfer to be handled PC side by sending it to the SEG-9 tape "of (raw) record", when a processed sample buffer is full an interrupt is triggered for a different type of DMA transfer that takes processed data to display, printer, and to a different SEG-9 track for processed data.

Not much of this is the standard C library, so you can see why you might write your own low level handling.

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

#142
post #138

Earlier quoted context omitted.

Yet surgeons killed people all of the time and had to be forced to use checklists because it turns out credentialism doesn’t prevent mistakes.

Which is also something that we need to add as well, on top of credentialism. More quality and process validation, less cowboy programming.

And skip the credentialism, because that’s generally bullshit.

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

#143
post #138

Earlier quoted context omitted.

Which is also something that we need to add as well, on top of credentialism. More quality and process validation, less cowboy programming.

And skip the credentialism, because that’s generally bullshit.

Not at all, that is a guarantee that people actually know how to hold the scapel and can enter the operating room safely.

A brick layer doesn't turn into a Construction Engineer, only because they think they know everything about building houses, and have somehow built their own during long weekends.

Likewise a coder out of a bootcamp, isn't someone versed in what actually entails to be a Software Engineer, regarless how cool the title might sound like.

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

#144

Earlier quoted context omitted.

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.

To correctly handle exceptions GCC generates DWARF unwind tables that the runtime uses during unwinding to call the correct destructors but also restore registers and fixup the stack (i.e. the compensation code). Normally unwind tables have entries for each non-noexcept function call in a function as these are the only exception throwing edges. Asynchronous unwind also generates table entries for any instruction that…

Neat! Thanks for the detailed writeup.

I had long wanted to understand the interplay between -funwind-tables, -fasynchronous-unwind-tables and -fexceptions since the last is language specific but the first two are not. GCC docs are not of much help in understanding what exactly is going on in each case so i guess i need to do some research and experimentation.

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

#145
post #143

Earlier quoted context omitted.

And skip the credentialism, because that’s generally bullshit.

Not at all, that is a guarantee that people actually know how to hold the scapel and can enter the operating room safely. A brick layer doesn't turn into a Construction Engineer, only because they think they know everything about building houses, and have somehow built their own during long weekends. Likewise a coder out of a bootcamp, isn't someone versed in what actually entails to be a Software Engineer, regarless…

No, it’s not a guarantee, it’s just a noisy filter with a very high false negative rate and a high enough false positive rate (as visible in malpractice insurance).

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

#146

Earlier quoted context omitted.

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.

Fair enough. I was not criticising you, sorry if it sounded that way. I am just puzzled by instances where it seems an old fashioned approach (such as control-F in this case) would perform at least as well as fancy (and not always reliable) AI techniques.

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

#147
post #118

Earlier quoted context omitted.

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.

https://c-faq.com/null/machexamp.html

> Depending on the ``memory model'' in use, 8086-family processors (PC compatibles) may use 16-bit data pointers and 32-bit function pointers, or vice versa.

And it isn't just zero anyway because there's segmentation in play.

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

#148

Earlier quoted context omitted.

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.

By default you can only map addresses over 65536 (root can map anything though). But you can disable the protections with: echo 0 > /proc/sys/vm/mmap_min_addr

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

#149

Before I watch the full talk for the answer (what I've seen so far, he states there's not a right answer)... I know on some microcontrollers (e.g. Arm) addresses 0x0 and 0x4 are usually used to define the initial stack pointer and entry point and then are never needed again. Later, you probably want to detect the presence of null pointers by checking if &maybeAStruct is either 0 or valid. If you accidentally test the…

W.r.t. your Microsoft compiler testcase, your process does receive a Access Violation exception but because there are no registered handlers it is terminated silently. You can check the process exit code for confirmation.

For all the gory details see Unwinding the Stack: Exploring How C++ Exceptions Work on Windows - https://www.youtube.com/watch?v=COEv2kq_Ht8

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

#150

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.

> but it takes ages to go to the point

The point is the journey and the discussion around what the line of code does, and how that discussion is valuable. The point isn't to tell us what the code itself does.

Post reply on HN