问题描述
这可能是特定于系统的,但我想知道哪些系统也不同(它的字节序).
我有一个
This is probably system specific but I would like to know what systems are different also (its endian stuff).
I have a
unsigned char buffer[4]
我已将所有字节初始化为0
0x00
我偶尔会添加第0个元素
I have initialized all bytes to have 0
0x00
I am adding to the 0 th element occasionally
buffer[0] += someSmallAddValueThatIsAnInt;
当值太大而溢出时会发生什么?
崩溃了吗?
buffer [1]获取LS数据,buffer [0]获取MS?
buffer [1]获取MS数据,buffer [0]获取LS?
buffer [-1]获取LS数据,buffer [0]获取MS数据?
buffer [-1]获取MS数据,buffer [0]获取LS数据?
谢谢.
At the point that the value is too large and overflows what will happen?
Crash?
buffer[1] gets LS data and buffer[0] gets MS?
buffer[1] gets MS data and buffer[0] gets LS?
buffer[-1] gets LS data and buffer[0] gets MS data?
buffer[-1] gets MS data and buffer[0] gets LS data?
Thank you.
推荐答案
(buffer[0] + thatValueWithThatSillyName ) % 256
(即结果被截断以适合一个字节).
没什么.
:)
(i.e. the result is truncated to fit into a byte).
Nothing else.
:)
unsigned char test[10] = {0};
int * testptr= NULL;
testptr = (int*)&test[5];
test[5] = 254;
test[5] = test[5] + 3;
*testptr = 300;
如您所料,当您将3加到254时,它会被截断.
通过在执行赋值操作时将对im编辑值的引用转换为int,假设我在该内存位置有4字节的空间.不出所料,上述值渗入测试[6].取决于系统,它很可能渗入test [4]而不是
SH
As expected when the you add 3 to 254 it truncates.
By casting a reference to the value im editing as an int when you do the assignment it is assuming I have a 4 Byte space at that memory location. As expected the value above bleeds over into test[6]. Depending on the system it could very well bleed over into test[4] instead
SH
unsigned char buffer[4];
buffer[0] = 0;
buffer[1] = 0;
buffer[2] = 0;
buffer[3] = 0;
buffer[0] = 255;
buffer[0] += 3;
这篇关于添加到未签名的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!