This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

why does my program fails when using -O2


Hola:

I hope you can help me with this program:
- it works OK if I compile it with: g++ main2.cpp -o main2
- it fails if I compile it with: g++ -O2 main2.cpp -o main2
(but if I enable the comment inside the "try", then the program works.)


This happens in my FC4 box with g++ --version g++ (GCC) 4.0.2 20051125 (Red Hat 4.0.2-8)

On my FC3 boxes and my RH7.3 boxes the -O2 is OK.

Any help is appreciated

Thanks.

-------------------------------------------------------------------------


#include<string> #include<iostream> #include<fstream>

using namespace std;

int refDates[20000];

int getRefDates()
{

 // JAM 2003-01-23 added search for SPX_DAILY_DATES.asc on /usr/local/etc
 // get the reference date series
 string filespec =  "/usr/local/etc/SPX_DAILY_DATES.asc";

 ifstream *ifile;
 ifstream ifile1(filespec.c_str());

 ifile = &ifile1;
 if (!ifile->is_open())
 {

cout << "ERROR: could not find varset date reference file at: " << filespec <<" or at: "<< endl;
return(-1);
}


 cout<<"MainClass.cpp::getRefDates: Using "<<filespec<<endl;
 string sDate;

 int count = 0;
 int i = 0;
 for (i = 0; ;++i)
 {

refDates[i] = 0;

std::getline(*ifile, sDate);

cout << "ref date Line: " << sDate <<" count: "<<count<< endl;

if (ifile->bad() || ifile->eof())
{
break;
}
//cout << "ref date Line: " << sDate <<" i:"<<i<<" count: "<<count<<" refDates:"<<refDates[i]<<" prior refDate ="<<refDates[i-1]<< endl;


try
{
//char str[8];
//memcpy(str,sDate.c_str(),8);
//refDates[i] = (int)atoi(str);
refDates[i] = (int)atoi(sDate.c_str());
//cout<<count<<" "<<i<<" "<<refDates[i]<<" "<< atoi(sDate.c_str())<<" "<< sDate.c_str()<<endl;
++count;
}
catch(...)
{
// error in date fetch
cout << "ERROR reading reference date from: "<<filespec<<" line number: "<<count<< endl;
return(-1);
}


}

//cout<<"finished loading "<<count-1<<" "<<refDates[0]<<" "<<refDates[count-1]<<" "<<refDates[count-2]<<endl;

 int lastdate = refDates[count-1];
 int firstdate = refDates[0];
 refDates[count] = 0;

cout<<count<<" "<<i<<" "<<refDates[0]<<" "<<refDates[count-1]<<" "<< refDates[count-2]<<" "<< refDates[count-3]<<" "<<refDates[count-4]<<endl;
//cout<<"first "<<testdef->Vars.GetNumber("StartDate")<<" "<<(int)testdef->Vars.GetNumber("StartDate")<<" "<<firstdate<<endl;
if (firstdate > 20061031)
{
cout << "ERROR: First Date reference ("<<firstdate<<") is after test's start date"<<20061031<<"), dates file="<< filespec << endl;
return(0);
}
//cout<<"last "<<testdef->Vars.GetNumber("EndDate")<<" "<<(int)testdef->Vars.GetNumber("EndDate")<<" "<<lastdate<<endl;
if (lastdate < 19901231)
{
cout << "ERROR: Last Date reference ("<<lastdate<<") is before test's end date("<<19901231<<"), dates file="<< filespec << endl;
cout<<" i="<<i<<" count="<<count<<endl;
return(0);
}
// MSE !!! add date error checks


ifile1.close();

 // ok
 return count;

}

int main()
{
 cout<<" Returned: "<<getRefDates()<<endl;
}


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]