Skip to content

Assignment Discards Volatile Qualifier 105

transfer of control bypasses initialization of:


int main(void){ int choice = 1; int z =1; switch(choice) { case 1: int y = 1; z = y + z; break; case 2: break; } return 0;

In the example, is an initialized variable that is in scope (but unused) in the other cases.

The C++ Standard says in section 6.7:

"It is possible to transfer into a block, but not in a way that bypasses declarations with initialization. A program that jumps from a point where a local variable with automatic storage duration is not in scope to a point where it is in scope is ill-formed unless the variable has POD type (3.9) and is declared without an initializer (8.5)."


The transfer from the condition of a switch statement to a case label is considered a jump in this respect.

The usual way to fix this is to enclose the case that declares in braces:

case 1: { int y = 1; z = y + z; } break;

Because is a POD (Plain Old Data) type, so an alternative is to not use initialization:

case 1: int y; y = 1; z = y + z; break;

General advice is not to use for hardware peripheral registers or qualified objects in general, even iff they occupy a gap-less memory region. They typically need a specific access pattern does not guarantee. This includes using optimised wider transfers, accessing the same location multiple times or changing the order of accesses.

For the reasons above and the following, don't even think about casting away the qualifier! The compiler might very well optimize the call away (e.g. if you have two identical calls without changes to source nor destination area in-between), combine two accesses or move the call before/after other hardware accesses.

Instead write your own copy function/loop keeping the qualifier. That will force the compiler to generate code which exactly does what you want. Remember to use the correct type for the copy pointers. Note also that the standard integer types are not a good choice for hardware registers of a specific size. Use the fixed-width types from like , , ... instead.