简体   繁体   中英

why doesn´t char newWord[45]; have "clean" values at the start of a function like char newWord[45] = ""; does?

I´ma bit confused with these 2.

I have a function called check that does the following:

bool check(const char *word)
{
    char newWord[LENGTH + 1] = "";
    
    for (int i = 0; word[i]; i++)
    {
        newWord[i] = tolower(word[i]);
    }
}

Now for example if I use ="" , the variable newWord will have all of it´s values as '\\0' anytime I run the function check();

But when using char newWord[LENGTH + 1]; the variable seems to keep the old values even after my functions has returned, so when I do check() again, the char newWord already has values from the previous time I ran that function.

I know this is related to pointers and memory allocation but I just cannot seem to get how this works.

The values that you find after running check() twice are still garbage values. When you allocate some memory using

char newWord[LENGTH + 1];

You are always getting "some" memory that the operating system handles you (in laymen-terms), having initially garbage values. It is just a coincidence that you're getting the same memory blocks that you got from the previous call to check() .
However, when you do:

char newWord[LENGTH + 1] = "";

You are explicitly initializing those memory blocks to \\0 .

It's not a fancy answer, but compilers (and versions of compilers) have different opinions on whether to initialize memory before you use it. Unless things have changed recently, the only variables that get automatically initialized are variables with a static scope (global variables and those explicitly marked static ).

For everything else, certain compilers might set everything to zeroes (or another value for debugging), but most won't add that small overhead to your program, when you're probably just going to assign a value of your own, soon enough. One of the biggest debugging effort of my career was because we changed C compilers from one that didn't pre-initialize variables (like most do) to one that did, exposing a bunch of errors my predecessors didn't catch (assigning to the wrong variable, looking for non-zero values and suddenly finding them), so it's an important feature to know about.

The key to your question, though is " seems to keep," because it's only an accident that the old string is still in the right position. If you call another function between check() calls, you'll start to see different scratch memory.

Moral of the story? Always initialize every one of your variables, unless you absolutely know that it's going to get a value before you use it.

If you don't initialize a local variable, it has an indeterminate value . Which could be anything, including random "garbage" or left-overs sitting in RAM memory since previous execution. There are no guarantees of what value you will get - and that's it.

Accessing such an uninitialized variable's value might also invoke undefined behavior in some cases:
(Why) is using an uninitialized variable undefined behavior?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM