Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help on "Access Violation (Segmentation Fault)" :(

Status
Not open for further replies.
Oct 11, 2006
5
US
I wrote a C++ program in Dev-C++. I kept having the problem of access violation (segmentation fault) when I was running the program. :( The following was a segment at which the problem probably occurred.

int main(){
ofstream fout("output.txt");
for(int j=0;j<20;j++)
{
cout<<"SEED:"<<j+1<<endl;
srand(j+1);
testRun(fout);
}
fout.close();
system("pause");
return 0;
}

void testRun(ofstream& fout){

for (int s = 0; s < 1000; s++){
double parameter = l.getParameters(s);
float c=1;
float e=0.2;
int d=20;
if (fout.is_open())
{
fout << "step: \t" << s << "\n";
}
else
{
cout << "error!";
}
}
}

It seems the problem occurred at fout line, but I didn't get why. :( I tried to blocked the line "fout << "step: \t" << s << "\n";" and the program worked. However I really need to output the results to the file. Could anyone give me some suggestions? Thanks a lot!
 
I have no idea what the purpose of this line is:
Code:
double parameter = l.getParameters(s);

But when I comment out that line and run your code in VC++ 2005 express, I don't have any problems.
 
> if (fout.is_open())
Why do you do this every time? The place to check would be just after you opened the file, not 20000 times in the middle of your innermost loop.

> double parameter = l.getParameters(s);
1. You don't use the answer.
2. What is your 'l' object doing anyway.
The root cause is more likely to be in that class implementation, given cpjust's observations, and the lack of anything else obvious in your code.

Oh, and please use the [code][/code] tags when posting code.

--
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top