The document discusses integer operations in C/C++ and potential errors that can occur. It proposes a SafeInt class that prevents common integer errors like overflow, underflow and division by zero at compile-time by wrapping basic integer types and checking operations. The SafeInt class aims to make integer operations safer and avoid hard to debug bugs.
4. short len;
len = strlen(str);
warning C4244:
'=' : conversion from
'size_t' to 'short„
, possible loss of data
short len;
len = (short) strlen(str);
13. char a, b, c, t, r1, r2;
a = b = c = 100;
r1 = a + b – c;
t = a + b;
r2 = t – c;
if(r1 == r2)
printf(“EQ”);
else
printf(“NEQ”);
14. int compare(INT a, USHORT b)
{
if(a > b) return 1;
else if(a < b) return -1;
else return 0;
}
printf(“%d”, compare(-1, -1));
15. int compare(INT a, UINT b)
{
if(a > b) return 1;
else if(a < b) return -1;
else return 0;
}
printf(“%d”, compare(-1, 0));
16. int a = 6;
int b = -2;
printf(“%d”, a + b);
unsigned int c = -2;
printf(“%d”, a + c);
short d = -2;
printf(“%d”, a + d);
unsigned char e = -2;
printf(“%d”, a + e);
17. int a = 6, b = -2;
printf(“%d”, a / b);
unsigned int c = -2;
printf(“%d”, a / c);
short d = -2;
printf(“%d”, a / d);
unsigned char e = -2;
printf(“%d”, a / e);
25. Usual Arithmetic Conversions
• If either operand is of type long double, the other operand is converted to
type long double.
• If the above condition is not met and either operand is of type double, the
other operand is converted to type double.
• If the above two conditions are not met and either operand is of type float,
the other operand is converted to type float.
• If the above three conditions are not met (none of the operands are of
floating types), then integral conversions are performed on the operands as
follows:
– If either operand is of type unsigned long, the other operand is converted to type
unsigned long.
– If the above condition is not met and either operand is of type long and the other of
type unsigned int, both operands are converted to type unsigned long.
– If the above two conditions are not met, and either operand is of type long, the other
operand is converted to type long.
– If the above three conditions are not met, and either operand is of type unsigned int,
the other operand is converted to type unsigned int.
– If none of the above conditions are met, both operands are converted to type int.
38. Sign Error
int off, len;
if(off > len – sizeof(type_name))
goto error;
int off, len;
if(off + sizeof(type_name) > len)
goto error;
39. Truncation Error
int a = USHRT_MAX + 1;
USHORT b = (USHORT) a;
short a = 3000;
char b = (char) a;
40. 왜 어려울까?
__try
{
int a = INT_MAX, b = 1;
int c = a + b;
}
__except(EXCEPTION_EXECUTE_HANDLER)
{
// ??
}
41. 왜 어려울까?
int a = INT_MAX, b = 1;
int c = a + b;
char a = INT_MAX, b = 1;
int c = a + b; INT_MAX, b = 1;
unsigned a =
int c = a = INT_MAX, b = 1;
short a + b;
int c = a + b;
long a = INT_MAX, b = 1;
int c =aa=*INT_MAX, b = 1;
char b;
int c = a * b;
42. 정수 연산 똑바로 하라고 책까지
썼는데, 사서 읽어 보는 놈이 없
눼... ㅠㅠ~
우리가 그냥 하나 만들어 주는게
좋겠어. 멍청한 애들 고생 안하
게...
그래? 근데 나 코딩 안한지 엄청
오래 됐는데. 니가 만들어. ㅋㅋ~
54. short len;
len = strlen(str);
warning C4244:
'=' : conversion from
'size_t' to 'short„
, possible loss of data
short len;
len = (short) strlen(str);
short len;
len = SafeInt<short>(strlen(str));