Earlier quoted context omitted.
I've only written a little of each, but my impression is that C has mostly remained a focused, effective language for careful systems programming. While C++ has become a sprawling cthulhu of leaky abstractions and other nasty foot-guns and people only use it because "it's as fast as C but sorta feels like a higher-level language". And that therefore Rust is a strict improvement over C++, but not necessarily C.
I thought this way too until I worked with competent C++ devs on a modern code base. (By my definition of “competent,” no one ever encountered by the Rust brigade bloggers is competent, fwiw). At some point, I realized that C++ is a language for implementing new languages in a way that is backward compatible with existing programs. Templates are Turing complete (lazily evaluated, purely functional, and side-effect fr…
Stop Memsetting Structures
211–220 of 277 posts
Re: Stop Memsetting Structures
#212Re: Stop Memsetting Structures
#213Last I checked C99 initializers require constants, so they don't generalize, whereas the pattern shown does. I'd prefer if the initializers pattern did allow variables on the right-hand side for this reason.
Re: Stop Memsetting Structures
#214memset() heads off the dreaded "I added more fields, but forgot to add the initializers everywhere" bugs. Well, it will still be a bug, but at least it won't be a heisenbug.
Re: Stop Memsetting Structures
#215Re: Stop Memsetting Structures
#216My favourite trick with designated initialisers is using them with array indices and enums, e.g. enum color {RED, GREEN, BLUE}; char *color_name[] = { [RED] = "Red", [GREEN] = "Green", [BLUE] = "Blue" }; printf("%s",color_name[RED]); This means you don't have to ensure ordering is the same between the enum and the array, which can be a pain for more complex tables. It's a shame C++ doesn't support this, more details:…
What you are implementing is a map. Just use std::map.
Re: Stop Memsetting Structures
#217Ugh, what do you do when the structure definition is updated and you now have uninitialized values? If there's no explicit initializer I think memset,calloc or ={0} is just fine thanks.
Re: Stop Memsetting Structures
#218There were a few old free implementations which misbehaved when given null. Maybe they are all gone now, but there was a reason to do it.
Re: Stop Memsetting Structures
#219My favourite trick with designated initialisers is using them with array indices and enums, e.g. enum color {RED, GREEN, BLUE}; char *color_name[] = { [RED] = "Red", [GREEN] = "Green", [BLUE] = "Blue" }; printf("%s",color_name[RED]); This means you don't have to ensure ordering is the same between the enum and the array, which can be a pain for more complex tables. It's a shame C++ doesn't support this, more details:…
Why would you ever want to do something like this in c++? What you are implementing is a map. Just use std::map.