1

How to detect overflow of unsigned char variable in c++?

2
  • 1
    Well first of all, an unsigned char ranges from 0 to 255, and youve assigned it 260. Commented Nov 30, 2015 at 16:27
  • Compiled with default settings, gcc and g++ will issue a warning: large integer implicitly truncated to unsigned type [-Woverflow] Commented Nov 30, 2015 at 16:28

3 Answers 3

1

unsigned numbers always positive between 0 to 255.and obey the law 2^n(n = numbers of bit in type);if char 8 bit then unsigned char variables have values between 0 and 255 , while signed chars have values between -128 and 127.

Sign up to request clarification or add additional context in comments.

2 Comments

I guess, you meant unsigned char value to be in range 0 to 255 but even that is false: there is no constraint limiting the size of a char to 8 bits. There is only a requirement that char has at least 8 bits (and the number of bits in an unsigned char matches the number of bits in a char).
@SudipDas actually signed chars do not need to be in range [-128...127], range is implementation defined (note that not all architectures represent negative numbers in C2). That said how this answers the question?
1

unsigned char Test = 260;

Because 260 is an integer literal your compiler should emit a warning. How to handle that? Do not ignore compiler warnings (or use an alternative syntax to avoid automatic conversions or enable this warning as error). Also note that integer literals are always positive (unsigned): -1 is not an integer literal: it's i.l. 1 and unary operator -. For gcc I'd suggest to use -Wstrict-overflow=2 (or more, according to your code policies) and possibly enabling -Werror=strict-overflow. For MS VC++ you may enable warning C4307 with /we4307 and /W14307 if you keep warnings at level 1 (!!!) (you may also do it with #pragma warning directive).

How to detect overflow of unsigned char variable in c++?

At compile-time compiler warnings are your friends but at run-time?

There is not a portable way (like, for example, checked in C#) to do this and better technique depends on which type of operation you want to monitor. For a simple assignment (made with values known at run-time) you may write something like this:

int32_t bigNumber = 260;
uint8_t smallNumber = static_cast<uint8_t>(bigNumber);
if (static_cast<int32_t>(smallNumber) != bigNumber) {
    // Overflow...
}

In alternative you may check before assigning:

int32_t bigNumber = 260;
if (bigNumber > UINT8_MAX) {
    // Overflow
}

Note that you may also make compiler life easier writing (after assignment):

if (smallNumber != bigNumber) {
    // Overflow
}

It works because automatic promotions will convert smallNumber to bigNumber type (unless you're performing a signed/unsigned comparison, in this case you should simply avoid this alternative).

If you need it often you may write a small helper function to perform this conversion. For some ideas and possible implementations, if you're using MS compiler, you may take a look to SafeInt family functions (note, however, that in this case assignment and casting won't throw).

1 Comment

This might help me. Thank you
0

You can use braces to initialize your value to force a compile-time error (assuming you use C++11 or later):

unsigned char Test{260};

Brace-initialization doesn't allow narrowing conversions.

Of course, that still wouldn't allow sticking the value 260 into an unsigned char but it would draw attention to the attempt. You'd need a bigger data type, e.g., unsigned short, to represent 260.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.