for following C/C++ code
void fun(int* x)
{
//SOMECODE;//WITHOUT SAVING PREVIOUS VALUE
if(SOME CONDITION)
printf("VALUE OF X IS DIFFERENT THAN PREVIOUS CALL \n");
}
int main()
{
int a;
a=9;
fun(&a);
a=12;
fun(&a);
return 0;
}
is there any function or flag value which give us information about whether variable get changed or not so if there is any solution please reply
Without providing some extra code to store the parameter 'during' the 'last' call to your function there is no chance at all to detect if it was called with the same value or another.
As the 'current' value of x will be living on the stack only it will be completely lost once your function call finishes.
In the pre-multi-threading era just using a static variable to cache the value of x during the last run of your function func
could (almost) have solved your problem:
void fun (int x) {
static int old_value = VALUE_INVALID;
if(x != old_value)
printf("VALUE OF X IS DIFFERENT THAN PREVIOUS CALL \n");
old_value = x;
}
The only problem still staying with this approach is re-entrancy - meaning the cached value might get screwed up if func
gets called in a signal handler.
If running in a multi-threaded application you additionally have to provide some kind of locking mechanism when using this approach or you'll get pretty much screwed up sooner or later.
Probably you'll be screwed up in this scenario anyways as you would have to clarify what 'last' call to function and value 'during' execution exactly mean.
Is the current right value that of thread A that entered func before thread B but is still executing or that of thread B that entered after B but which has finished execution of func already?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With