LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Is there a way of initializing a function? When I make a second call to this part

icular function, it seems as though it still contains data from last call? How can I rectify this? Do you see any problems with this function? It seems to work fine the first set of function calls but, when I try a second round it doesn't work properly.void Interpolate( short voltage, int time )
{
static short lastVoltage;
static int lastTime;
static int totalTime;
static int arrayPtr;
double t;
double deltaVoltage, deltaTime;

totalTime = lastTime + time;

deltaVoltage = voltage - lastVoltage;
deltaTime = (double)(totalTime - lastTime);

if( time == 0 ) {
lastTime = 0;
arrayPtr = 0;
totalTime = 0;
lastVoltage = voltage;
potenti
alArray[arrayPtr++] = voltage;
}

else {

for ( t = 1; t <= deltaTime; t++ ) {
potentialArray[arrayPtr++] = t * (deltaVoltage/deltaTime) + lastVoltage;
}

lastTime += time;
lastVoltage = voltage;
}
}
0 Kudos
Message 1 of 2
(2,814 Views)
icular function, it seems as though it still contains data from last call? How can I rectify this? Do you see any problems with this function? It seems to work fine the first set of function calls but, when I try a second round it doesn't work properly.First, it looks like you intentionally have the function set up to retain the data for some variables (lastVoltage, lastTime, arrayPtr, totalTime). The static flag means that it will retain these values between calls to this function. The other variables will not retain data (except for the potentialArray buffer which must be defined globally). What exactly is the complaint?

Best Regards,

Chris Matthews
Measurement Studio Support Manager
Message 2 of 2
(2,814 Views)