1 hour ago · Tech · 0 comments

Today, in weird C code tricks, I want to show you a small example from Git’s source code.Recently, I was poking around the Git source when a directory name caught my eye, it was named “compiler-tricks”. I thought, “This is a promising name, let’s see what’s inside of it”.Inside this directory, there was a file called not-constant.c, and the whole file contained just this:#include <git-compat-util.h> int false_but_the_compiler_does_not_know_it_ = 0;The variable name made me curious. Why would Git need a global variable whose name says that the value is false, but the compiler does not know it?To understand this, let’s first look at where this variable is used.It is used inside a macro called NOT_CONSTANT in git-compat-util.h:/* * Prevent an overly clever compiler from optimizing an expression * out, triggering a false positive when building with the * -Wunreachable-code option. false_but_the_compiler_does_not_know_it_ * is defined in a compilation unit separate from where the macro is…

No comments yet. Log in to reply on the Fediverse. Comments will appear here.