Why does gcc allow char array initialization with string literal larger than array?

匿名 (未验证) 提交于 2019-12-03 02:24:01

问题:

int main() {     char a[7] = "Network";     return 0; } 

A string literal in C is terminated internally with a nul character. So, the above code should give a compilation error since the actual length of the string literal Network is 8 and it cannot fit in a char[7] array.

However, gcc (even with -Wall) on Ubuntu compiles this code without any error or warning. Why does gcc allow this and not flag it as compilation error?

gcc only gives a warning (still no error!) when the char array size is smaller than the string literal. For example, it warns on:

char a[6] = "Network"; 

[Related] Visual C++ 2012 gives a compilation error for char a[7]:

1>d:\main.cpp(3): error C2117: 'a' : array bounds overflow 1> d:\main.cpp(3) : see declaration of 'a' 

回答1:

Initializing a char array with a string literal that is larger than it is fine in C, but wrong in C++. That explains the difference in behavior between gcc and VC++.

You would get no error if you compiled the same as a C file with VC++. And you would get an error if you compiled it as a C++ file with g++.

The C standard says:

[...]

EXAMPLE 8

The declaration

char s[] = "abc", t[3] = "abc"; 

defines ‘‘plain’’ char array objects s and t whose elements are initialized with character string literals. This declaration is identical to

char s[] = { 'a', 'b', 'c', '\0' },      t[] = { 'a', 'b', 'c' }; 

(Section 6.7.9 of the C11 draft standard, actual wording in final standard might be different.)

This means that it's perfectly correct to drop the termination character if the array doesn't have room for it. It's maybe unexpected, but it's exactly how the language is supposed to work, and a (at least to me) well-known feature.

On the contrary, the C++ standard says:

There shall not be more initializers than there are array elements.

Example:

 char cv[4] = "asdf"; // error 

is ill-formed since there is no space for the implied trailing '\0'.

(8.5.2 of the C++ 2011 draft n3242.)



回答2:

The preffered way of declaring a string literal is usually:

   char a[] = "Network";    printf("size of a: %d\n", sizeof a); // The compiler 'knows' the size of a.    // this prints '8' 

Let the compiler figure it out. It's cumbersome to manually specify the array size and keep it in sync with the string literal's actual length...

So I guess GCC doesn't really bother with anything more than a warning.



回答3:

In the early days of C and Unix, memory and disk were small, so not storing the NUL byte at the end of the string was actually a technique that was used. If the string variable is seven characters long, you could store a seven-character string in it, and since seven was the max length, you knew the string ended there, even without the terminator character. This is why strncpy works the way it does.



回答4:

While unwind's answer explains why gcc doesn't warn about this, it doesn't say what you can do about it.

gcc's -Wc++-compat warning option will detect this particular issue with the message:

foo.c: In function ‘main’: foo.c:3:17: warning: initializer-string for array chars is too long for C++ [-Wc++-compat] 

That's the only option that will cause gcc to warn about this problem. You can write a short script to quickly grep the warning options out of gcc's man page, try compiling with each, and see if it complains.

$ time for F in $(man gcc | grep -o -- '-W[^= ]*')     do if gcc -c "${F}" foo.c |& grep :3 >& /dev/null; then          echo "${F}"; gcc -c "${F}" foo.c     fi   done man gcc | grep -o -- '-W[^= ]*') man gcc | grep -o -- '-W[^= ]*' -Wall foo.c: In function ‘main’: foo.c:3:10: warning: unused variable ‘a’ [-Wunused-variable] -Wc++-compat foo.c: In function ‘main’: foo.c:3:17: warning: initializer-string for array chars is too long for C++ [-Wc++-compat] -Wunused foo.c: In function ‘main’: foo.c:3:10: warning: unused variable ‘a’ [-Wunused-variable] -Wunused-variable foo.c: In function ‘main’: foo.c:3:10: warning: unused variable ‘a’ [-Wunused-variable] -Wtraditional foo.c: In function ‘main’: foo.c:3:5: warning: traditional C rejects automatic aggregate initialization [-Wtraditional] -Wall foo.c: In function ‘main’: foo.c:3:10: warning: unused variable ‘a’ [-Wunused-variable] -Wall foo.c: In function ‘main’: foo.c:3:10: warning: unused variable ‘a’ [-Wunused-variable] -Wunused-variable foo.c: In function ‘main’: foo.c:3:10: warning: unused variable ‘a’ [-Wunused-variable] -Wunused foo.c: In function ‘main’: foo.c:3:10: warning: unused variable ‘a’ [-Wunused-variable] -Wunused foo.c: In function ‘main’: foo.c:3:10: warning: unused variable ‘a’ [-Wunused-variable] -Wall foo.c: In function ‘main’: foo.c:3:10: warning: unused variable ‘a’ [-Wunused-variable] -Wall foo.c: In function ‘main’: foo.c:3:10: warning: unused variable ‘a’ [-Wunused-variable] -Wall foo.c: In function ‘main’: foo.c:3:10: warning: unused variable ‘a’ [-Wunused-variable] -Wunused foo.c: In function ‘main’: foo.c:3:10: warning: unused variable ‘a’ [-Wunused-variable] -Wunused-variable foo.c: In function ‘main’: foo.c:3:10: warning: unused variable ‘a’ [-Wunused-variable] -Wunused foo.c: In function ‘main’: foo.c:3:10: warning: unused variable ‘a’ [-Wunused-variable] -Wunused foo.c: In function ‘main’: foo.c:3:10: warning: unused variable ‘a’ [-Wunused-variable] -Wunused foo.c: In function ‘main’: foo.c:3:10: warning: unused variable ‘a’ [-Wunused-variable] -Wall foo.c: In function ‘main’: foo.c:3:10: warning: unused variable ‘a’ [-Wunused-variable] -Wall foo.c: In function ‘main’: foo.c:3:10: warning: unused variable ‘a’ [-Wunused-variable] -Wall foo.c: In function ‘main’: foo.c:3:10: warning: unused variable ‘a’ [-Wunused-variable] -Wall foo.c: In function ‘main’: foo.c:3:10: warning: unused variable ‘a’ [-Wunused-variable] -Wtraditional foo.c: In function ‘main’: foo.c:3:5: warning: traditional C rejects automatic aggregate initialization [-Wtraditional] -Wtraditional foo.c: In function ‘main’: foo.c:3:5: warning: traditional C rejects automatic aggregate initialization [-Wtraditional] -Wc++-compat foo.c: In function ‘main’: foo.c:3:17: warning: initializer-string for array chars is too long for C++ [-Wc++-compat] -Wall foo.c: In function ‘main’: foo.c:3:10: warning: unused variable ‘a’ [-Wunused-variable] -Wtraditional foo.c: In function ‘main’: foo.c:3:5: warning: traditional C rejects automatic aggregate initialization [-Wtraditional] -Wall foo.c: In function ‘main’: foo.c:3:10: warning: unused variable ‘a’ [-Wunused-variable] -Wall foo.c: In function ‘main’: foo.c:3:10: warning: unused variable ‘a’ [-Wunused-variable] -Wall foo.c: In function ‘main’: foo.c:3:10: warning: unused variable ‘a’ [-Wunused-variable] -Wall foo.c: In function ‘main’: foo.c:3:10: warning: unused variable ‘a’ [-Wunused-variable] -Wall foo.c: In function ‘main’: foo.c:3:10: warning: unused variable ‘a’ [-Wunused-variable] -Wtraditional foo.c: In function ‘main’: foo.c:3:5: warning: traditional C rejects automatic aggregate initialization [-Wtraditional]  real    0m26.399s user    0m5.128s sys 0m15.329s 

In general, a lint-like tool such as splint will warn you about all sorts of potential issues. In this case, it will say:

foo.c:3:17: String literal with 8 characters is assigned to char [7] (no room                for null terminator): "Network"   A string literal is assigned to a char array that is not big enough to hold   the null terminator. (Use -stringliteralnoroom to inhibit warning) foo.c:3:10: Variable a declared but not used 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!