I'm using std::clock() in two instances of the same program in Windows Vista. One instance is giving normal behavior, and the other is giving crap data. Could this be my system's implementation, or is something suspect in this code:
Code:
//Runs in two programs -- the one I opened first behaves, the second does not
stopwatch timer;
timer.start();
some_long_function();
timer.stop();
std::cout << timer.read() << std::endl;
Code:
class stopwatch
{
	public:
		void start();
		void stop();
		const bool running() const { return rng; }
		const unsigned long long int read();
		static const bool auto_start = true;
		stopwatch(bool auto_start_ = false) : curr(0), data(0)
		{
			initialize_data();
			if(auto_start_ == auto_start) start();
		}
		~stopwatch();
	private:
		void initialize_data();
		void calc_diff();
		void* data;
		unsigned long long int curr;
		bool rng;
};
Code:
#include "stopwatch.hpp"
#include <ctime>
#include <cassert>

void stopwatch::start()
{
	if(rng || (data == 0))
		return;
	((std::clock_t*)data)[0] = std::clock();
	rng = true;
}

void stopwatch::stop()
{
	if(!rng || (data == 0))
		return;
	((std::clock_t*)data)[1] = std::clock();
	rng = false;
	calc_diff();
}

void stopwatch::calc_diff()
{
	if(rng || (data == 0))
		return;
	std::clock_t & beg = ((std::clock_t*)data)[0];
	std::clock_t & end = ((std::clock_t*)data)[1];
	curr = end - beg;
}

const unsigned long long int stopwatch::read() //if rng, read() will give a current reading, if !rng, it'll return curr.
{
	if(!rng) return curr;
	std::clock_t now = std::clock();
	std::clock_t & beg = ((std::clock_t*)data)[0];
	return now - beg;
}

void stopwatch::initialize_data()
{
	assert(data == 0);
	data = new std::clock_t[2];
}

stopwatch::~stopwatch()
{
	if(data)
		delete [] (std::clock_t*)(data);
	data = 0;
}