3

I tried the code below to trace the iterations of individual loops using a new variable ForloopCountM. The code is working good for a loop within a loop and for a loop after another. but Macro is failing in below two cases, I need a help how can I fix this issue and track the iteration count.

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;
}
  • 2
    Put that code into your question by hitting the edit button below your post. – rozina Jun 05 '15 at 06:04
  • Doesn't this get infinitely recursive? – cup Jun 05 '15 at 06:39
  • @cup: Macro replacement is non-recursive, i.e. after a macro has been replaced, the same macro replacement is no more applied to the code part resulting from the replacement. See http://stackoverflow.com/a/12447739/908515 – undur_gongor Jun 05 '15 at 11:21
  • 1
    Redefining keywords has [issues](http://stackoverflow.com/questions/12100789/redefinedefine-reserved-c-key-word). Pretty sure it is actually undefined behavior. – nwp Jun 09 '15 at 09:28

2 Answers2

7

You can add a __LINE__ predefined macro to declaration of your ForloopCountM

#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 gcc's page about predefined macroses

borisbn
  • 4,988
  • 25
  • 42
1

On a first overview it looks that your code do something wrong if your conditions is ForloopCountM==50 because the rest of the original code runs in the else path which is not executed then.

Next issue is that you have to define your variable before the loop to let the result existing after the loop.

The next issue is, that this is not really helpful code at all. If you think you could debug or monitor the iteration in a for loop you are on the wrong way. The compiler can unroll the code for you or simply calculate the results during compile time or what else optimization is the best in the given loop. Your macro may brake all possible optimizations! This means: Your code with your macro will give different results to the "real world code".

If you really want to know what the compiled original! code do, use code coverage/profiling tools. Look for gcov and gprof if you are running with gcc/g++. If you build your code with coverage/profiling switched on, you see how often every code block runs. This means also, every code block inside a loop which is exactly what you ask for. And the result is exactly the same as your original code, because coverage/profiling did not influence the optimization.

There are a wide range of tools to get a good print out and graphical statistic from collected results.

Klaus
  • 24,205
  • 7
  • 58
  • 113