Search code examples
clinuxlinux-kernelscheduled-tasks

In C language, if the array members is not assigned a value when they are initialized, is the initial value random?


When I was reading the source code/kernel/sched.c of the Linux 0.96a version, I found that the averunnable array was not assigned a value when it was initialized, so I wondered whether this would cause its array members'value becomes a random number when it is used. source link: https://elixir.bootlin.com/linux/0.96a/source/kernel/sched.c line 355: unsigned long averunnable[3]; /* fixed point numbers */

  1. I tried to read the relevant source code, but I didn't find where this averunnable array is assigned a value.
  2. I tried to run this code segment and found that when this array member is used, the initial value is random.
# cat testaverunnable.c
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>

int main()
{

#define FSHIFT  11
#define FSCALE  (1<<FSHIFT)
/*
 * Constants for averages over 1, 5, and 15 minutes
 * when sampling at 5 second intervals.
 */
static unsigned long cexp[3] = {
        1884,   /* 0.9200444146293232 * FSCALE,  exp(-1/12) */
        2014,   /* 0.9834714538216174 * FSCALE,  exp(-1/60) */
        2037,   /* 0.9944598480048967 * FSCALE,  exp(-1/180) */
};
unsigned long averunnable[3];   /* fixed point numbers */

        int i, n=10;
        printf("before into for cycle, the averunnable [%u] is %u \n",i, averunnable[i]);

        for (i = 0; i < 3; ++i)
                {
                        printf("averunnable [%u] is %u \n",i, averunnable[i]);
                        averunnable[i] = (cexp[i] * averunnable[i] + n * FSCALE * (FSCALE - cexp[i])) >> FSHIFT;
                        printf("atfer calculation, the averunnable [%u] is %u \n",i, averunnable[i]);
                }

    return 0;
}
# gcc -o testaverunnable testaverunnable.c
# ./testaverunnable
before into for cycle, the averunnable [0] is 4195856 
averunnable [0] is 4195856 
atfer calculation, the averunnable [0] is 3861499 
averunnable [1] is 4195392 
atfer calculation, the averunnable [1] is 4126081 
averunnable [2] is 3756023344 
atfer calculation, the averunnable [2] is 3805055516 

This is the Linus kernel, so I think I must have overlooked something, please help me, thanks!


Solution

  • Variables declared inside of a function that are not explicitly initialized have indeterminate values. Reading a variable with an indeterminate value can lead to undefined behavior. Such behavior can manifest as reading "garbage" values, though any value including 0 can be considered "garbage", and it's also possible that subsequent reads can result in seeing different values.

    However, the Linux kernel code you referenced, unlike your sample code, does not declare averunnable inside of a function, but instead declares it at file scope. For variables with static storage duration, which include variables declared at file scope, they are initialized to 0 (for numeric types) or NULL (for pointer types) if not explicitly initialized.