Click here to Skip to main content
15,881,600 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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]


I have initialized all bytes to have 0
0x00

I am adding to the 0 th element occasionally
buffer[0] += someSmallAddValueThatIsAnInt; 


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.
Posted
Updated 9-Sep-10 10:02am
v2

It happens that buffer[0] value is set to
(buffer[0] + thatValueWithThatSillyName ) % 256

(i.e. the result is truncated to fit into a byte).
Nothing else.
:)
 
Share this answer
 
Comments
[no name] 9-Sep-10 16:13pm    
All systems?
CPallini 9-Sep-10 17:38pm    
Yes, that is a requirement of the C programming language (i.e. if the C compiler implements correctly the language...).
One would hope that doing addition doesn't corrupt your neighbors. Now if you really just want to see what would happen if it did over flow this little snippet forces an overflow.

C#
unsigned char test[10] = {0};
    int * testptr= NULL;
    testptr = (int*)&test[5];

    test[5] = 254;
    test[5] = test[5] + 3;
    *testptr = 300;


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
 
Share this answer
 
Comments
[no name] 9-Sep-10 16:52pm    
Thank you. That is what I was looking for. You not only provided an answer but a way have the data maintained (assuming I have the buffer set up right and the code to check what system it is)
[no name] 9-Sep-10 17:00pm    
Unfortunately on my system the last line causes an exception :(
Eugen Podsypalnikov 9-Sep-10 17:06pm    
The address of test[5] does not lie at the integer-boundary,
the testptr can not be used to dereference its value directly on some systems :)
[no name] 9-Sep-10 17:08pm    
NM last comment. Forgot to initialize. However, I am seeing overflow for the first test also...
[no name] 9-Sep-10 17:14pm    
NM again.. Was not reading the memory right. Looks like first one truncates and the second one gets the data in test[4]. THanks again!
I tested this with VS2010.

buffer[0] gets LS and MS goes to the bit bucket. Looking at the memory debug window I was able to see that the memory was set to this...

cc cc cc cc 02 00 00 00 cc cc cc cc

when I used this following code. I moved back in memory to show the -1 value.

MIDL
unsigned char buffer[4];
buffer[0] = 0;
buffer[1] = 0;
buffer[2] = 0;
buffer[3] = 0;
buffer[0] = 255;
buffer[0] += 3;
 
Share this answer
 
Comments
[no name] 9-Sep-10 16:18pm    
What do you mean the bit bucket?
Also I am confused by the memory you are showing. buffer is only 4 bytes long but you are showing 12 bytes.
Steve Maier 9-Sep-10 16:29pm    
When something goes to the bit bucket, it means it just goes away. The 12 bytes that I was showing are the 4 bytes before the buffer and the 4 bytes after so that you can see that they did not change at all.
CPallini 9-Sep-10 16:42pm    
Experimental evidence. :-)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900