gcc的非空终止字符串编译器选项

时间:2022-11-02 20:40:40

Update

turns out this is just another case of "c++ is not c blues"

这是另一个"c++不是c蓝调"的例子


What I want

我想要的

const char hex[16] = "0123456789ABCDEF";

the only thing that works

唯一有效的方法。

char hex[16] = "0123456789ABCDE"; hex[15] = "F";

are there any compiler options or something I can do to make strings not null terminated in the gcc compiler. so that I can make a(n) constant array

是否有任何编译器选项或我可以做的事情使字符串在gcc编译器中以非空结束。所以我可以做一个(n)不变的数组。

5 个解决方案

#1


11  

No need for a compiler option, it's already non-NUL terminated. The standard says a NUL should only be added if it can fit, otherwise it would be an overflow. It may just be that the next byte in memory past your array is \0

不需要编译器选项,它已经非nul终止。标准规定,只有在合适的时候才会添加一个NUL,否则就会溢出。它可能只是内存中数组后面的下一个字节是\0

§ 6.7.8p14

An array of character type may be initialized by a character string literal, optionally enclosed in braces. Successive characters of the character string literal (including the terminating null character if there is room or if the array is of unknown size) initialize the elements of the array.

§6.7.8p14字符类型的数组可以初始化字符串文字,封闭在括号内选择。字符串文字的连续字符(如果有空间或数组大小未知,包括终止空字符)初始化数组的元素。

#2


7  

No. NUL-terminated strings are intrinsic to the language. You can have a character array though, and set each character one by one:

不。终止字符串是语言固有的。你可以有一个字符数组,并设置每个字符一个:

char hex [] = {'0', '1', '2', ... 'F'};

#3


5  

You answered your own question. If you explicitly give the array a length, as in:

你回答了你自己的问题。如果显式地给数组一个长度,如:

const char hex[16] = "0123456789ABCDEF";

then of course it won't be null-terminated because there is no storage reserved for null termination. (hex[16] is outside the bounds of the object and thus reading or writing it is undefined behavior. If it happens to read as 0, that's UB for ya...)

当然它不会被空终止,因为没有为空终止保留的存储。(hex[16]超出对象的范围,因此读取或写入它是未定义的行为。如果它读起来是0,那就是UB…

It's only if you leave the length implicit, as in:

只有当你把长度隐去,比如:

const char hex[] = "0123456789ABCDEF";

or if you use the string literal as an object rather than as an initializer, that it will have null termination.

或者,如果您将字符串文字用作对象而不是初始化器,那么它将具有null终止。

By the way, why do you care if the null termination is there or not, if you're not planning to use it. Are you trying to shave bytes off your binary? :-)

顺便问一下,如果你不打算使用零终止,为什么要考虑它是否存在呢?您是否试图从二进制文件中删除字节?:-)

#4


0  

Strings are null terminated in C. If you want to populate a non-null-terminated char array you can use an array initializer.

字符串在c中以null结尾,如果要填充非空终止字符数组,可以使用数组初始化器。

#5


0  

I believe the question is a bit unclear: In C, the qoted initialization:

我认为这个问题有点不清楚:在C中,qoted初始化:

static const char hex[16] = "0123456789ABCDEF";

is legal. In C++, it is not. So it is one of the pieces of code, that fail (fortunately at compile time), when you move from C to C++.

是合法的。在c++中,它不是。因此,当您从C迁移到c++时,它是一段失败的代码(幸运的是在编译时)。

It would be nice to have a way to force string literals without termination \0 byte. Something like:

如果有一种方法可以强制字符串文字而不终止\0字节,那就太好了。喜欢的东西:

static const char hex[16] = "0123456789ABCDEF\!0";

where the \!0 at the end tells the compiler to not zero-terminate the string! \! or even \!0 anywhere else in the string would behave unmodified, so just put out a literal ! or !0 .

在\ !最后告诉编译器不要零终止字符串!\ !甚至\ !在字符串中其他任何地方都不会被修改,所以只要写一个文字!或! 0。

#1


11  

No need for a compiler option, it's already non-NUL terminated. The standard says a NUL should only be added if it can fit, otherwise it would be an overflow. It may just be that the next byte in memory past your array is \0

不需要编译器选项,它已经非nul终止。标准规定,只有在合适的时候才会添加一个NUL,否则就会溢出。它可能只是内存中数组后面的下一个字节是\0

§ 6.7.8p14

An array of character type may be initialized by a character string literal, optionally enclosed in braces. Successive characters of the character string literal (including the terminating null character if there is room or if the array is of unknown size) initialize the elements of the array.

§6.7.8p14字符类型的数组可以初始化字符串文字,封闭在括号内选择。字符串文字的连续字符(如果有空间或数组大小未知,包括终止空字符)初始化数组的元素。

#2


7  

No. NUL-terminated strings are intrinsic to the language. You can have a character array though, and set each character one by one:

不。终止字符串是语言固有的。你可以有一个字符数组,并设置每个字符一个:

char hex [] = {'0', '1', '2', ... 'F'};

#3


5  

You answered your own question. If you explicitly give the array a length, as in:

你回答了你自己的问题。如果显式地给数组一个长度,如:

const char hex[16] = "0123456789ABCDEF";

then of course it won't be null-terminated because there is no storage reserved for null termination. (hex[16] is outside the bounds of the object and thus reading or writing it is undefined behavior. If it happens to read as 0, that's UB for ya...)

当然它不会被空终止,因为没有为空终止保留的存储。(hex[16]超出对象的范围,因此读取或写入它是未定义的行为。如果它读起来是0,那就是UB…

It's only if you leave the length implicit, as in:

只有当你把长度隐去,比如:

const char hex[] = "0123456789ABCDEF";

or if you use the string literal as an object rather than as an initializer, that it will have null termination.

或者,如果您将字符串文字用作对象而不是初始化器,那么它将具有null终止。

By the way, why do you care if the null termination is there or not, if you're not planning to use it. Are you trying to shave bytes off your binary? :-)

顺便问一下,如果你不打算使用零终止,为什么要考虑它是否存在呢?您是否试图从二进制文件中删除字节?:-)

#4


0  

Strings are null terminated in C. If you want to populate a non-null-terminated char array you can use an array initializer.

字符串在c中以null结尾,如果要填充非空终止字符数组,可以使用数组初始化器。

#5


0  

I believe the question is a bit unclear: In C, the qoted initialization:

我认为这个问题有点不清楚:在C中,qoted初始化:

static const char hex[16] = "0123456789ABCDEF";

is legal. In C++, it is not. So it is one of the pieces of code, that fail (fortunately at compile time), when you move from C to C++.

是合法的。在c++中,它不是。因此,当您从C迁移到c++时,它是一段失败的代码(幸运的是在编译时)。

It would be nice to have a way to force string literals without termination \0 byte. Something like:

如果有一种方法可以强制字符串文字而不终止\0字节,那就太好了。喜欢的东西:

static const char hex[16] = "0123456789ABCDEF\!0";

where the \!0 at the end tells the compiler to not zero-terminate the string! \! or even \!0 anywhere else in the string would behave unmodified, so just put out a literal ! or !0 .

在\ !最后告诉编译器不要零终止字符串!\ !甚至\ !在字符串中其他任何地方都不会被修改,所以只要写一个文字!或! 0。