Unhandled exception at 0x003a1957 in Nouveau.exe: 0xC00000FD: Stack overflow.

Hello everyone,

I'm new to C++. I usually program in MATLAB. So a friend gave me a code that transforms a text file called plasmaforce.dat to a binary file called plasmaforce_b.dat (I don't know how to program in C++ but I can understand '' the big lines '').

The text file contains 161620 lines; and each line contains 4 numbers like this:
3.96842000E+05,3.00231024E-32,-1.22247701E-30,-3.00231024E-32

When I run the code, I have this msg:
Unhandled exception at 0x003a1957 in Nouveau.exe: 0xC00000FD: Stack overflow.

Also, I have noticed that if I reduce the number of lines in my text file, let's say to 35901 (each line still contains 4 numbers); the program works.

I don't know what's the problem.. I would appreciate if someone can help me on this please. It's for a research project for my university.

Here's the C++ code:

Note that at third line, we have: #define NUM 161620*4
161620 is the number of lines in the text file. We can change this number if the number of lines is different from 161620.
4 is the number of numbers in each line.



______________________
#include<stdio.h>
#include<stdlib.h>
#define NUM 161620*4
void main()
{
FILE *fp;
char temp[20] ;
int i =0;
for(i=0; i<20; i++)
temp[i]='0';
if( (fp=fopen("plasmaforce.dat","r")) == NULL)
{
printf("failed to open file!\n");
return;
}
double result[NUM];
int ri=0;
int ti=0;


char ch = fgetc(fp);
while(ch != EOF)
{
if(ch == ',' || ch =='\n')
{
temp[ti++] ='\0';
result[ri++] = strtod(temp, NULL);
ti=0;
}
else
temp[ti++]=ch;
ch = fgetc(fp);
}

for(i=0; i<ri; i++)
printf("%5.8e\n", result[i]);

fp=fopen("plasmaforce_b.dat", "wb");
fwrite(result, sizeof(double), ri, fp);
fclose(fp);

return;
}


_______________

Thank you very much,
Sincerly,
JunMi


Last edited on
Does the prorgam just create a binary file of those doubles?

If that's the case, don't store them all in memory, you just need enough space to help one, right?
Hello kbw,

I didn't understand your question totally ... This program ''normally'' must creates one binary file.
In my case, I need all the information in one binary file because after that, I'll load this file into Fluent, Ansys (a numerical solver).

How can I upload the text file that I'm using, so people can try the program if they have time to try to know where the problem is ?

Thank you,

JunMi
You don't need to upload the text file that you're using. The problem is that you are allocating a very large array on a stack that is not large enough to hold the array.

There are several possible solutions. Check your compiler's documentation on how to increase the size of the stack and ensure that it is large enough for your purposes. Remove your buffer from the stack (use a vector instead of an array.) Or, as kbw was suggesting, there is no reason to store every double read from the file in memory at the same time. You could simply read one from the source file and write it to the destination file, which would only require ever holding one double in memory.
Topic archived. No new replies allowed.