mercredi 9 mars 2022

Why there is a pattern on the garbage values in the C program?

I try this program in CS50 Week 4 memory. I noticed there is a pattern on the garbage values. Initially, I thought garbage values should be randomize. But, it looks like there is different pattern depended on the number of garbage value requested.

Code: #include <stdio.h> #include <stdlib.h>

int main (void)
{
    //Create an array of 3
    int scores[3];

    //Print 3 random garbage values
    //scores[0] have big random values
    //scores[1] have 3276X
    //scores[2] have 0
    for (int i = 0; i < 3; i++)
    {
        printf("%i\n", scores[i]);
    }
}

Result: pset4/W4.1/ $ ./garbage -1498813296 32767 0 pset4/W4.1/ $ ./garbage -1011161520 32764 0 pset4/W4.1/ $ ./garbage 1340521040 32765 0 pset4/W4.1/ $ ./garbage 1244491248 32765 0 pset4/W4.1/ $ ./garbage -1200874656 32764 0

Can anyone help to explain what is happening here? Thanks!

Aucun commentaire:

Enregistrer un commentaire