-1

I require a c/c++ MACRO to trace the "for" loop iterations in the existing source code and to print some message if loop is running more that 50 iterations. NOTE: the macro should not change the existing functionality of code. I tried some logic C/C++: "for" macro to track loops which are running many iterations but it is not working in all cases!

  int main()
  {
    int i,j,x,y;
    j=100;
    y=200;

    //case 1
    for(i=0;i<j;i++)   
        for(x=0;x<y;x) 
        x++;


    //case 2
    if(i>0)
      for(;i>0;)    
        i--;

     //case 3
     for(;;)  
     {
        i++;
     }

     return 0;
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
  • Please, there is nothing `c/c++`, don't promote it. – Sourav Ghosh Jun 12 '15 at 10:11
  • 1
    possible duplicate of [C/C++: "for" macro to track loops which are running many iterations](http://stackoverflow.com/questions/30659340/c-c-for-macro-to-track-loops-which-are-running-many-iterations) – m.s. Jun 12 '15 at 10:16
  • [link](http://stackoverflow.com/questions/30659340/c-c-for-macro-to-track-loops-which-are-running-many-iterations) it is failing in above mentioned cases – Rakshith Kumar N Jun 12 '15 at 10:19
  • 1
    @RakshithKumarN You said that it "failed", but didn't say how. – Carcigenicate Jun 12 '15 at 10:52
  • @Carcigenicate That implementation is assuming the for loop will have all (or at least the last) sections. If called with `for(;;)`, it will end up with `for(;; , something)`, which is a compile error. – mtijanic Jun 12 '15 at 11:26
  • @Carcigenicate In case 2 we are declaring FORlOOPCOUNTM in "if" block and trying to use it in "for" loop block so we will get variable not declared in the scope error! – Rakshith Kumar N Jun 12 '15 at 11:39

1 Answers1

1

Here is a proof of concept that passes all three cases:

// Max lines in a file you are profiling
#define MAX_LINES 65535
// Array to count all iterations, initialized to zero
uint32_t iterations[MAX_LINES];
// Increments the iteration for the given line, returns non-zero
#define ITER(ln)  ++iterations[ln]
// Override for definition
#define for(...) for(__VA_ARGS__) if (!ITER(__LINE__)) { } else

The limitation here is that you can only have one for per line, otherwise it will combine their iterations. So, this is okay:

for(i=0;i<j;i++)   
    for(x=0;x<y;x)
        x++;

but this isn't:

for(i=0;i<j;i++) for(x=0;x<y;x) x++;

The other limitation is that we define an array for every line. You can easily make it a hashmap or something, but that isn't relevant for this example. With a hashmap, you can use the __LINE__ and #x (string representation of the loop) to construct the ID, so the first limitation mostly goes away as well.

mtijanic
  • 2,872
  • 11
  • 26
  • on c++ i changed to "#define ITER(ln) iterations[ln]++" to make it work – Varun Garg Jun 12 '15 at 11:46
  • @UzumakiIchigo I don't do much C++, but I'm completely baffled that it doesn't implement the comma operator properly. Anyway, I've updated the code to make it work in both - you need `++iterations[ln]` and `if (!ITER(__LINE__))` instead, since it will return different values. – mtijanic Jun 12 '15 at 11:50