Strange problem in C

Jun 15, 2008 at 6:00 AM Thread Starter Post #1 of 7

Clutz

Tells us when we're offset.
Joined
Jun 7, 2002
Posts
2,483
Likes
13
I'm having a very odd problem in a C program I'm working on. It's a pretty old program from when I first was learning C, so I know that it doesn't have the best style/formatting, but I also know that the program does what I need it to do, other than this one strange problem I'm having. I also know that I didn't have this problem with this program with a previous version of GCC, so I think this may be something specific to GCC 4.1, but I'm not sure. Hopefully some of you more serious programmers will be able to help me.

Here is the snippet of code:

while (!feof(in1))
{
printf("starting end of file status check= %d\n", feof(in1));
flushall();
fscanf(in1, "%s %d %g %g %g %g %d %g %g %g %d %g %g",&outputFileName, &startupN, &startupMU, &startupR, &m1cost, &E, &repeats, &fit_type, &repro_type, &mort_type, &rnNum, &initialFitness, &repStoch);
openFile2(outputFileName);
modelExecution(outputFileName, startupN, startupMU, startupR, m1cost, E, I, repeats, fit_type, repro_type, mort_type, rnNum, initialFitness, repStoch);

printf("\ne\n");
printf("error in stream? %d\n", ferror(in1));
printf("end of file status check= %d\n", feof(in1));
printf("\ne2\n");
}

The error is coming from either feof or ferror. I am getting a bus error as I am trying print either the "error in stream?" line or the "end of status check" if I delete the ferror line. If I delete both of these lines, then the program fails back when it goes to the while statement which is containing the feof check. It is *not* at the end of the file, other stuff exists in the file after this on a new line.

Even if I'm doing something stupid with feof in the while statement (and I can't see it if I am), there shouldn't be any problem with printing out the status of feof, should there?

thanks!
Brad
 
Jun 15, 2008 at 9:44 AM Post #2 of 7
I have seen this error once before (maybe twice, can't remember).
Try changing the level of optimisation the compiler uses... change it to a higher level -O3 and it may just disappear
wink.gif

Even try turning optimisation off, just vary it.

I've seen some very strange things happen using gcc. When I saw the bus error before it disappeared by changing the level of optimisation.
No guarantees though sorry. I haven't looked into this error since I first saw it and someone knowledgeable told me it doesn't usually reflect error in the code.
 
Jun 15, 2008 at 4:08 PM Post #3 of 7
Hi Silence,

I've tried this - gone with -O0, -O1, -O3, -O3, and even -Os, and none of it makes any difference.

I've been working with it some more, and I have a problem even if I use fgets. The first fgets works fine, which should read the first line - but the second fgets (and there are multiple lines also causes a Bus error - so there is some problem with reading this file.

I've even tried moving the files to a different folder, thinking it could be some weird hardware problem with the hard drive (a problem with that particular sector) - but no bones.

Any other suggestions?
 
Jun 15, 2008 at 4:35 PM Post #4 of 7
This seems to be a strange problem specific to GCC on Macintosh computers. I've tried this bit of code on five different Macs, a Mac Pro with two dual core processors; a dual G4 867 PowerMac, a Macbook Pro, an older G4 733 machine, and a dual G5. I've tried with various different level of optimizations on all cases (-Os, -O0, -O1, -O2, -O3) and in none of the cases does it work.

The cluster I usually run on (glacier on westgrid) is down right now because the building is undergoing a major power system overhaul, so I can't try it on the system I would usually use. But I remembered there are other systems on westgrid (Westgrid.ca) that I can try and run the code on, so I went to another cluster (matrix.westgrid.ca) and boom - it works just fine. It also works great on robson.westgrid.ca.

So this seems to be a problem specific to macs.

Anybody have any idea how I could complain about this problem to Apple?

Brad
 
Jun 16, 2008 at 1:08 AM Post #6 of 7
I'll have to do that sometime this week. This bug really irritated me because I spent all day yesterday working on it. I even tried re-writing huge chunks of the code other places (resulted in better code, although like I said before - it worked, it didn't need to be better) to see if it was some sort of strange, strange bug elsewhere that was rearing it's ugly head at feof (or any other file access) stage.

But no.

Thanks ingwe
 
Jun 16, 2008 at 2:42 AM Post #7 of 7
Quote:

Originally Posted by Clutz /img/forum/go_quote.gif
I'll have to do that sometime this week. This bug really irritated me because I spent all day yesterday working on it. I even tried re-writing huge chunks of the code other places (resulted in better code, although like I said before - it worked, it didn't need to be better) to see if it was some sort of strange, strange bug elsewhere that was rearing it's ugly head at feof (or any other file access) stage.

But no.

Thanks ingwe



No prob bro.
smily_headphones1.gif


I go through similar frustrations on a weekly basis, it seems. I truly empathize.
 

Users who are viewing this thread

Back
Top