简体   繁体   English

如果为char分配的值太大而无法放入字节,会发生什么?

[英]What happens when a char is assigned a value too large to fit in a byte?

Let's say I have a char pointer: 假设我有一个char指针:

    char * cp;
    int val2 = 2;
    cp = &val2;
    *cp = 1234;

What will happen since the value 1234 is too large to fit in 1 byte? 由于值1234太大而无法容纳1个字节会发生什么? Will it just overflow and cause incorrect values to be stored, or will it somehow store the correct value across several bytes? 它会溢出并导致存储不正确的值,还是会以某种方式将正确的值存储在几个字节中?

In *cp = 1234; *cp = 1234; , *cp refers to just one byte, the first (lowest-addressed) byte of val2 . *cp仅指一个字节,即val2的第一个(最低寻址)字节。

In an assignment, the value of the right-hand side is converted to the type of the left side. 在赋值中,右侧的值将转换为左侧的类型。 So, 1234 will be converted to char . 因此,1234将转换为char

There are two complications here. 这里有两个复杂的问题。 One, in most C implementations, 1234 is too big to fit into a char . 一,在大多数C实现中,1234太大而不适合char (The C standard allows a char to be larger than eight bits, but this is rare in modern computers.) Two, char may be signed or unsigned. (C标准允许char大于8位,但这在现代计算机中很少见。)2, char可以是有符号或无符号的。

If char is unsigned, then the conversion is well defined: 1234 is reduced modulo one more than the largest value of char (usually 255, so the reduction is modulo 256). 如果char是无符号的,那么转换很明确:1234比char的最大值模数减少一个(通常为255,因此减少模数为256)。 In the usual case, 1234 will be reduced to 210, and this will be stored in the byte referred to by *cp . 在通常情况下,1234将减少到210,并且这将存储在*cp引用的字节中。

If char is signed and 1234 does not fit in a char , then the conversion is implementation-defined or an implementation-defined signal is raised. 如果char已签名且1234不适合char ,则转换是实现定义的,或者引发实现定义的信号。 In many modern C implementations, the result of the conversion will be -46. 在许多现代C实现中,转换的结果将是-46。

The byte that cp points to after cp = &val2; cp指向cp = &val2;之后的字节cp = &val2; is the lowest-addressed byte of val2 . val2的最低地址字节。 It is not necessarily the least significant byte of val2 . 它不一定是val2的最低有效字节。 Some C implementations store integers with the least significant byte lowest in memory ( little endian ), and some C implementations store integers with the most significant byte lowest in memory ( big endian ). 一些C实现存储内存中最低有效字节最小的整数( 小端 ),而一些C实现存储内存中最高有效字节最低的整数( 大端 )。 (There are even systems that do not store the bytes in order; they may be mixed up in memory.) (甚至有些系统不按顺序存储字节;它们可能在内存中混淆。)

Most modern C implementations store integers in two's complement form without padding, so, once you know whether the C implementation is big-endian or little-endian, you would know how changing one byte would affect the value of the int . 大多数现代C实现以二进制形式存储整数而没有填充,因此,一旦你知道C实现是big-endian还是little-endian,你就会知道改变一个字节会如何影响int的值。 However, the C standard still permits sign-and-magnitude or ones' complement. 但是,C标准仍然允许符号或数字补码。 It also permits padding bits in signed integers. 它还允许有符号整数中的填充位。

Finally, note that char is a special type which permits accessing parts of other objects in this way. 最后,请注意char是一种特殊类型,它允许以这种方式访问​​其他对象的一部分。 If, for example, you used short instead of char , as in short *sp = &val2; *sp = 0; 例如,如果你使用short而不是char ,就像short *sp = &val2; *sp = 0; short *sp = &val2; *sp = 0; , it would be a violation of aliasing rules, and the behavior would be undefined. ,这将违反别名规则,并且行为将是未定义的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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