krishna
krishna

Reputation: 443

Can I know whether the variable has changed its value without comparing it with previous value in a function call C?

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

Upvotes: 5

Views: 26497

Answers (4)

mikyra
mikyra

Reputation: 10377

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?

Upvotes: 6

Sumit Kumar
Sumit Kumar

Reputation: 160

what is the purpose of checking the whether variable get changed or not ?

How about using structure containing variable and flag. when variable is changed set the flag and passed struct to function . Now test the flag in function to check whether variable is changed or not.

Upvotes: 0

Ben Voigt
Ben Voigt

Reputation: 283733

Reading your question again, you aren't checking whether a variable changes, but whether two different instances of the same lexical identifier are different.

That isn't possible except by explicit comparison.


Original answer concerning detection of changes to a variable:

There is no way in portable, standard C or C++.

However, there are environment-specific ways to detect when a variable is changed. You can use the MMU access control flags (via mprotect or VirtualProtect) to generate an exception on the first write, and set a dirty flag from inside the handler. (Almost every modern OS does this with memory-mapped files, to find out whether it needs to be written back to disk). Or you can use a hardware breakpoint to match a write to that address (debuggers use this to implement breakpoints on variables).

None of these will be as efficient as comparing to the old value. (But comparing to the old value will miss ABA scenarios)

Upvotes: 2

flyingOwl
flyingOwl

Reputation: 244

There is no possibility to check if the current value differs from the value send with the last call.

At runtime, every call to fun() creates an independent stack frame with its own copy of x. fun() cannot save any value inside this stack frame (used to store local variables) for a later call.

There is also no way to determine if a "variable has changed its value without comparing it with previous value".

Upvotes: 1

Related Questions