"variable = clock()" makes my program crash
Aug 7, 2009 at 11:58pm UTC
This is my Timer class:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
typedef clock_t TIMER_TYPE;
class Timer
{
public :
Timer();
~Timer();
void StartTimer();
double SecondsPassed();
unsigned int MillisecondsPassed();
private :
TIMER_TYPE start;
};
And some of the methods:
1 2 3 4 5 6 7 8 9 10 11 12
Timer::Timer()
{
start = 0;
log_info("Timer initialized." );
}
void Timer::StartTimer()
{
log_info("Starting timer... Time is %ld" , clock());
start = clock();
log_info("Timer started!" );
}
This is the resulting output from my log_ functions:
1 2 3 4 5 6 7
...
07/08/2009 19:47:27 [INF] Initializing camera; aspect ratio is 1.333333
07/08/2009 19:47:27 [INF] Initializing camera timer...
07/08/2009 19:47:27 [INF] Initializing timer system, using time.h...
07/08/2009 19:47:27 [INF] Timers OK! CLOCKS_PER_SEC = 1000; SECS_PER_CLOCK*1000 = 0.000000
07/08/2009 19:47:27 [INF] Start timer...
07/08/2009 19:47:27 [INF] Starting timer... Time is 46
As you can see, the Timer::StartTimer() method crashes with the start = clock() sentence.
I'm completely dumbfounded. Any help would be appreciated.
EDIT: I already tried changing clock_t to long int, same result. I'm using MinGW, by the way.
Aug 8, 2009 at 1:15am UTC
First thing that comes to mind is a bad 'this' pointer. If you are calling this function via a pointer to an object, make sure the pointer is valid:
1 2
Timer* foo; // a bad/uninitialized pointer
foo->StartTimer(); // this line doesn't crash -- but may cause a crash on the start = clock() line.
Aug 8, 2009 at 1:24am UTC
You're right! :D It works now. I feel so stupid now, I can't believe I missed something so simple. Thank you!
Aug 8, 2009 at 2:33am UTC
As a matter of good practice, since TIMER_TYPE is used only privately by your class, the typedef should be private inside the class rather than at file scope (or even public within the class).
Topic archived. No new replies allowed.