Hello,
I am trying to use an alternative Sleep() function in C that has a better solution. (The solution of sleep() is only 15 ms). I need 1 ms. My teacher gave me the above code
static double esperaH(double *temp) //sleep in ms
{
bool resultado;
unsigned _int64 *now, *frec,goal;
//LARGE_INTEGER *now, *frec, goal;
now = (unsigned _int64*)calloc(1,sizeof(now));
//now = (LARGE_INTEGER*)calloc(1,sizeof(now));
resultado = QueryPerformanceCounter(now);
frec = (unsigned _int64*)calloc(1,sizeof(frec));
//frec = (LARGE_INTEGER*)calloc(1,sizeof(frec));
resultado=QueryPerformanceFrequency(frec);
goal = *now + (unsigned _int64)((*temp*1.0e-3)*(*frec));
while (goal > *now)
resultado = QueryPerformanceCounter(now);
return ((double)*now/(double)*frec)*1000;
//return ((LARGE_INTEGER)*now/(LARGE_INTEGER)*frec)*1000;
}
if I compile it like this I get the following warnings/errors:
--------------------Configuration: pci 1784 configuration5 - Win32 Debug--------------------
Compiling...
pci 1784 configuration5.cpp
C:\datos\judith\proyecto_judith\pci 1784\pci 1784 configuration5.cpp(110) : warning C4305: 'argument' : truncation from 'const double' to 'float'
C:\datos\judith\proyecto_judith\pci 1784\pci 1784 configuration5.cpp(55) : warning C4101: 'num_contador' : unreferenced local variable
C:\datos\judith\proyecto_judith\pci 1784\pci 1784 configuration5.cpp(387) : warning C4244: 'argument' : conversion from 'unsigned long' to 'unsigned short', possible loss of data
C:\datos\judith\proyecto_judith\pci 1784\pci 1784 configuration5.cpp(426) : warning C4244: 'argument' : conversion from 'const double' to 'unsigned char', possible loss of data
C:\datos\judith\proyecto_judith\pci 1784\pci 1784 configuration5.cpp(728) : error C2664: 'QueryPerformanceCounter' : cannot convert parameter 1 from 'unsigned __int64 *' to 'union _LARGE_INTEGER *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
C:\datos\judith\proyecto_judith\pci 1784\pci 1784 configuration5.cpp(732) : error C2664: 'QueryPerformanceFrequency' : cannot convert parameter 1 from 'unsigned __int64 *' to 'union _LARGE_INTEGER *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
C:\datos\judith\proyecto_judith\pci 1784\pci 1784 configuration5.cpp(737) : error C2664: 'QueryPerformanceCounter' : cannot convert parameter 1 from 'unsigned __int64 *' to 'union _LARGE_INTEGER *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
Error executing cl.exe.
pci 1784 configuration5.obj - 3 error(s), 4 warning(s)
If I compile it using _int
If I use the other code version:
static double esperaH(double *temp) //sleep in ms
{
bool resultado;
//unsigned _int64 *now, *frec,goal;
LARGE_INTEGER *now, *frec, goal;
//now = (unsigned _int64*)calloc(1,sizeof(now));
now = (LARGE_INTEGER*)calloc(1,sizeof(now));
resultado = QueryPerformanceCounter(now);
//frec = (unsigned _int64*)calloc(1,sizeof(frec));
frec = (LARGE_INTEGER*)calloc(1,sizeof(frec));
resultado=QueryPerformanceFrequency(frec);
goal = *now + (LARGE_INTEGER)((*temp*1.0e-3)*(*frec));
while (goal > *now)
resultado = QueryPerformanceCounter(now);
return ((double)*now/(double)*frec)*1000;
//return ((LARGE_INTEGER)*now/(LARGE_INTEGER)*frec)*1000;
}
...I get the following compiler warnings/errors:
--------------------Configuration: pci 1784 configuration5 - Win32 Debug--------------------
Compiling...
pci 1784 configuration5.cpp
C:\datos\judith\proyecto_judith\pci 1784\pci 1784 configuration5.cpp(110) : warning C4305: 'argument' : truncation from 'const double' to 'float'
C:\datos\judith\proyecto_judith\pci 1784\pci 1784 configuration5.cpp(55) : warning C4101: 'num_contador' : unreferenced local variable
C:\datos\judith\proyecto_judith\pci 1784\pci 1784 configuration5.cpp(387) : warning C4244: 'argument' : conversion from 'unsigned long' to 'unsigned short', possible loss of data
C:\datos\judith\proyecto_judith\pci 1784\pci 1784 configuration5.cpp(426) : warning C4244: 'argument' : conversion from 'const double' to 'unsigned char', possible loss of data
C:\datos\judith\proyecto_judith\pci 1784\pci 1784 configuration5.cpp(728) : warning C4800: 'int' : forcing value to bool 'true' or 'false' (performance warning)
C:\datos\judith\proyecto_judith\pci 1784\pci 1784 configuration5.cpp(732) : warning C4800: 'int' : forcing value to bool 'true' or 'false' (performance warning)
C:\datos\judith\proyecto_judith\pci 1784\pci 1784 configuration5.cpp(734) : error C2677: binary '*' : no global operator defined which takes type 'union _LARGE_INTEGER' (or there is no acceptable conversion)
C:\datos\judith\proyecto_judith\pci 1784\pci 1784 configuration5.cpp(736) : error C2678: binary '>' : no operator defined which takes a left-hand operand of type 'union _LARGE_INTEGER' (or there is no acceptable conversion)
C:\datos\judith\proyecto_judith\pci 1784\pci 1784 configuration5.cpp(736) : fatal error C1903: unable to recover from previous error(s); stopping compilation
Error executing cl.exe.
pci 1784 configuration5.obj - 3 error(s), 6 warning(s)
I don´t know what to do. The Query-fungtions seem to only accept large_integer, but large_integer cannot operate with +,*,/
Does anyone have a suggestion what could be my error or how I could solve my problem?? I would be so glad since I am very unexperienced in programming.
Judith
by: KdoPosted on 2007-02-22 at 06:14:16ID: 18587543
Hi Judith,
It's a bit of a challenge to take these error messages (which cover the entire program or module) and relate them to this small piece of code. Still, I think that I can tie them together. :)
One of the things that you can do to simplify the program is to use scalar variables instead of pointers within the function. Here's a quick edit to demonstrate:
static double esperaH(double *temp) //sleep in ms
{
bool resultado;
//unsigned _int64 *now, *frec,goal;
LARGE_INTEGER now, frec, goal;
resultado = QueryPerformanceCounter(&n
resultado=QueryPerformance
goal = now + (LARGE_INTEGER)((*temp*1.0
while (goal > now)
resultado = QueryPerformanceCounter(&n
return ((double)now/(double)frec)
//return ((LARGE_INTEGER)now/(LARGE
}
I'm curious as to why you pass a pointer to a double instead of just passing a double.
Kent