Unexpected output of printf

You are passing a float for the %d format string, but printf is expecting an int. printf is a variable argument list function: printf(const char *format_string,…); This means that the rest of the arguments after the format string can be of any type and number, and the compiler doesn’t know what they should be. It … Read more

Why Doesn’t reinterpret_cast Force copy_n for Casts between Same-Sized Types?

why doesn’t reinterpret_cast handle that for me? One reason is that the size, alignment, and bit representations aren’t specified, so such a conversion wouldn’t be portable. However, that wouldn’t really justify making the behaviour undefined, just implementation-defined. By making it undefined, the compiler is allowed to assume that expressions of unrelated types don’t access the … Read more

How can I cast an int to a bit in MySQL 5.1?

You cannot! CAST and CONVERT only work to: BINARY[(N)] CHAR[(N)] DATE DATETIME DECIMAL[(M[,D])] SIGNED [INTEGER] TIME UNSIGNED [INTEGER] No room for: BIT, BITINT, TINYINT, MEDIUMINT, BIGINT, SMALLINT, … However, you can create your own function cast_to_bit(n): DELIMITER $$ CREATE FUNCTION cast_to_bit (N INT) RETURNS bit(1) BEGIN RETURN N; END To try it yourself, you can … Read more

Is it OK to use C-style cast for built-in types?

Can I use C-style casting for built-in types like long x=(long)y; or it’s still considered bad and dangerous? Don’t use them, ever. The reasons against using them applies here as well. Basically, once you use them, all bets are off because the compiler won’t help you any more. While this is more dangerous for pointers … Read more

Mis-aligned pointers on x86

The situations are uncommon where unaligned access will cause problems on an x86 (beyond having the memory access take longer). Here are some of the ones I’ve heard about: You might not count this as x86 issue, but SSE operations benefit from alignment. Aligned data can be used as a memory source operand to save … Read more

No function matches the given name and argument types

Your function has a couple of smallint parameters. But in the call, you are using numeric literals that are presumed to be type integer. A string literal or string constant (‘123’) is not typed immediately. It remains type “unknown” until assigned or cast explicitly. However, a numeric literal or numeric constant is typed immediately. The … Read more

Why is “operator void” not invoked with cast syntax?

The technical reason why is found in ยง12.3.2: A conversion function is never used to convert a (possibly cv-qualified) object to the (possibly cv-qualified) same object type (or a reference to it), to a (possibly cv-qualified) base class of that type (or a reference to it), or to (possibly cv-qualified) void. The rationale is (likely) … Read more

Lexical cast from string to type

I like using locate, which works on built-in types: >>> from pydoc import locate >>> locate(‘int’) <type ‘int’> >>> t = locate(‘int’) >>> t(‘1’) 1 …as well as anything it can find in the path: >>> locate(‘datetime.date’) <type ‘datetime.date’> >>> d = locate(‘datetime.date’) >>> d(2015, 4, 23) datetime.date(2015, 4, 23) …including your custom types: >>> … Read more