Home > Blockchain >  One string is affecting the size, length and value of another one on C
One string is affecting the size, length and value of another one on C

Time:12-22

int main ()
{
    /*
    char a[] = "abc";
    printf("strlen(a): %li", strlen(a));
    printf("\nsizeof(a): %li", sizeof(a));
    */

    char b[3];
    printf("\nstrlen(b): %li", strlen(b));
    printf("\nsizeof(b): %li", sizeof(b));

    printf("\nb = ");
    puts(b);

return 0;
}

When I run the above code it outputs the following:

strlen(b): 1

sizeof(b): 3

b =

but if I undo the comment, it outputs:

strlen(a): 3

sizeof(a): 4

strlen(b): 6

sizeof(b): 3

b = ���abc

Why does this happens? I would appreciate a good in depth explanation about it principally and if possible a quick "fix" for it so I don't get this problem again.

I'm relatively a beginner in programming and C in general and based on what I learned until now, this shouldn't happen

thanks and sorry if I broke any rule from this website, I'm new here too!

CodePudding user response:

strlen(b) causes undefined behavior because the array b is not initialized. The contents of the array are therefore indeterminate. strlen may return a small number if there happens to be a null byte in the garbage contents of the array (acting as a null terminator), or a large number if there is no null byte in the array but there is one in memory adjacent to it (that happens not to crash when accessed), or it may segfault, or fail in some other unpredictable way. The particular misbehavior you observe can easily depend on the contents of other nearby memory and therefore be influenced by adding or removing other variables, or altering surrounding code in apparently unrelated ways.

puts(b) is similarly undefined behavior.

(Another bug: sizeof and strlen both return size_t, for which the correct printf format specifier is %zu, not %li which would be for long int.)

I would appreciate a good in depth explanation about it principally and if possible a quick "fix" for it so I don't get this problem again.

Do not attempt to read or use the contents of local variables that have not been initialized.

See also What happens to a declared, uninitialized variable in C? Does it have a value? and (Why) is using an uninitialized variable undefined behavior?.

If you enable compiler warnings, your compiler can warn you about some instances of this, e.g. gcc catches this example. Tools like valgrind can help too.

I'm relatively a beginner in programming and C in general and based on what I learned until now, this shouldn't happen

On the contrary, such behavior is extremely common in C. The C language does not guarantee any checks for bugs like this, and implementations generally don't provide them. You should get used to the possibility that the language will not stop you from doing something erroneous, and will instead misbehave in unpredictable ways (or worse, appear to work just fine for a while). As a result, when programming in C, you have to be much more careful and attentive to the language rules than when working with "safer" languages. It's a tough and unfriendly language for beginners.

  • Related