Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does my C++ program crash on one machine and not on another?

Tags:

c++

I have written a simple C++ program that I am compiling using g++ in command prompt and also running it through command prompt. The code of my program is as follows:

#include<iostream>

int main() 
{
    std::cout<<"Hello world"<<std::endl;
    return 0;
}

When I run this code. I get a "hello_world.exe has stopped working" kind of error on my office machine. But when I run the same portion of code at home it works fine. Any idea why this is happening? Also, if I remove std::endl it works fine.

I am using Notepad++ to code.

UPDATE: I am not running the same binary on both machines. I compile on both the machines separately. I am using windows 7 32-bit at both the locations. I am using mingw. For compiling I type "g++ hello_world.cpp -o hello_world.exe". For running I typed "hello_world.exe". I downloaded mingw from the site mingw.org and used the "mingw-get-setup.exe" to install. And I installed g++ and gcc through the command prompt using the command "mingw-get install gcc g++".

like image 315
Shaun Avatar asked Sep 30 '13 13:09

Shaun


People also ask

How can you prevent a program from crashing when it gets an error?

Don't run it. OR characterize what needs to be done to cause a crash and avoid doing that. OR analyze the crash state and fix the code or the hardware which might be causing bad behaviour on the part of the software.

What causes a program to crash?

Typical causes include accessing invalid memory addresses, incorrect address values in the program counter, buffer overflow, overwriting a portion of the affected program code due to an earlier bug, executing invalid machine instructions (an illegal opcode), or triggering an unhandled exception.

What happens when a program crashes?

When a program crashes, something unexpected has happened which the program itself is not equipped to handle; when the operating system detects such an event, it (usually) terminates the program.

Why does my C++ program crash?

When a program uses more space than available on the stack then the stack is said to overflow and cause the program to crash. The most common cause is infinite recursion. The following program contains infinite calls to function factorial().


2 Answers

When you return from main(), your program stops working. In a gui-based environment, I wouldn't be surprised to see a pop-up message warning about a terminal-based application reaching completion where the user has to click "dismiss" before the terminal spawned to support the application is terminated as well. Windows 9x used to have such checkboxes in launcher preferences for MS-DOS programs.

Questions you should use to find out the issue are: - Is it showing the same error message if you launch the shell yourself ? - Do you use the very same binary on both machine, and if so, are your machines both capable of running it (e.g. not trying to launch a 64-bit binary on a 32-bit OS as one of the case)

like image 70
PypeBros Avatar answered Oct 29 '22 10:10

PypeBros


It would help to see the exact text of the error message.

Your program depends on C and C++ runtime libraries. I suspect you have the libraries installed on the machine where it works and don't where it doesn't, probably because you installed Visual Studio on the machine where you wrote the code but not on the machine where you're trying to run it.

You can install the runtime libraries on the second machine by search Microsoft Download for vcredist for the version of Visual Studio that you compiled the program with.

like image 39
Adrian McCarthy Avatar answered Oct 29 '22 11:10

Adrian McCarthy