Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to instruct C to not zero-initialize global arrays?

Tags:

c

gcc

embedded

c99

I'm writing an embedded application and almost all of my RAM is used by global byte-arrays. When my firmware boots it starts by overwriting the whole BSS section in RAM with zeroes, which is completely unnecessary in my case.

Is there some way I can instruct the compiler that it doesn't need to zero-initialize certain arrays? I know this can also be solved by declaring them as pointers, and using malloc(), but there are several reasons I want to avoid that.

like image 840
Maestro Avatar asked Feb 04 '13 11:02

Maestro


People also ask

Is global variables always initialized to zero in C?

Yes, all members of a are guaranteed to be initialised to 0. If an object that has static storage duration is not initialized explicitly, it is initialized implicitly as if every member that has arithmetic type were assigned 0 and every member that has pointer type were assigned a null pointer constant.

Are global arrays initialized in C?

They are created in the beginning of program execution and last till its end. Also, all the elements of these arrays are initialized to default values (zero for arithmetic types and NULL for pointers).

Are global structs initialized to 0?

Since globals and static structures have static storage duration, the answer is yes - they are zero initialized (pointers in the structure will be set to the NULL pointer value, which is usually zero bits, but strictly speaking doesn't need to be).

Can we initialize global variables in C?

In C language both the global and static variables must be initialized with constant values. This is because the values of these variables must be known before the execution starts. An error will be generated if the constant values are not provided for global and static variables.


2 Answers

The problem is that standard C enforces zero initialization of static objects. If the compiler skips it, it wouldn't conform to the C standard.

On embedded systems compilers there is usually a non-standard option "compact startup" or similar. When enabled, no initialization of static/global objects will occur at all, anywhere in the program. How to do this depends on your compiler, or in this case, on your gcc port.

If you mention which system you are using, someone might be able to provide a solution for that particular compiler port.

This means that any static/global (static storage duration) variable that you initialize explicitly will no longer be initialized. You will have to initialize it in runtime, that is, instead of static int x=1; you will have to write static int x; x=1;. It is rather common to write embedded C programs in this manner, to make them compatible with compilers where the static initialization is disabled.

like image 138
Lundin Avatar answered Sep 19 '22 05:09

Lundin


It turned out that the linker-script included in my toolchain has a special "noinit" section.

__attribute__ ((section (".noinit")))

/** Forces the compiler to not automatically zero the given global variable on startup, so that the current RAM contents is retained. Under most conditions this value will be random due to the behaviour of volatile memory once power is removed, but may be used in some specific circumstances, like the passing of values back after a system watchdog reset.

So all global variabeles marked with that attribute will not be zero-initialised during boot.

like image 45
Maestro Avatar answered Sep 18 '22 05:09

Maestro