In a program I am writing, the one line of declaring an ifstream object causes the program to "crash" and a non-zero return code to be returned. Instead of pasting the full program here, I'll paste a sample program that "crashes" just the same.

I put "crash" in quotations because the program will run to completion, but I will get an error message in Vista saying "your program has stopped working" and the program will return a non-zero error. In XP, I don't get the pop-up message, but I do get the non-zero number returned. It's the same non-zero return code each time, but I haven't figured out what it means.

First, some background info:
OS this happens in: Vista and XP
IDE being used: Codeblocks 8.02
Compilers tried: GCC and Borland
Main returns: -1073741819

Second, here is the program that fails with the aforementioned error:

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    ifstream inFile;

    cout << "Hello world!" << endl;
    return 0;
}

In the console, I get:

Hello world!

Process returned -1073741819 (0xC0000005) execution time : 4.137 s
Press any key to continue.
If I remove the ifstream declaration, I get:

Hello world!

Process returned 0 (0x0) execution time : 0.051 s
Press any key to continue.

I tried it with both VC++ 2008 Express and Code::Blocks 8.02 on Vista Home Premium. No problems or errors. With both compilers I first created a project and compiled for debug.
This is what Code::Blocks console window

Hello world!

Process returned 0 (0x0) execution time : 0.040 s
Press any key to continue.

Do one thing:
Go to the compiler and just try to comment out the line ifstream inFile; Check if it runs or not. If it does, tell us. If it doesn't Try to comment-out #include <fstream> .
If it now runs, tell us. If it doesn't, there is some problem in your compiler or perhaps the linker as it cannot run a simple helloworld program.

I tried the same code on the same IDE (same version as well), with the same compiler, running under Windows XP, and it executed perfectly. Check the integrity of your copy of GCC, and if it isn't, try installing it again.

Thanks to all for your replies and trials.

Do one thing:
Go to the compiler and just try to comment out the line ifstream inFile; Check if it runs or not. If it does, tell us. If it doesn't Try to comment-out #include <fstream> .
If it now runs, tell us. If it doesn't, there is some problem in your compiler or perhaps the linker as it cannot run a simple helloworld program.

I did try that before and found it ran fine without the ifstream declaration.

What are the exact versions and compiler commands executed?

Borland 5.5 and and I'm not sure which version of GCC codeblocks is using, but my version of codeblocks is 8.02 downloaded a month ago. Strange, but when I select the GCC compiler in codeblocks, the executable is mingw32-g++.exe - so I'm not sure exactly what this means. For the Borland compiler, the command used by codeblocks is:

bcc32.exe -q -w -x -O2 -I"C:\Program Files\Borland\BDS\4.0\include" -oobj\Release\main.obj -c main.cpp

I've reinstalled codeblocks a few times, so that's not it. I did manually run the Borland compiler with the command:

bcc32 -If:\Borland\bcc55\include -Lf:\Borland\bcc55\Lib <filename>

and ran it from the command line and that version ran successfully. I tried running the codeblocks-compiled code (same program, just compiled within codeblocks instead of manually at the command line), and that failed just like when run from within codeblocks.

So, my next step is to take the command run by codeblocks, examine it, try it in the command line by itself, and work from there. I just haven't had time to do it yet. If anyone has any predictions, thoughts, or insights, feel free to offer them. There's a ton of work-arounds to this problem, but it really bothers me that this error exists and I at least want to know why. Thanks again.

I just installed BCC55 compiled and ran your program with no problems. Here's what I did

C:\Borland\BCC55>bcc32 main.cpp
Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
main.cpp:
Turbo Incremental Link 5.00 Copyright (c) 1997, 2000 Borland

C:\Borland\BCC55>dir *.exe
 Volume in drive C is HP
 Volume Serial Number is FA09-7490

 Directory of C:\Borland\BCC55

05/25/2009  08:54 PM           114,176 main.exe
               1 File(s)        114,176 bytes
               0 Dir(s)  526,502,633,472 bytes free

C:\Borland\BCC55>main
Hello world!

C:\Borland\BCC55>

I tried it in Dev C++ in my Windows Vista computer, and it worked great, no error whatsoever, so I am not sure why it doesn't work on yours ...

To the program you mention, I added system("PAUSE");

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    ifstream inFile;

    cout << "Hello world!" << endl;
    
    system("PAUSE");
    
    return 0;
}

And there were no errors, the output was:

Hello world!

Press any key to continue ...

I have a question, why are you using ifstream if you actually never use it in the program to write to any file? It's useless then, in this specific code.

In response to the above question, the program provided is just an example program to illustrate the bug. That is why I declare ifstream. There is no logical purpose, just an illustration of the problem I'm having.

I've found a work-around, so I'm going to go forward with that. Here's the epilogue to this thread:

1) I narrowed down the problem to the Borland linker, ilink32.exe. I did this by copying the two commands from borland (one command for the compiler, and one for the linker) and trying them outside of codeblocks. The compiler commands (bcc32 -c) both outside and inside codeblocks produce almost identical files, so I concluded that isn't the problem. However the linker command, when used both outside and inside codeblocks, produces an executable with the same error -- so, the this is how I narrowed the problem down to the linker (and not a codeblocks bug). Strangely, when I simply call bcc32 without the "-c" option, the executable created works fine. So, compiling using bcc32 to an object file, then linking to an executable is when the problem happens. I'd love to find out exactly why, but I must move on.

2) Why did I say earlier that it failed with the GCC compiler as well? This may be a bug in codeblocks. In mid project, I switched my compiler from borland to GCC, and at this point the program didn't even compile. I misread the results, and assumed my error was the same. It wasn't. Not sure how I missed this, actually. But, when I started a new codeblocks project with GCC from the start, the program worked fine. So here I conclude that GCC does in fact work fine.

Thanks to all who helped, especially to Stinomus who asked me to look at the command line for the compiler. The information I provided back to you was incomplete, but it did eventually point me to seeing that something most likely went wrong in the linking process.

Comments
Great followup.

I had the exact same problem: simply instantiating an fstream object
caused a runtime error even though it compiled and linked just fine.
I compared the default command line options of codeblocks to an old
makefile I had and simply changed the default command line options
in the codeblocks setup:

In Code Blocks go to Project/Build Options/Linker Settings.
In the Link libraries you will see the default library "cw32mt.lib".
Click on it and edit it to say just "cw32.lib". Recompile your program
and you should get it to run without any errors being thrown.

I'm not sure exactly why the default library has problems with ifstream
instantiation, let me know if you have time to chase that one down.

-James Phillips
C++ Warrior

This article has been dead for over six months. Start a new discussion instead.