问题描述
我编写一个简单的C ++程序,它将动态内存分配给我的程序,然后删除这个内存。这是我的计划:
I am writing a simple C++ Program which allocates dynamic memory to my Program and then deletes this memory. Here is my Program:
#include <iostream>
#include <new>
using namespace std;
int main ()
{
int i,n;
int * p;
cout << "How many numbers would you like to type? ";
cin >> i;
p= new (nothrow) int[i];
if (p == nullptr)
cout << "Error: memory could not be allocated";
else
{
for (n=0; n<i; n++)
{
cout << "Enter number: ";
cin >> p[n];
}
cout << "You have entered: ";
for (n=0; n<i; n++)
cout << p[n] << ", ";
delete[] p;
}
return 0;
}
在上述程序中,当我输入i的值等于或小于20亿比这个计划工作正如预期。然而,当我输入高于20亿的任何东西,如30亿或更高,这个程序进入无限循环,没有在我的for循环中输入数字。
In the above program when I input the value of i (Number of inputs) equal to or less than 2 Billion than this program works as expected. However when I enter anything above 2 Billion like 3 Billion or higher, this program goes in an infinite loop without taking a number input in my for loop.
我期望这个程序失败,当我进入一个非常高的价值,因为它不能分配内存。
I am expecting this program to fail when I enter a very high value for i by saying it could not allocate the memory.
根据我的理解,我想当我进入一个非常高的价值的int i,我将走出整数数据类型,但仍然在这种情况下,它应该从for循环中我的数字输入,因为我有一个cin语句,而不是进入循环或内存分配应该简单地失败。
As per my understanding, I think when I enter a very high value of int i, I am going out of bound for integer data type but still in this case, it should take number input from me in for loop as I have a cin statement there instead of going in for loop or memory allocation should fail simply.
当我将i的类型从int更改为long时,它很有效,但我很想知道为int类型,为什么它进入无限循环,而不是取值当它看到cin in for循环?
When I changed type of i from int to long then it works but I am curious to know for i of type int, why it goes in infinite loop instead of taking values when it sees cin in for loop?
我在Mac OS X上运行这个程序,并使用g ++编译器编译它。
I am running this program on Mac OS X and compiling it using g++ compiler.
推荐答案
1)您尝试分配给 int
大于2147483647的值,这通常是此类型的最大值。
一般来说,如果你想处理这么大的数字,你应该使用 long long int
(或者从< cstdint>
以便更好的可移植性)。
1) You are trying assign to int
value bigger than 2147483647, which is usually maximum value for this type.Generally, if you want to handle such a large numbers, you should use long long int
(or something from <cstdint>
for better portability).
2)您不清除 cin
它失败。
代码bollow生成无限循环:
2) You don't clear state of cin
after it fails.The code bollow generate infinite loop:
int i = 0;
while (i <= 0)
{
std::cout << "Enter a number greater than 10..." << std::endl;
std::cin >> i;
}
您可以这样解决:
int i = 0;
while (i <= 0)
{
std::cout << "Enter a number greater than 10..." << std::endl;
if (!(std::cin >> i))
{
std::cin.clear(); // Clear error flag
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // Remove incorrect data from buffer
}
}
试图创建一个真正的大数组。你需要几个GiB的
连续内存。即使你成功分配数组,它仍然是一个设计问题。您应该使用许多较小的数组或使用/创建一个合适的容器。
3) You are trying to create a really big array. You need a few GiB ofcontiguous memory to this. Even if you succeed allocate the array, it is still a design issue. You should use many of smaller arrays or use/create a suitable container.
这篇关于无限循环,不接受输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!