Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 使用布尔值检查整数溢出_C_Integer Overflow - Fatal编程技术网

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

C 使用布尔值检查整数溢出,c,integer-overflow,C,Integer Overflow,这个小项目基于在执行操作之前检测整数溢出的最佳方法。我想做的是让一个程序演示使用整数检查的有效性。对于某些数字,它应该生成一个未经检查的整数溢出,而如果使用check(-c)标志,则应该在执行操作之前退出。-m表示乘法 该程序在没有布尔部分的情况下运行良好,但我需要一些关于执行highestOneBitPosition检查的布尔部分的帮助。添加真/假逻辑后,我遇到编译错误。我不确定是否正确调用和使用了highestOneBitPosition函数。谢谢 #include <stdio.h&

这个小项目基于在执行操作之前检测整数溢出的最佳方法。我想做的是让一个程序演示使用整数检查的有效性。对于某些数字,它应该生成一个未经检查的整数溢出,而如果使用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);}
检查以下链接:

标准C不支持嵌套函数。因此您会看到编译错误。
请将您的函数移到
main()
之外,只需从
main()

[渴望注释]

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 -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);
}
#包括
#包括
#包括
/*布尔值*/
#定义真1
#定义false 0
typedef int bool;
void ShowUsage()
{
printf(“执行算术之前的整数溢出检查。\n”
“===============================================================================\n”
“用法:\n”
“整数运算符(-a,-s,-m,-d)已选中/未选中(-u,-c)\n”
“示例:./溢出检查2-a 2-u\n”
“\n”);
}
尺寸高度内固定位置(uint32)
{
大小\u t位=0;
while(a!=0)
{
++比特;
a>>=1;
};
返回位;
}
布尔乘法是安全的(uint32\u t a,uint32\u t b)
{
a=atoi(argv[1]);
b=atoi(argv[3]);
大小\u t a_位=高位(a),b_位=高位(b);

return(a_bits+b_bits您正在尝试定义嵌套函数吗?C不支持它。在
main
??
if(乘法是安全的==true)
-->
if(乘法是安全的(0,0)==true)
if(乘法是安全的==false)
-->
其他
,以及
#包括
也包括
#包括
#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);
}