这个小项目基于this discussion,关于在执行操作之前检测整数溢出的最佳方法。我想要做的是有一个程序证明使用整数检查的有效性。对于某些数字,它应该产生一个未经检查的整数溢出,而如果使用check(-c)标志,它应该在执行操作之前退出。 -m用于乘法。
程序在没有布尔部分的情况下运行正常,但是我需要一些帮助进行最高开率检查的布尔部分。添加true / false逻辑后,我收到编译错误。我不确定我是否正在调用并正确使用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)
^
答案 0 :(得分:1)
检查以下链接: Nested function in C
标准C不支持嵌套函数。因此,您会看到编译错误。
请将您的功能移到main()
之外,然后从main()
答案 1 :(得分:1)
[渴望评论]
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);
}
但是仍然存在一个错误,您可能希望找到并修复自己。仔细查看编译器警告您的内容。要启用所有警告,请使用-Wall -Wextra -pedantic
作为gcc。