c++保护超出整数限制
C++ safeguards exceeding limits of integer
我正在做一本书的章节回顾:在章节的末尾有一些问题/任务需要你完成。
我决定用程序而不是文本文件的格式来写它们:
#include <iostream>
int main(int argc, char* argv[]) {
std::cout << "Chapter reviewn"
<< "1. Why does C++ have more than one integer type?n"
<< "tTo be able to represent more accurate values & save memory by only allocating what is needed for the task at hand.n"
<< "2. Declare variables matching the following descriptions:n"
<< "a.tA short integer with the value 80:n";
short myVal1 = 80;
std::cout << "tt"short myVal1 = 80;": " << myVal1 << std::endl
<< "b.tAn unsigned int integer with the value 42,110:n";
unsigned int myVal2 = 42110;
std::cout << "tt"unsigned int myVal2 = 42110;": " << myVal2 << std::endl
<< "c.tAn integer with the value 3,000,000,000:n";
float myVal3 = 3E+9;
std::cout << "tt"float myVal3 = 3E+9;": " << static_cast<unsigned int>(myVal3) << std::endl
<< "3. What safeguards does C++ provide to keep you from exceeding the limits of an integer type?n"
<< "tWhen it reaches maximum number it starts from the begging again (lowest point).n"
<< "4. What is the distinction between 33L and 33?n"
<< "t33L is of type long, 33 is of type int.n"
<< "5. Consider the two C++ statements that follow:ntchar grade = 65;ntchar grade = 'A';nAre they equivalent?n"
<< "tYes, the ASCII decimal number for 'A' is '65'.n"
<< "6. How could you use C++ to find out which character the code 88 represents?nCome up with at least two ways.n"
<< "t1: "static_cast<char>(88);": " << static_cast<char>(88) << std::endl; // 1.
char myChar = 88;
std::cout << "t2: "char myChar = 88;": " << myChar << std::endl // 2.
<< "t3: "std::cout << (char) 88;" " << (char) 88 << std::endl // 3.
<< "t4: "std::cout << char (88);": " << char (88) << std::endl // 4.
<< "7. Assigning a long value to a float can result in a rounding error. What about assigning long to double? long long to double?n"
<< "tlong -> double: Rounding error.ntlong long -> double: Significantly incorrect number and/or rounding error.n"
<< "8. Evaluate the following expressions as C++ would:n"
<< "a.t8 * 9 + 2n"
<< "ttMultiplication (8 * 9 = 72) -> addition (72 + 2 = 74).n"
<< "b.t6 * 3 / 4n"
<< "ttMultiplication (6 * 3 = 18 -> division (18 / 4 = 4).n"
<< "c.t3 / 4 * 6n"
<< "ttDivision (3 / 4 = 0) -> multiplication (0 * 6 = 0).n"
<< "d.t6.0 * 3 / 4n"
<< "ttMultiplication (6.0 * 3 -> 18.0) -> division (18.0 / 4 = 4.5).n"
<< "e.t 15 % 4n"
<< "ttDivision (15 / 4 = 3.75) Then returns the reminder, basically how many times can 4 go into 15 in this case that is 3 (3*4 = 12).n"
<< "9. Suppose x1 and x2 are two type of double variables that you want to add as integers and assign to an integer variable. Construct a C++ statement for doing so. What if you wanted to add them as type double and then convert to int?n"
<< "t1: "int myInt = static_cast<double>(doubleVar);"nt2: "int myInt = int (doubleVar);".n"
<< "10. What is the variable type for each of the following declarations?n"
<< "a.t"auto cars = 15;"nttintn"
<< "b.t"auto iou = 150.37f;"nttfloatn"
<< "c.t"auto level = 'B';"nttcharn"
<< "d.t"auto crat = U'/U00002155';"nttwchar_t ?n"
<< "e.t"auto fract = 8.25f/.25;"nttfloat" << std::endl;
return 0;
}
由于移动/其他一些现实生活中的东西,我已经有一段时间没有读第三章了。
这里我不确定的基本上是问题3:它以复数形式说保障措施。
然而,我只知道一个:它在达到最大值后重新开始?我遗漏了什么吗?
让我知道,如果你看到任何其他错误也-我这样做是为了学习毕竟:)。
基本上我不能接受评论作为答案,所以总结一下:
没有任何保障措施,我误解了@n.m。为我澄清。
10。@Jarod42指出他错了,这是正确的。
谢谢!
对于我来说"声明值为3,000,000,000的整型变量"是:
unsigned anInteger = 3000000000;
因为c++第11条提供了15种整数类型,unsigned int是能存储像3 000 000 000这样大的整数的最小类型
c++将整数溢出分类为"未定义行为" -任何事情都可能因此发生。这本身可以被称为"保障"(尽管这是一个延伸),通过以下思路:
gcc有一个-ftrapv
编译开关,当整数溢出发生时,它会使程序崩溃。这允许您轻松地调试溢出。这个特性是可能的,因为c++使在这些情况下使程序崩溃是合法的(根据未定义行为的性质)。我认为c++委员会在制定c++标准的这一部分时就考虑到了这一点。
这与Java不同,在Java中,整数溢出会导致绕行,并且可能更难调试。
相关文章:
- 如何反转整数参数包
- enum是C++中的宏变量还是整数变量
- 努力将整数转换为链表。不知道我在这里做错了什么
- 整数不会重复超过随机数
- 在C++中手动调整数组大小
- 检查输入是否不是整数或数字
- C++使用整数的压缩数组初始化对象
- 在C++中,将大的无符号浮点数四舍五入为整数的最佳方法是什么
- 将"打开的CV图像"中的"颜色"转换为整数格式
- 有充分的理由在h文件中使用include保护而不是cpp文件吗
- 为什么在保护模式下继承升级不起作用
- 通过套接字[TCP]传输数据 如何在C / C ++中打包多个整数并使用send() recv()传输数据
- 访问被拒绝后,c++中的故障保护代码
- C++:无法访问声明的受保护成员
- 如何只允许用户输入正整数
- 如何在c++中从文本文件中逐行读取整数
- C++:如何循环通过向量中的整数元素
- 我可以信任表示整数的浮点或双精度来保持精度吗
- do整数读取需要受到关键部分的保护
- c++保护超出整数限制