C语言 使用布尔值检查整数溢出



这个小项目基于关于在执行操作之前检测整数溢出的最佳方法的讨论。我想做的是让一个程序演示使用整数检查的有效性。对于某些数字,它应该在未经检查的情况下产生整数溢出,而如果使用检查 (-c) 标志,它应该在执行操作之前退出。-m 用于乘法。

该程序在没有布尔部分的情况下运行良好,但我需要一些关于执行最高OneBitPosition检查的布尔部分的帮助。添加真/假逻辑后,我遇到编译错误。我不确定我是否正确调用和使用最高的OneBitPosition函数。谢谢!

#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 -un"
    "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)
                ^

[渴望评论]

C 语言不支持嵌套函数。

正确缩进的 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 -un"
      "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

检查以下链接:C 语言中的嵌套函数

标准 C 不支持嵌套函数。所以你看到编译错误。请将您的函数移到main()之外,然后从main()调用该函数

相关内容

  • 没有找到相关文章

最新更新