ChrisFNZ
ChrisFNZ

Reputation: 617

C - Newly declared array contaminated with values from other variables

I'm alarmed to see that a newly declared array is being contiminated with some random values and some partial values from other variables within my C program.

Here's the source code of my function. I'm basically writing some pseudo code in preparation for doing some complex XML parsing and file manipulation (think similar to a mail merge). Anyway I'm concerned if there are random values in my newly declared array. Why isn't it empty of values when I first declare it? Do I really need to traverse my entire array to set it's elements to blank values before I begin assigning values or is it likely that there's something wrong with other variable declarations in my code?

Thank you for your help.

Regards, Chris

    void ShowArray(void)
    {
            char aryString[5][5][255];

            sprintf(aryString[1][1],"AAAAA");
            sprintf(aryString[1][2],"BBBBB");
            sprintf(aryString[1][3],"CCCCC");
            sprintf(aryString[1][4],"DDDDD");
            sprintf(aryString[1][5],"EEEEE");
            sprintf(aryString[2][1],"A2");
            sprintf(aryString[2][2],"B2");
            int numRow;
            int numCol;
            for (numRow=1;numRow < 6;numRow++)
            {
                    for (numCol=1;numCol < 6;numCol++)
                            printf("%d,%d:%s\n", numRow, numCol,aryString[numRow][numCol]);
    }
}

Upvotes: 1

Views: 108

Answers (4)

nalzok
nalzok

Reputation: 16107

Yes, all auto(opposite to static, which is declared explicitly) variables you declare in a function calls for manual initialization. The compiler won't initialize it automatically because it don't know what do you want to be written to that memory. To make it write the default value, which is usually 00000000, to uninitialized variables, write char aryString[5][5][255] = {};, or more commonly, char aryString[5][5][255] = {0};.

Also, the value an uninitialized variable contains is not only a garbage value, but also likely a trap representation, and merely accessing it will cause undefined behavior.

Upvotes: 0

Abhishek Joshi
Abhishek Joshi

Reputation: 33

You are also writing to the [1][5]th string in your code with sprintf. Your aryString variable is of dimensions [5][5][255]. Remember that array indexing in C is 0-based. You should not go beyond the [1][4]th element. You might want to delete that line and try again, because you will end up corrupting your own data by yourself.

Upvotes: 1

SegFault
SegFault

Reputation: 2546

Unfortunately you have to initialise the values of every element in an array.

Having random values populating your array and variables when you first declare it is normal. This is because when your computer frees up memory, it doesn't reset them to zero. You computer just allows other programs to overwrite the values in those newly freed memory locations.

Those uninitiallized values are just leftovers from other functions.

Upvotes: 1

Tom Karzes
Tom Karzes

Reputation: 24052

A local variable in a function will have an initially undefined value. This is, in fact, what you want, since the alternative would be for the compiler to force an initialization that in most case you don't want, unavoidably slowing your function. It is your responsibility to ensure that any variable has been properly defined before trying to use its value. I have never found this to be a problem.

Upvotes: 1

Related Questions