Skip to content
DarkKaiser의 블로그
DarkKaiser의 블로그
  • 개발 관련 자료(노션)
  • Raspberry Pi(노션)
  • WD My Cloud(노션)
  • GitHub
DarkKaiser의 블로그

SafeInt in Visual C++ 2010

DarkKaiser, 2010년 5월 25일2023년 9월 5일

참고 : http://www.nuonsoft.com/blog/2009/06/09/safeint-in-visual-c-2010/

VS2010에 추가된 라이브러리, 정수형 연산시에 오버플로우 확인이나 나눗셈시에 0으로 나누는 값 확인에 사용될 수 있다.

실제 아래 소스의 결과를 보면 연산 결과가 오버플로우시에는 결과값이 0으로 출력되며 이때 연산중에 SafeIntOnOverflow() 함수가 실행되는 것을 볼 수 있다.

또한 나눗셈 연산 시에도 나누는 값이 0이라면 연산시에 SafeIntOnDivZero() 함수가 호출되는 것을 볼 수 있다.

using namespace std;
using namespace msl::utilities;

class CMySafeIntException : public SafeIntException
{
public:
  static void CMySafeIntException::SafeIntOnOverflow()
  {
    cout << "Caught a SafeInt Overflow exception!" << endl;
  }

  static void CMySafeIntException::SafeIntOnDivZero()
  {
    cout << "Caught a SafeInt Divide By Zero exception!" << endl;
  }
};

int _tmain(int argc, _TCHAR* argv[])
{
  while (1)
  {
    unsigned int a, b;
    cout << "Enter a first 8-bit unsigned integer: ";
    cin >> a;
    cin.ignore();
    cout << "Enter a second 8-bit unsigned integer: ";
    cin >> b;
    cin.ignore();

    // Add both integers
    cout << "Adding both numbers as 8-bit integers:" << endl;

    // Add both integers using normal operators
    unsigned __int8 i1 = a;
    unsigned __int8 i2 = b;
    cout << "    Using standard operators, result=";
    unsigned __int8 iResult = i1 + i2;
    cout << (int)iResult << endl;

    // Add both integers using SafeInt objects
    SafeInt<unsigned __int8, CMySafeIntException> si1(i1);
    SafeInt<unsigned __int8, CMySafeIntException> si2(i2);
    cout << "    Using SafeInt objects, result=";
    SafeInt<unsigned __int8, CMySafeIntException> siResult = si1 + si2;
    cout << (int)siResult << endl;

    // Divide both integers
    cout << endl << "Dividing both numbers:" << endl;

    // Divide both integers using normal operators
    cout << "    Using standard operators, result=";
    if (i2 != 0)// Prevent a crash!
    {
      iResult = i1 / i2;
      cout << (int)iResult << endl;
    }
    else
      cout << "Aborting because divide by zero." << endl;

    // Divide both integers using SafeInt objects
    cout << "    Using SafeInt objects, result=";
    siResult = si1 / si2;
    cout << (int)siResult << endl;

    cout << endl << endl;
  }

  return 0;
}

결과:

사용자 삽입 이미지
C/C++/VC++ SafeInt

글 내비게이션

Previous post
Next post

답글 남기기 응답 취소

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다

최신 글

  • AssertJ 소개testCompile ‘org.assertj:assertj-core:3.6.2’ 2017년 9월 14일
  • 자주 사용되는 Lombok 어노테이션 2017년 9월 14일
  • 유니코드 #3 2017년 9월 14일
  • 유니코드 #2 2017년 9월 14일
  • 유니코드 #1 2017년 9월 14일

최신 댓글

    카테고리

    • 개인 자료 (1)
      • 일기 (1)
    • 주절주절 (7)
    • 프로그래밍 갤러리 (16)
    • 프로그래밍 언어 (186)
      • Java (29)
      • C/C++/VC++ (114)
      • C# (11)
      • Visual Basic (6)
      • 안드로이드 (9)
      • Objective-C (5)
      • JavaScript (4)
      • JSP/Servlet (2)
      • Python (4)
      • 어셈블러 (1)
    • 개발++ (44)
      • Book (11)
        • Joel On Software (10)
      • 프로젝트 관리 (6)
      • Maven (1)
      • 디버깅 (1)
      • DirectX (1)
      • Silverlight (1)
      • RESTful (1)
      • Hacking (1)
      • WDM (4)
      • VoIP (5)
      • 기타 (1)
    • 개발 도구 (15)
      • eclipse (14)
      • Sublime Text (1)
    • 네트워크 (7)
    • 설치 및 배포 (7)
      • InstallShield (2)
      • NSIS (4)
    • 버전 관리 (9)
      • Git (2)
      • CVS (2)
      • Subversion (5)
    • 데이터베이스 (7)
      • Oracle (3)
      • Sybase (2)
      • MS-SQL (2)
    • 단위테스트 (3)
      • JUnit (1)
      • NUnit (2)
    • 버그추적시스템 (2)
      • mantis (2)
    • 운영체제 (7)
      • Windows (5)
      • 리눅스 (2)
    • WAS (3)
      • WebLogic (3)
    • 디자인패턴 (1)
    • 디지털 이미지 프로세싱 (16)

    태그

    Abstract ActiveX AfxParseURL Automation boost devenv.exe event EventLogTraceListener Hover interface IO iTextSharp JAD jar JavaScript Joel Leave MFC Monitor msdev.com MSDN mutable PDF Properties RAW Saturation SHGetFolderPath SHGetKnownFolderPath SQLite STLTask String TextWriterTraceListener URL VI 권한 데이터소스 디컴파일러 문자열 스레드 동기화 스레드 생성 실용주의 프로그래머 자동화 테스팅 파일포맷 프리컴파일

    메타

    • 로그인
    • 엔트리 피드
    • 댓글 피드
    • WordPress.org
    ©2025 DarkKaiser의 블로그 | WordPress Theme by SuperbThemes