2014-11-30 2 views
0

이 작은 프로젝트는 작업이 수행되기 전에 정수 오버플로를 감지하는 가장 좋은 방법은 this discussion을 기반으로합니다. 내가하고 싶은 것은 정수 검사를 이용하는 것의 유효성을 보여주는 프로그램을 가지고있는 것입니다. 일부 숫자의 경우 정수 오버플로를 선택 해제해야하지만 check (-c) 플래그를 사용하면 작업을 수행하기 전에 종료해야합니다. -m은 곱하기입니다.부울을 사용하여 정수 오버플로 확인

프로그램이 부울 부분없이 잘 실행되지만 highestOneBitPosition 검사를 수행하는 부울 부분에 대한 도움이 필요합니다. 진실/거짓 로직을 추가 한 후 컴파일 오류가 발생합니다. highestOneBitPosition 함수를 제대로 호출하고 사용하는지 잘 모르겠습니다. 감사!

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 
/*boolean */ 
#define true 1 
#define false 0 
typedef int bool; 

void ShowUsage() 
{ 
    printf (
    "Integer Overflow Check before performing an arithmetic.\n" 
    "=======================================================\n" 
    "Usage:\n" 
    "Integer Operant (-a, -s, -m, -d) Checked/Unchecked (-u, -c)\n" 
    "Example: ./overflowcheck 2 -a 2 -u\n" 
    "\n" 
); 
} 

size_t highestOneBitPosition(uint32_t a) { 
size_t bits=0; 
while (a!=0) { 
    ++bits; 
    a>>=1; 
}; 
return bits; 
} 

int main(int argc, char *argv[]) { 
    if  (argc != 5) {ShowUsage(); return (0);} 
    else if (strcmp(argv[2],"-m") == 0 && strcmp(argv[4],"-u") == 0) 
      {printf("%s * %s = %d -- Not checked for integer overflow.\n",argv[1],argv[3], atoi(argv[1])*atoi(argv[3]));return 0;} 
/*Works fine so far */ 
    else if (strcmp(argv[2],"-m") == 0 && strcmp(argv[4],"-c") == 0) 
    { 
    bool multiplication_is_safe(uint32_t a, uint32_t b) { 
    a = atoi(argv[1]); 
    b = atoi(argv[3]); 
    size_t a_bits=highestOneBitPosition(a), b_bits=highestOneBitPosition(b); 
    return (a_bits+b_bits<=32);} 
     if (multiplication_is_safe==true) 
     {printf("%s * %s = %d -- Checked for integer overflow.\n",argv[1],argv[3], atoi(argv[1])*atoi(argv[3]));return 0;} 
     if (multiplication_is_safe==false) 
     {printf("Operation not safe, integer overflow likely.\n");}  
    } 

    ShowUsage(); 
    return (0);} 

편집 :

gcc integer_overflow2.c -o integer_overflow 
integer_overflow2.c:40:61: error: function definition is not allowed here 
     bool multiplication_is_safe(uint32_t a, uint32_t b) { 
                  ^
integer_overflow2.c:45:17: error: use of undeclared identifier 
     'multiplication_is_safe' 
      if (multiplication_is_safe==true) 
       ^
integer_overflow2.c:47:17: error: use of undeclared identifier 
     'multiplication_is_safe' 
      if (multiplication_is_safe==false) 
       ^
+1

중첩 함수를 정의하려고합니까? C가 지원하지 않습니다. –

+0

'main' 안에이 함수 선언은 무엇입니까 ??? –

+1

if (multiplication_is_safe == true)'->'(multiplication_is_safe (0,0) == true)','if (곱셈 _is_safe == false)'->'else' 그리고'#include ' 또한 '#include ' – BLUEPIXY

답변

1

에서 해당 함수를 호출하십시오 다음과 같을 수

제대로 들여 쓰기 C 소스 :

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 
/*boolean */ 
#define true 1 
#define false 0 
typedef int bool; 

void ShowUsage() 
{ 
    printf("Integer Overflow Check before performing an arithmetic.\n" 
     "=======================================================\n" 
     "Usage:\n" 
     "Integer Operant (-a, -s, -m, -d) Checked/Unchecked (-u, -c)\n" 
     "Example: ./overflowcheck 2 -a 2 -u\n" 
     "\n"); 
} 

size_t highestOneBitPosition(uint32_t a) 
{ 
    size_t bits = 0; 
    while (a != 0) 
    { 
    ++bits; 
    a >>= 1; 
    }; 
    return bits; 
} 

bool multiplication_is_safe(uint32_t a, uint32_t b) 
{ 
    a = atoi(argv[1]); 
    b = atoi(argv[3]); 
    size_t a_bits = highestOneBitPosition(a), b_bits = highestOneBitPosition(b); 
    return (a_bits + b_bits <= 32); 
} 

int main(int argc, char *argv[]) 
{ 
    if (argc != 5) 
    { 
    ShowUsage(); 
    return (0); 
    } 
    else if (strcmp(argv[2], "-m") == 0 && strcmp(argv[4], "-u") == 0) 
    { 
    printf("%s * %s = %d -- Not checked for integer overflow.\n", argv[1], 
     argv[3], atoi(argv[1]) * atoi(argv[3])); 
    return 0; 
    } 

    /*Works fine so far */ 
    else if (strcmp(argv[2], "-m") == 0 && strcmp(argv[4], "-c") == 0) 
    { 
    if (multiplication_is_safe == true) 
    { 
     printf("%s * %s = %d -- Checked for integer overflow.\n", argv[1], 
      argv[3], atoi(argv[1]) * atoi(argv[3])); 
     return 0; 
    } 

    if (multiplication_is_safe == false) 
    { 
     printf("Operation not safe, integer overflow likely.\n"); 
    } 
    } 

    ShowUsage(); 
    return (0); 
} 

그러나 여전히 버그를 발견하고 고칠 수 있습니다. 컴파일러에서 경고하는 것을 자세히 살펴보십시오. 모든 경고를 사용하려면 gcc에 -Wall -Wextra -pedantic을 사용하십시오.

1

확인 아래 링크 : Nested function in C

표준 C는 컴파일 에러를보고있는 중첩 된 functions.So를 지원하지 않습니다. main() 외부 함수를 이동하고 단지

중첩 기능 C.에서 지원되지 않습니다 [코멘트에 대한 오랜에] main()