c++act

Can a function return the same value inside a loop, and return different values outside of loops?


It acts like this.

fun();//return 1;
for (int i=0;i++;i<100)
    fun();//return 2;
fun();//return 3;

I don't want to do it manually, like:

static int i=0;
fun(){return i};
main()
{
    i++;
    fun();//return 1;
    i++;
    for (int i=0;i++;i<100)
        fun();//return 2;
    i++;
    fun();//return 3;
}

New classes and static variables are allowed.

I am trying to design a cache replacement algorithm. Most of the time I use the LRU algorithm, but, if I use LRU algorithm inside a loop I would very likely get a cache thrashing.

https://en.wikipedia.org/wiki/Thrashing_(computer_science)

I need to know if I am inside a loop. Then I can use the LFU algorithm to avoid thrashing.


Solution

  • An obvious way of doing this would be using the __LINE__ macro. It will return the source code line number, which will be different throughout your function.