Static variables

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Static variables

Maxime Villard
Hi,
is it normal that in some functions like

        tc_ticktock(void)
        {
                static int count;
       
                if (++count < tc_tick)
                        return;
                count = 0;
                tc_windup();
        }

the static variables are not initialized?

- kern_tc.c l.547
- kern_resource.c l.499
- kern_sched.c l.106
- subr_extent.c l.130
- ...

Reply | Threaded
Open this post in threaded view
|

Re: Static variables

Franco Fichtner-2
Hi Maxime,

On Jul 8, 2013, at 10:40 AM, Maxime Villard <[hidden email]> wrote:

> the static variables are not initialized?

Static variables are always zeroed when not specified otherwise.


Regards,
Franco

Reply | Threaded
Open this post in threaded view
|

Re: Static variables

Maxime Villard
Le 08/07/2013 11:00, Franco Fichtner a écrit :

> Hi Maxime,
>
> On Jul 8, 2013, at 10:40 AM, Maxime Villard <[hidden email]> wrote:
>
>> the static variables are not initialized?
>
> Static variables are always zeroed when not specified otherwise.
>
>
> Regards,
> Franco
>
>

Ah, yes. I didn't know.

Reply | Threaded
Open this post in threaded view
|

Re: Static variables

Matthew Dempsky-3
On Mon, Jul 8, 2013 at 2:06 AM, Maxime Villard <[hidden email]> wrote:
> Ah, yes. I didn't know.

For what it's worth, this is specified in C99 §6.7.8 (Initializaton)
paragraph 10:

"If an object that has static storage duration is not initialized
explicitly, then:
— if it has pointer type, it is initialized to a null pointer;
— if it has arithmetic type, it is initialized to (positive or unsigned) zero;
— if it is an aggregate, every member is initialized (recursively)
according to these rules;
— if it is a union, the first named member is initialized (recursively)
according to these rules."

On OpenBSD (and most, if not all, ELF platforms), this is implemented
by assigning these objects into the .bss section, which is initialized
to all zero bytes at program startup, taking advantage of the fact
that all of our platforms represent null pointers and zero values as
sequences of zero bytes.