I recently ran into some weird bugs due to this kind of code.
vector<int> a(a);
Why the code above is accepted? When is this necessary? And how to ask the compiler to forbid this kind of usage? Thank you.
The most common use case for that is actually in C, not C++ (you should not use malloc
in C++, although you can), but in this case C++ is backwards compatible:
mytype *p = malloc(sizeof *p);
By allowing the use of p
in the initializer expression, you can pass sizeof *p
to malloc
ensuring that whatever the size of mytype
is, the right size will be allocated. If you were not allowed to do *p
, then the expression above would have to have the type mytype
twice, and later maintenance in the code might update one of the types and fail to update the other.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With