RAII in C++ does not mean that all bytes are initialized, only the ones you want. In
struct Foo {
int header = 0;
float foo;
int data[64];
int footer;
// just to show that having an explicit constructor / dtor does not change anything
Foo(): footer{456} { }
~Foo() { }
};
only "header" and "footer" will be initialized: https://gcc.godbolt.org/z/4W1WzfPEvIt's not a matter of optimization, absolutely no compiler will zero-initialize unless you explicitely ask for it (or you are in a situation where the language mandates initialization, like global static in C and C++)
Oh man I'm about to ruin your day. Behold the horror:
https://stackoverflow.com/questions/29765961/default-value-a...
Thing I keep coming back to is the proportion of effort you have to spend learning and keeping up with your tools vs the problem domain your working on. Feels like a big problem with C++ is how much energy the language itself uses up.
A friend of mine is, unlike me, really really smart. He likes golang and hates C++. And not like he hasn't spent years professionally writing C++ code. I think the advantage of golang is he can write it reflexively. So all of his attention is on the problem not the language.
Me I feel like the problem with C is not so much the language. It's that I'm always worried about stepping off a ledge. Things like defer would help. Because failing to clean up resources is a big problem with C.
I think most of my functions that allocate a resource only release it on error; otherwise the allocated resource lives on and gets freed later by another function. Defer doesn't help at all here. It just helps with the trivial ones..
The SO post looks confused ; A, B, D, E and F are the exact same case wrt the initialization of the int. It does not matter that there's a constructor or not and which shape it has, only that the variables get initialization somewhere or not (and that can be in the ctor's member init list, in the struct définition or even when creating a value if aggregate-initializing).
While the first draft was released in 1983, the first ratified C standard was in 1989.
The first edition of "The C Programming Language" (from which I first learned the language) was published in 1978, over a decade before the language was standardized.
You are correct that C89 added function prototypes. However, contrary to your implication, C borrowed the concept and syntax from C++.
There's a notable difference between C's prototypes and C++'s ... `int foo();` in C is not a prototype, whereas in C++ it's equivalent to `int foo(void);` ... C had to maintain compatibility with K&R style declarations, whereas C++ didn't.