C / C ++: Macro "for" keeps track of loops that have many iterations

I tried the code below to keep track of the iterations of individual loops using a new variable ForloopCountM

. The code works well for loop in loop and loop after loop. but Macro doesn't work in two cases, I need help how can I fix this problem and keep track of the number of iterations.

void    forloopTrace(int ForloopCount)
{
    if( ForloopCount==50)
    printf("Invalid \n");
}

#define CONCAT_IMPL( x, y ) x##y
#define MACRO_CONCAT( x, y ) CONCAT_IMPL( x, y )

#define FORlOOPCOUNTM MACRO_CONCAT(ForloopCountM,__LINE__)

#define for(args...)\
    int FORlOOPCOUNTM=0;\
for(args,FORlOOPCOUNTM++,forloopTrace(FORlOOPCOUNTM))\

int main()
{
    int i,j,x,y;
    j=100;
    y=200;
    for(i=0;i<j;i)   //works fine
    {
        i++;
        for(x=0;x<y;x) //works fine
            x++;
    }
    if(i>0)
       for(;i>0;)    //ERROR
         i--;
    for(;;)   //Error
    {
        i++;
    }

    return 0;
}

      

+3


source to share


2 answers


You can add a __LINE__

predefined macro to your declarationForloopCountM

#define CONCAT_IMPL( x, y ) x##y
#define MACRO_CONCAT( x, y ) CONCAT_IMPL( x, y )

#define FORlOOPCOUNTM MACRO_CONCAT(ForloopCountM,__LINE__)

#define for(args...)\
int FORlOOPCOUNTM=0;\
for(args,FORlOOPCOUNTM++)\
    if(FORlOOPCOUNTM==50)\
        printf("\n For loop is running more than 50 iterations !! \n");\
    else

int main()
{
    int i,j,x,y;
    j=100;
    y=200;
    for(i=0;i<j;i)   //works fine
    {
        i++;
        for(x=0;x<y;x) //works fine
            x++;
    }
    for(i=0;i<y;i)   //works fine too :)
    {
        i++;
    }
    return 0;
}

      



http://ideone.com/kvQ09r

Edit : Thanks to user3956566 I added a link to the gcc page about predefined macros

+7


source


In the first overview it looks like your code is doing something wrong if your conditions are true ForloopCountM==50

, because the rest of the source code runs in the else path, which is not executed then.

The next problem is that you must define your variable before the loop to give the result that exists after the loop.

The next problem is that this is not very useful code at all. If you think you can debug or control iteration in a for loop, you are wrong. The compiler can unwrap the code for you, or just calculate the results at compile time, or whatever other optimization is best in a given loop. Your macro can stop all possible optimizations! This means: your code with your macro will give different results to the "real world".



If you really want to know what the compiled original is! code, use code coverage / profiling tools. Find gcov

and gprof

if you are working with gcc / g ++. If you build your code with scoping / profiling enabled, you can see how often each block of code is executed. This also means that every block of code inside the loop is exactly what you ask for. And the result is exactly the same as your original code because coverage / profiling did not affect the optimization.

There are many tools available to get good printouts and graphical statistics from the collected results.

+1


source







All Articles