Why does C++ allow variable length arrays that aren’t dynamically allocated?

Support for variable length arrays (VLAs) was added to the C language in C99. It’s likely that since support for them exists in gcc (to support C99), it was relatively straightforward to add support for them to g++. That said, it’s an implementation-specific language extension, and it’s not a good idea to use implementation-specific extensions … Read more

Why does std::cout convert volatile pointers to bool?

Up through the C++20 standard, ostream::operator<< has the following overloads, among others: ostream& operator<< (bool val ); ostream& operator<< (const void* val ); When you pass in a volatile pointer, the second overload can’t apply because volatile pointers cannot be converted to non-volatile without an explicit cast. However, any pointer can be converted to bool, … Read more

Java reflection: Is the order of class fields and methods standardized?

According to the documentation: getFields() Returns an array containing Field objects reflecting all the accessible public fields of the class or interface represented by this Class object. The elements in the array returned are not sorted and are not in any particular order. This method returns an array of length 0 if the class or … Read more

Is the compiler allowed to recycle freed pointer variables?

Upon an object reaching the end of its lifetime, all pointers to it become indeterminate. This applies to block-scope variables and to malloced memory just the same. The applicable clause is, in C11, 6.2.4:2. The lifetime of an object is the portion of program execution during which storage is guaranteed to be reserved for it. … Read more

Does constexpr imply inline?

Yes ([dcl.constexpr], ยง7.1.5/2 in the C++11 standard): “constexpr functions and constexpr constructors are implicitly inline (7.1.2).” Note, however, that the inline specifier really has very little (if any) effect upon whether a compiler is likely to expand a function inline or not. It does, however, affect the one definition rule, and from that perspective, the … Read more

tech