I have an issue that I’m basically baffled by. To start off, I have two global arrays – trustArray[] and fashionArray[]. Here is the function that fills the trustArray:
void getTrust()
{
string line;
int reachedTrust=0;
int numberOfTrustsRecorded=0;
ifstream myfile ("BuyingJeans.Hw5 (1).csv");
if (myfile.is_open())
{
while ( myfile.good() )
{
getline (myfile,line,',');
//int found=line.find("Like-Purchase");
if (line=="Trust-Like"){
reachedTrust=1;
getline (myfile,line,',');
}
if(reachedTrust==1){
if(numberOfTrustsRecorded <6){
double testValue = atof(line.c_str());
trustArray[numberOfTrustsRecorded] = testValue;
numberOfTrustsRecorded++;
}
}
}
myfile.close();
}
else
cout << "Unable to open file";
}
For some reason, the atof() in this function is changing two of the values in fashionArray[]. If I change the atof() to say, an atoi(), the problem no longer occurs. Here is the method that fills the array that is being changed (fashionArray[]):
void getFashion(){
string line;
int reachedFashion=0;
int numberOfFashionsRecorded=0;
ifstream myfile ("BuyingJeans.Hw5 (1).csv");
if (myfile.is_open())
{
while ( myfile.good() )
{
getline (myfile,line,',');
if (line=="Like-Fash -->"){
reachedFashion=1;
getline (myfile,line,',');
//cout<<line<<endl;
//getchar();
}
if(reachedFashion==1){
if(numberOfFashionsRecorded <6){
fashionArray[numberOfFashionsRecorded] = atoi(line.c_str());
numberOfFashionsRecorded++;
}
}
}
myfile.close();
}
else cout << "Unable to open file";
}
Here is the main method that calls these two methods:
int main () {
getFashion();
getTrust();
for(int x=0; x<6;x++)
cout<<fashionArray[x]<<endl;
getchar();
return 0;
}
The first two values of fashionArray end up being changed to some ridiculously large negative and positive integers. One interesting thing is that if I reverse the order in which the two methods are called in the main() method, the issue no longer occurs. Anyone have any idea what could be causing this?
I think you are writing beyond
trustArrayand intofashionArray. You have not provided initialisation code (please do), but I suppose it looks something like this :With N equal to some positive integer. My guess is that in your case,
N=5.In your loop, you test that
numberOfTrustsRecorded < 6. Values ofnumberOfTrustsRecordedthat will pass that test are 0, 1, 2, 3, 4, 5. That’s six (6) floats to fit in a array of 5. WritingnumberOfTrustRecorded[5]will overwrite memory. Change your test or increase your buffer size to 6.Why aren’t you seeing valid values in fashionArray[0] ? I don’t know. Maybe your compiler aligned the fashionArray memory buffer so the overwrite starts in unused memory, leaving you with half the bits required to make a IEEE float. Whatever bits are in memory make up a random float. A memory dump would show the problem.
Why is running the method in reverse order works ? Probably that the bug is still there, but running
getFashion()second cleans the mess left bygetTrust(). The memory you overwrite is yours, so as long as you don’t try to make sense of it, nobody complains. InitializefashionArray[0] = 0.0, rungetTrust()and look atfashionArray[0]before runninggetFashion(). You will probably see the random floats.