当一个 char 被赋予一个太大而无法放入一个字节的值时会发生什么? [英] What happens when a char is assigned a value too large to fit in a byte?

查看:18
本文介绍了当一个 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只指一个字节,第一个(最低-addressed) 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.

cpcp = &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 实现是大端还是小端,您就会知道更改一个字节将如何影响 的值整数.但是,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天全站免登陆