Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Program Crashing From Running Too Fast?

Reply
Thread Tools

Program Crashing From Running Too Fast?

 
 
Daragoth
Guest
Posts: n/a
 
      08-24-2004
Hi, I'm writing a program using Metrowerks CodeWarrior 4.0 that
determines the best possible combination of a data set by checking
every possible combination. I found there were about 250,000,000
possibilities and decided to insert a cout in the loop that gave the
percentage that had been completed. The program worked, but I soon
realized it would take almost a month of execution time for it to
finish. I figured the cout was slowing the program down so I decided
to make it occur less by mod'ing the program's overall count number
with a large number in an if statement and seeing if it was equal to 0
and putting the cout inside (ie. if (numTries % 10000 == 0) { cout <<
....; }). However, after doing so the program would crash with code
"c0000005" (I later found even using mod 2 would cause it to crash).
I then commented off the if and cout statements, but it still crashed.
When I put the cout statement back by itself it worked fine again. I
then checked with the debugger to see where it was crashing and it
turned out to be a simple addition assignment without overflow. So my
only idea is that the program is running too fast. But due to the
nature of my program, I need it to run as fast as possible. Anyone
know what causes this to happen and how to fix it?

Also, because I would like to be able to pause the program as well as
send various other user-input commands to it without stopping it, is
it possible for a console window to accept keyboard input without
halting execution of the program? I would prefer not to have to alter
the entire program to have it run in a window. Also, is there an
equivalent in CodeWarrior to the clrscr() statement?

Thanks a lot for any help.

Sincerely,
Darien A. Gothia
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      08-24-2004
Daragoth wrote:
> Hi, I'm writing a program using Metrowerks CodeWarrior 4.0 that


Usually, in this newsgroup it doesn't matter what you use unless
it is a known bug in the compiler that causes your problems (which
in this case it might be).

> [...] Anyone
> know what causes this to happen and how to fix it?


I've encountered only one situation in my whole career when changing
the code to output something fixed some problem I couldn't otherwise
detect or debug. It was faulty optimization that the C++ compiler
applied and a variable didn't get its value stored. Adding a printf
or a cout statement caused the optimizer to choke on a long sequence
of statements and not "optimize away" storing the correct value in
memory.

Whether you're experiencing the same situation, I don't know. Try
disabling some optimizations and see if the situation changes. Sheer
speed of an algorithm cannot change the outcome of the system if it
is single-threaded.

> Also, because I would like to be able to pause the program as well as
> send various other user-input commands to it without stopping it, is
> it possible for a console window to accept keyboard input without
> halting execution of the program? I would prefer not to have to alter
> the entire program to have it run in a window. Also, is there an
> equivalent in CodeWarrior to the clrscr() statement?


There is nothing of that sort in the Standard C++ language.

Victor
 
Reply With Quote
 
 
 
 
Mad Hamish
Guest
Posts: n/a
 
      08-25-2004
On 24 Aug 2004 12:52:06 -0700, http://www.velocityreviews.com/forums/(E-Mail Removed) (Daragoth)
wrote:

>Hi, I'm writing a program using Metrowerks CodeWarrior 4.0 that
>determines the best possible combination of a data set by checking
>every possible combination. I found there were about 250,000,000
>possibilities and decided to insert a cout in the loop that gave the
>percentage that had been completed. The program worked, but I soon
>realized it would take almost a month of execution time for it to
>finish. I figured the cout was slowing the program down so I decided
>to make it occur less by mod'ing the program's overall count number
>with a large number in an if statement and seeing if it was equal to 0
>and putting the cout inside (ie. if (numTries % 10000 == 0) { cout <<
>...; }). However, after doing so the program would crash with code
>"c0000005" (I later found even using mod 2 would cause it to crash).
>I then commented off the if and cout statements, but it still crashed.
> When I put the cout statement back by itself it worked fine again. I
>then checked with the debugger to see where it was crashing and it
>turned out to be a simple addition assignment without overflow. So my
>only idea is that the program is running too fast. But due to the
>nature of my program, I need it to run as fast as possible. Anyone
>know what causes this to happen and how to fix it?


Your best bet is to find the minimal source that will produce the
error and repost it here.
 
Reply With Quote
 
Old Wolf
Guest
Posts: n/a
 
      08-25-2004
(E-Mail Removed) (Daragoth) wrote:

> [My program fails when some debugging is added]
>
> However, after doing so the program would crash with code "c0000005"


ISTR this is the Windows code for a segfault.

> So my only idea is that the program is running too fast.


Unlikely, to say the least.

> Anyone know what causes this to happen and how to fix it?


Most likely, you have a bug in your program elsewhere that causes
memory corruption, and it only shows up under some circumstances.

Post your code (or preferably, the smallest subset of your code
that shows the problem).

> Also, because I would like to be able to pause the program as well as
> send various other user-input commands to it without stopping it, is
> it possible for a console window to accept keyboard input without
> halting execution of the program? I would prefer not to have to alter
> the entire program to have it run in a window.


You would have to create a background processing thread, or
use a system-specific function that can poll the keyboard
without waiting.
 
Reply With Quote
 
Dave Vandervies
Guest
Posts: n/a
 
      08-25-2004
In article <(E-Mail Removed) >,
Old Wolf <(E-Mail Removed)> wrote:
>(E-Mail Removed) (Daragoth) wrote:


>> Also, because I would like to be able to pause the program as well as
>> send various other user-input commands to it without stopping it, is
>> it possible for a console window to accept keyboard input without
>> halting execution of the program? I would prefer not to have to alter
>> the entire program to have it run in a window.

>
>You would have to create a background processing thread, or
>use a system-specific function that can poll the keyboard
>without waiting.


Also potentially workable, and less unportable, is to install a handler
for the interrupt signal that sets a "user wants to say something" flag,
and periodically check this flag and stop for user input if it's set.

(The user needs to know to say "Okay, get going again now", since the
program will have to suspend normal operation while it's waiting for
user input.)


dave

--
Dave Vandervies (E-Mail Removed)

You can start a flame war in comp.lang.c just by saying "good morning".
--Richard Heathfield in comp.lang.c
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Program running too slowly? Just give it another core! (NOT!) Paulo ASP .Net 7 01-18-2009 12:23 AM
Program Crashing when freeing memory vashwath@rediffmail.com C Programming 4 12-02-2005 05:21 AM
why this program is crashing James C Programming 6 04-18-2005 01:47 AM
why this program is not crashing ghyott@yahoo.com C Programming 20 02-21-2005 07:25 AM
coins program, need some help with crashing Shark1 C Programming 3 01-20-2005 07:18 PM



Advertisments