简体   繁体   English

C ++动态声明的数组无法正常工作

[英]c++ dynamically declared array fails to work

I am trying to read the file's data into a dynamically declared array, using double *data = new double[14141414]() declaration. 我正在尝试使用double *data = new double[14141414]()声明将文件的数据读取到动态声明的数组中。 Note, it is rather a large file; 注意,这是一个很大的文件。 hence large size of an array. 因此数组的大小很大。

The problem is I can not put all the data into an array as somewhere around index=14000000 the execution would just stop. 问题是我无法将所有数据放入数组,因为index = 14000000左右的地方执行会停止。
The code compiles just fine (no errors). 该代码可以很好地编译(没有错误)。 I did debug and the new returns an address, not 0 or NULL. 我做了调试, new函数返回一个地址,而不是0或NULL。 So looks like there is no problem with memory allocation (ie running out of memory). 因此,看起来内存分配没有问题(即内存不足)。 I even echo-ed the file to screen without array assignment just to see that I am able to read through the file well. 我什至在没有数组分配的情况下将文件回显到屏幕上,只是为了看清楚我能够很好地阅读文件。 All looks good. 一切看起来不错。

Yet, the moment I start putting data into an array, the program would just stop closer to the end but at random locations, Sometime it would be 14000000 sometimes the index would be a little bit more and sometimes a little bit less. 但是,当我开始将数据放入数组时,程序将停在最后,但会在随机位置出现,有时大约是14000000,有时索引会多一点,有时会少一点。 There were couple times when the program ran well. 有几次,程序运行良好。

Does anybody know what is going on? 有人知道发生了什么吗? I suspect the the computer running out of physical memory and hence this behavior of the program. 我怀疑计算机的物理内存不足,因此该程序无法正常运行。 But if this is so, then why does new operator return an address? 但是,如果是这样,那么new运算符为什么返回一个地址? Should it return 0 or NULL if memory allocate fails? 如果内存分配失败,它应该返回0还是NULL?

Thanks!! 谢谢!!

UPDATE: per #Jonathan Potter's request I am including the code here. 更新:根据#Jonathan Potter的要求,我在此处包括代码。 Thanks!! 谢谢!! Really good idea!! 真是个好主意!!

void importData(){

int totalLineCount = 14141414;

double *height = new (nothrow) double[totalLineCount]();
int *weight = new (nothrow) int[totalLineCount]();
double *pulse = new (nothrow) double[totalLineCount]();
string *dateTime = new (nothrow) string[totalLineCount];
int *year = new (nothrow) int[totalLineCount]();
int *month = new (nothrow) int[totalLineCount]();
int *day = new (nothrow) int[totalLineCount]();

fstream dataFile(file.location.c_str(), ios::in);
for (int i = 0; i < totalLineCount; i++) {      
  dataFile >> weight[i] 
      >> height[i] 
      >> pulse[i]
      >> year[i] 
      >> dateTime[i]; 
  } //for
dataFile.close();

delete height;
delete weight;
delete pulse;
delete dateTime;
delete year;
delete month;
delete day;

}//function end

save yourself loads of trouble, use a vector 节省麻烦,使用vector

std::vector<double> data;
data.reserve(SIZE_OF_ARRAY); // not totally required, but will speed up filling the values

vector will give you better debug messages and you won't have to deal with memory yourself. 向量将为您提供更好的调试消息,而您不必自己处理内存。

Your "new" memory allocation block need to correct as follows, there is no need of () at end of each line. 您的“新”内存分配块需要按以下方式更正,每行的末尾不需要()

double *height = new (nothrow) double[totalLineCount];
int *weight = new (nothrow) int[totalLineCount];
double *pulse = new (nothrow) double[totalLineCount];
string *dateTime = new (nothrow) string[totalLineCount];
int *year = new (nothrow) int[totalLineCount];
int *month = new (nothrow) int[totalLineCount];
int *day = new (nothrow) int[totalLineCount];

And you "delete" block need to correct as follows: 并且您“删除”块需要进行如下纠正:

delete [] height;
delete []weight[];
delete []pulse;
delete []dateTime;
delete []year;
delete []month;
delete []day;

I think improper delete operation may be reason for your failure. 我认为删除操作不当可能是您失败的原因。 You allocated memory for arrays but de-allocated by using pointer syntax of delete instead using array syntax. 您为数组分配了内存,但是使用delete的指针语法而不是使用数组语法来取消分配内存。

And another probability for the issue may be lack of physical memory, because as per code you are allocating a lot of memory, not only a double array as you mentioned earlier in question. 出现此问题的另一个可能性可能是物理内存不足,因为按照代码,您分配了很多内存,而不仅仅是前面提到的双精度数组。 There is an array of std::string and a few more. 有一个std :: string数组,还有更多。

To avoid all memory allocation and de-allocation hurdles better you can go for std::vector in place of array. 为了更好地避免所有内存分配和取消分配的障碍,您可以使用std::vector代替数组。 In one of your comments you raised concern of performance benefit by comparing array and std::vector. 在您的评论之一中,您通过比较array和std :: vector引起了对性能优势的关注。 If you are using compiler optimizations , (if in case of gcc -O2 ) std::vector will be at par with array unless you may make some serious mistake in your implementation. 如果使用的是编译器优化(如果使用gcc -O2 ),则std::vector将与array保持一致,除非您可能在实现中犯一些严重错误。

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

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