How does free know how much to free?

When you call malloc(), you specify the amount of memory to allocate. The amount of memory actually used is slightly more than this, and includes extra information that records (at least) how big the block is. You can’t (reliably) access that other information – and nor should you :-). When you call free(), it simply … Read more

enum to string in modern C++11 / C++14 / C++17 and future C++20

(The approach of the better_enums library) There is a way to do enum to string in current C++ that looks like this: ENUM(Channel, char, Red = 1, Green, Blue) // “Same as”: // enum class Channel : char { Red = 1, Green, Blue }; Usage: Channel c = Channel::_from_string(“Green”); // Channel::Green (2) c._to_string(); // … Read more

When is assembly faster than C? [closed]

Here is a real world example: Fixed point multiplies on old compilers. These don’t only come handy on devices without floating point, they shine when it comes to precision as they give you 32 bits of precision with a predictable error (float only has 23 bit and it’s harder to predict precision loss). i.e. uniform … Read more

Why are hexadecimal numbers prefixed with 0x?

Short story: The 0 tells the parser it’s dealing with a constant (and not an identifier/reserved word). Something is still needed to specify the number base: the x is an arbitrary choice. Long story: In the 60’s, the prevalent programming number systems were decimal and octal — mainframes had 12, 24 or 36 bits per … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)