简体   繁体   English

C ++堆栈溢出未处理的大数组异常

[英]c++ stack overflow unhandled exception with large array

Right, having looked up this issue, I believe that it is caused because I have in my class definition wchar_t downloadedText[400000]; 是的,在查了这个问题之后,我相信是由于我的类定义中有wchar_t downloadedText[400000]; I have read solutions about how I should deal with this by using the new operator to assign the space, ie: wchar_t *downloadedText; downloadedText = new wchar_t[400000]; 我已经阅读了有关如何使用new运算符分配空间的解决方案,例如: wchar_t *downloadedText; downloadedText = new wchar_t[400000]; wchar_t *downloadedText; downloadedText = new wchar_t[400000];

However I need to write instances of the class to a file, and assigning the variable like above appears to use pointers to point to data stored in a way that does not get written to my file. 但是,我需要将类的实例写入文件,并且像上面那样分配变量似乎是使用指针来指向以未写入我的文件的方式存储的数据。 It is this same reason why I cannot use std::vector . 这就是为什么我不能使用std::vector同样原因。

I have read that another option I may have is that I can increase the size of the 'stack'. 我读过我可能拥有的另一种选择是,我可以增加“堆栈”的大小。 I use VS2010 as my IDE, and I located in my project properties > Linker > System 'Stack Commit Size', 'Stack Reserve Size', 'Heap Commit Size' and 'Heap Reserve Size' fields, but I am unsure if this is how I can deal with my issue, and if it is, what to correctly set the appropriate fields to. 我使用VS2010作为我的IDE,我位于项目属性>链接器>系统“堆栈提交大小”,“堆栈保留大小”,“堆提交大小”和“堆保留大小”字段中,但是我不确定是否如何处理我的问题,以及如何正确设置适当的字段。

If you must do it this way... You could just write the array explicitly, after writing out the object. 如果您必须以这种方式执行此操作,则可以...在写出对象之后,显式地编写数组。 eg. 例如。

write((char*)&myObjects[i]), sizeof(MyClass));
write((char*)downloadedText, sizeof(downloadedText[0]) * 400000);

And to read it back in: 并读回:

read((char*)&myObjects[i]), sizeof(MyClass));
downloadedText = new wchar_t[400000];
read((char*)downloadedText, sizeof(downloadedText[0]) * 400000);

However, this is very fragile and prone to errors. 但是,这非常脆弱并且容易出错。 Overwriting objects in memory as done by the read is at best bad form except perhaps if you're using a struct created explicitly for that purpose which would typically contain only PODs. 通过读取来覆盖内存中的对象最多是一种不好的形式,除非您使用的是为此目的而明确创建的结构,该结构通常仅包含POD。 As a minimum, note that you have to set the downloadedText member field after the read writes over it. 至少要注意,您必须在读取覆盖它之后设置downloadedText成员字段。

You can allocate the whole object with new operator in system heap but not on stack. 您可以使用new运算符在系统堆中分配整个对象,但不能在堆栈上分配。 In this case you can write this object to file in the same manner and have no troubles with stack overflow. 在这种情况下,您可以以相同的方式将此对象写入文件,并且不会出现堆栈溢出的麻烦。

Yes, you can increase the stack size in Visual Studio with the linker option /STACK . 是的,您可以使用链接器选项/STACK增加Visual Studio中的堆栈大小。 This linker option is also editable with the project properties Stack Reserve Size and Stack Commit Size . 此链接器选项还可以使用项目属性“ 堆栈保留大小”和“ 堆栈提交大小”进行编辑 It is enough to set the reserve size, the commit size is optional. 设置保留大小就足够了,提交大小是可选的。 Nevertheless you should also be able to use std::vector . 不过,您还应该能够使用std::vector

It sounds like you're fine with your current serialization strategy (make the object POD, and write it as a POD value). 听起来您对当前的序列化策略很满意(将对象设为POD,并将其写入POD值)。 In that case, your question really is "how can a keep these objects from taking up too much space on the stack?" 在那种情况下,您的问题确实是“如何防止这些对象在堆栈上占用过多的空间?” The answer: don't put them on the stack. 答案:不要将它们放在堆栈上。

Allocate them with new. 用新的分配它们。 Or, preferably, wrap them in smart pointers of some kind. 或者,最好将它们包装在某种类型的智能指针中。


You have string-like data. 您有类似字符串的数据。 It so happens that C++ offers a string class. 碰巧C ++提供了一个字符串类。 In fact, since you're talking about wchar_t characters, you want to look at std::wstring (in the <string> header). 实际上,由于您正在谈论wchar_t字符,因此您要查看std::wstring (在<string>标头中)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM