Home > Enterprise >  Buffer overflow with integers
Buffer overflow with integers

Time:01-28

I have a very basic question. Lets say I have two variables(uint16_t a, uint16_t b) and in memory they are aligned next to each other like a=> 0x0 => 0x15 and b=> 0x16 to 0x31

Lets assume a = 0, b = 65535,

(1) if i increment b(b  ), b will become 0 but will it affect 'a' 0th bit?
(2) if i right shift b( b = b << 1), will it affect 'a' ?

Thank you

CodePudding user response:

No, unless you are doing odd things with pointers or casts.

CodePudding user response:

I'd say it wouldn't since lets say a starts at 0x00 and a=65535, when you add one you should(always) get weird behaviour at best (or segmentation fault). Im not 100% sure though, but im 90% sure about left shift not affecting the memory next to it.

CodePudding user response:

The answer is no.

a and b is a uint16_t, so it is of an unsigned type. And unsigned overflow (or wrap-around) is well-defined in C. It won't change the memory besides it.

CodePudding user response:

No, a correctly designed system will not have that happen. Also, I will point out that your numeral notation is incorrect by common convention. 0x is generally used to notate hexadecimal numbers, including in the C language, but from the context of your question, you are prefixing decimal base numbers with it for no apparent reason. For example, 0x31 is equal to 49 in decimal. And 16 16 is not equal to 49.

  • Related