Indexing an `unsigned long` variable and printing the result

As a preface, this program will not necessarily run exactly like how it does in the question as it exhibits implementation-defined behavior. In addition to this, tweaking the program slightly can cause undefined behavior as well. More information on this at the end. The first line of the main function defines an unsigned long foo … Read more

Why should I use reference variables at all? [closed]

The problem is not related to references itself. The problem is that in C++, object lifetime is managed differently than in Java or other run-time environments that use a garbage collector. C++ doesn’t have standard built-in garbage collector. C++ object lifetime can be automatic (within local or global scope) or manual (explicitly allocated/deallocated in heap). … Read more

Function pointers in C – nature and usage

Why should anyone compare function pointers? Here’s one example: #include <stdbool.h> /* * Register a function to be executed on event. A function may only be registered once. * Input: * arg – function pointer * Returns: * true on successful registration, false if the function is already registered. */ bool register_function_for_event(void (*arg)(void)); /* * … Read more

Passing by reference and value in Go to functions

First, Go technically has only pass-by-value. When passing a pointer to an object, you’re passing a pointer by value, not passing an object by reference. The difference is subtle but occasionally relevant. For example, you can overwrite the pointer value which has no impact on the caller, as opposed to dereferencing it and overwriting the … Read more

Why can’t a constant pointer be a constant expression?

It’s a bit more complicated. In C++03 and C++11, &var is a constant expression if var is a local static / class static or namespace scope variable. This is called an address constant expression. Initializing a class static or namespace scope pointer variable with that constant expression is guaranteed to be done before any code … Read more