当为char分配的值太大而无法容纳字节时,会发生什么情况? [英] What happens when a char is assigned a value too large to fit in a byte?

查看:292
本文介绍了当为char分配的值太大而无法容纳字节时,会发生什么情况?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个char指针:

Let's say I have a char pointer:

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

由于值1234太大而无法容纳1个字节,会发生什么?它会溢出还是会导致存储不正确的值,还是会以某种方式在几个字节之间存储正确的值?

What will happen since the value 1234 is too large to fit in 1 byte? Will it just overflow and cause incorrect values to be stored, or will it somehow store the correct value across several bytes?

推荐答案

* cp = 1234; * cp 只是指一个字节,<的第一个(最低寻址)字节code> val2 。

In *cp = 1234;, *cp refers to just one byte, the first (lowest-addressed) byte of val2.

在赋值中,右侧的值转换为左侧的类型。因此,1234将转换为 char

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.

这里有两个复杂之处。其中之一,在大多数C语言实现中,1234太大而无法放入 char 。 (C标准允许 char 大于8位,但这在现代计算机中很少见。)两个, char 可以是签名的或未签名的。

There are two complications here. One, in most C implementations, 1234 is too big to fit into a 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.

如果 char 是未签名的,则可以很好地定义转换:1234为比 char 的最大值(通常为255,因此以256为模)的模数减少了一个。在通常情况下,1234将减少为210,并将存储在 * cp 所指的字节中。

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). In the usual case, 1234 will be reduced to 210, and this will be stored in the byte referred to by *cp.

如果对 char 进行了签名并且1234不适合 char ,则转换是实现定义的或引发实现定义的信号。在许多现代C实现中,转换结果将为-46。

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. In many modern C implementations, the result of the conversion will be -46.

cp 指向的字节在 cp =& val2; 之后是 val2 的最低寻址字节。它不一定是 val2 的最低有效字节。一些C实现将具有最低有效字节最低的整数存储在内存中( little endian ),而某些C实现将具有最高有效字节最低的整数存储在内存中( big endian ) 。 (甚至有些系统没有按顺序存储字节;它们可能在内存中混合在一起。)

The byte that cp points to after cp = &val2; is the lowest-addressed byte of val2. It is not necessarily the least significant byte of 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). (There are even systems that do not store the bytes in order; they may be mixed up in memory.)

大多数现代C实现都以二进制补码形式存储整数,而无需填充,因此,一旦您知道C实现是big-endian还是little-endian,便会知道更改一个字节将如何影响 int 的值。但是,C标准仍允许使用符号和大小或补码。

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. However, the C standard still permits sign-and-magnitude or ones’ complement. It also permits padding bits in signed integers.

最后,请注意, char 是一种特殊的类型,允许访问部件其他对象以这种方式。例如,如果您使用 short 而不是 char ,如 short * sp = & val2; * sp = 0; ,这将违反别名规则,并且行为未定义。

Finally, note that char is a special type which permits accessing parts of other objects in this way. If, for example, you used short instead of char, as in short *sp = &val2; *sp = 0;, it would be a violation of aliasing rules, and the behavior would be undefined.

这篇关于当为char分配的值太大而无法容纳字节时,会发生什么情况?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆