将大文件读入数组会导致崩溃 [英] Reading a large file into an array causes a crash

查看:94
本文介绍了将大文件读入数组会导致崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个作业,我需要采取一个7位数字输入(电话号码),并检查它是否在pi的数字中找到。 pi的数字存储在所提供的空格分隔的文本文件中。它看起来相当简单:将输入拆分为数组,将pi的数字读入数组,然后检查是否找到匹配项。长话短说,我得到的程序工作,我的满意。我们提供了文本文档,其中pi的数字为10,100的倍数,等等,最多1百万个数字。我的程序工作高达100,000位数。但无论什么原因,在1百万数字文件,它崩溃与一个通用的Windows错误。我没有信息为什么它崩溃,没有给出错误消息(除了通用的一个问题导致此程序停止工作消息)。



注意到分配状态我不能使用任何面向对象的代码,除了cin,cout和文件流对象(这个限制是因为我们还没有进入类,他们不希望我们使用函数,而不知道它们如何工作) 。



无论如何,我正在寻找洞察为什么程序崩溃。我对每个应该需要它们的变量使用long int(包括计数器和函数返回),这应该是足够的,因为它们可以上升到大约20亿,这里不应该有大于一百万的数字。 p>

感谢您的帮助。我已经在过去几个小时没有成功。

  const long int numberOfDigits = 1000000; 
int digitsOfPi [numberOfDigits];


解决方案

  int digitsOfPi [位数]; 

堆栈没有足够的空间容纳这么大的数组。该堆栈是存储自动变量(AKA本地变量)的位置。当执行进入一个函数时,内存会自动分配给局部变量,并在函数返回时释放。堆栈是伟大的,因为这种自动内存管理,但一个限制是它的大小有限。



大对象应该在 1 堆是一个巨大的内存池,您可以随时随地动态分配内存。堆和堆栈之间的区别是,你负责分配和释放堆内存。



要使用C ++在堆上分配内存,请使用 $ >拥有相应的 delete 一旦不再需要就释放内存。 (或在我们的案例中,我们使用 new [ ] delete [] ,因为我们处理数组。)

  //在堆上分配内存。 
int * digitsOfPi = new int [numberOfDigits];

//使用它。

//然后释放它。
delete [] digitsOfPi;

//或者更好的是,一旦允许使用STL ...
std :: vector< int> digitOfPi;更大的问题,但是,为什么你需要读取所有的数字π



<立即进入内存。更好的设计,虽然更麻烦的代码,只需要一个固定的O(1)内存量,例如,一次7位数。



>






1 您可以探索编译器的选项来增加堆栈大小,但这不是正确的解决方案。


I have a assignment where I need to take a 7 digit input (a phone number) and check if it's found in the digits of pi. The digits of pi are stored in a supplied space separated text file. It seems reasonably straightforward: break the input into an array, read the digits of pi into an array, and check if a match is found. Long story short, I got the program working to my satisfaction. We were supplied text documents with the digits of pi in multiples of 10, 100, and so on up to 1 million digits. My program works up to 100,000 digits. But for whatever reason, on the 1 million digit file, it crashes with a generic windows error. I have no information on why it crashes and no error message is given (except the generic "a problem caused this program to stop working" message).

Noting that limits on the assignment state I cannot use any object-orientated code except for cin, cout, and the file stream objects (this limitation is because we've yet to get into classes and they don't want us using functions without knowing how they work).

Anyway, I'm looking for insight as to why the program is crashing. I'm using long ints on every variable that should need them (including counters and function returns), which should be sufficient, since they can go up to roughly 2 billion and there should not be any numbers larger than a million here.

Thanks for any help. I've been at this the past few hours with no success.

const long int numberOfDigits = 1000000;
int digitsOfPi[numberOfDigits];

解决方案

int digitsOfPi[numberOfDigits];

The stack does not have enough room to hold such a large array. The stack is where automatic variables (AKA local variables) are stored. Memory is automatically allocated for local variables when execution enters a function and is freed when the function returns. The stack is great because of this automatic memory management, but one restriction is that its size is limited.

Large objects should go on the heap.1 The heap is a gigantic pool of memory from which you can allocate pieces dynamically whenever you like. The difference between the heap and the stack is that you're responsible for allocating and freeing heap memory. It does not get automatically freed for you.

To allocate memory on the heap in C++, use the new operator, with each new having a corresponding delete to free the memory once it's no longer needed. (Or in our case, we use new[] and delete[] since we're dealing with an array.)

// Allocate memory on the heap.
int *digitsOfPi = new int[numberOfDigits];

// Use it.

// Then free it.
delete[] digitsOfPi;

// Or better yet, once you're allowed to use the STL...
std::vector<int> digitsOfPi;

The larger question, though, is why you need to read all the digits of π into memory at once. A better design, though trickier to code, would only need a fixed O(1) amount of memory—say, 7 digits at a time.

See also


1 You could explore your compiler's options to increase the stack size, but that's not the right solution.

这篇关于将大文件读入数组会导致崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆