Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
557 views
in Technique[技术] by (71.8m points)

c - 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 * cp;
    int val2 = 2;
    cp = &val2;
    *cp = 1234;

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In *cp = 1234;, *cp refers to just one byte, the first (lowest-addressed) byte of 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.

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.

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.

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.

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.)

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.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...