Static function variable problem
-
Hello, I have a function that is called from a dll and contains static variable:
void myFn(double data)
{
static double x;
...
}At each iteration x increases on some value. The problem occurs when pass diffent data at the same time (i.e. call the function more than once at the same time), x variable mixes all values.
data1[] = 1,2,3
data2[] = 4,5,6The result for the first call must be 1+2+3 = 6 The result for the second call must be 4+5+6 = 15 But now the result for the first call will be 1+4+2+5+... and so on. How to avoid this problem?
-
Hello, I have a function that is called from a dll and contains static variable:
void myFn(double data)
{
static double x;
...
}At each iteration x increases on some value. The problem occurs when pass diffent data at the same time (i.e. call the function more than once at the same time), x variable mixes all values.
data1[] = 1,2,3
data2[] = 4,5,6The result for the first call must be 1+2+3 = 6 The result for the second call must be 4+5+6 = 15 But now the result for the first call will be 1+4+2+5+... and so on. How to avoid this problem?
At first glance, your problem sounds like one of synchronization. But then I could not figure out how you were calling
myFn()
. It is expecting a single argument, yet you've shown two arrays. Are you passing one member of the array with each call tomyFn()
?alikalik wrote:
The result for the first call must be 1+2+3 = 6 The result for the second call must be 4+5+6 = 15 But now the result for the first call will be 1+4+2+5+... and so on.
Nothing you've shown supports this. :confused:
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
-
Hello, I have a function that is called from a dll and contains static variable:
void myFn(double data)
{
static double x;
...
}At each iteration x increases on some value. The problem occurs when pass diffent data at the same time (i.e. call the function more than once at the same time), x variable mixes all values.
data1[] = 1,2,3
data2[] = 4,5,6The result for the first call must be 1+2+3 = 6 The result for the second call must be 4+5+6 = 15 But now the result for the first call will be 1+4+2+5+... and so on. How to avoid this problem?
alikalik wrote:
call the function more than once at the same time
are you calling the DLL from multiple threads ?
-
Hello, I have a function that is called from a dll and contains static variable:
void myFn(double data)
{
static double x;
...
}At each iteration x increases on some value. The problem occurs when pass diffent data at the same time (i.e. call the function more than once at the same time), x variable mixes all values.
data1[] = 1,2,3
data2[] = 4,5,6The result for the first call must be 1+2+3 = 6 The result for the second call must be 4+5+6 = 15 But now the result for the first call will be 1+4+2+5+... and so on. How to avoid this problem?
-
Hello, I have a function that is called from a dll and contains static variable:
void myFn(double data)
{
static double x;
...
}At each iteration x increases on some value. The problem occurs when pass diffent data at the same time (i.e. call the function more than once at the same time), x variable mixes all values.
data1[] = 1,2,3
data2[] = 4,5,6The result for the first call must be 1+2+3 = 6 The result for the second call must be 4+5+6 = 15 But now the result for the first call will be 1+4+2+5+... and so on. How to avoid this problem?
Remove the static local variable making it an argument passed by reference. :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
alikalik wrote:
call the function more than once at the same time
are you calling the DLL from multiple threads ?