宏来跟踪其运行多次迭代循环

宏来跟踪其运行多次迭代循环

本文介绍了C / C ++:"为"宏来跟踪其运行多次迭代循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试过低于code跟踪使用一个新的变量 ForloopCountM 个人循环的迭代。在code工作很好的一个循环中,并为一个又一个循环回路。但宏如下2案件失败时,我需要帮助我如何可以解决此问题,并跟踪迭代次数。

 无效forloopTrace(INT ForloopCount)
{
    如果(ForloopCount == 50)
    的printf(无效\\ n);
}#定义CONCAT_IMPL(X,Y)×##ÿ
的#define MACRO_CONCAT(X,Y)CONCAT_IMPL(X,Y)#定义FORlOOPCOUNTM MACRO_CONCAT(ForloopCountM,__ LINE__)#定义为(参数...)\\
    INT FORlOOPCOUNTM = 0; \\
对于(参数,FORlOOPCOUNTM ++,forloopTrace(FORlOOPCOUNTM))\\诠释的main()
{
    INT I,J,X,Y;
    J = 100;
    Y = 200;
    对于(i = 0; I<焦耳; I)//工作正常
    {
        我++;
        为(X = 0; X&所述; Y; ​​X)//正常工作
            X ++;
    }
    如果(I 0)
       对于(; I> 0;)//错误
         一世 - ;
    为(;;)//错误
    {
        我++;
    }    返回0;
}


解决方案

您可以添加 __ __ LINE 的来声明你的 ForloopCountM

 的#define CONCAT_IMPL(X,Y)×##ÿ
的#define MACRO_CONCAT(X,Y)CONCAT_IMPL(X,Y)#定义FORlOOPCOUNTM MACRO_CONCAT(ForloopCountM,__ LINE__)#定义为(参数...)\\
INT FORlOOPCOUNTM = 0; \\
对于(参数,FORlOOPCOUNTM ++)\\
    如果(FORlOOPCOUNTM == 50)\\
        的printf(\\ n对于循环运行超过50次迭代!\\ n); \\
    其他诠释的main()
{
    INT I,J,X,Y;
    J = 100;
    Y = 200;
    对于(i = 0; I<焦耳; I)//工作正常
    {
        我++;
        为(X = 0; X&所述; Y; ​​X)//正常工作
            X ++;
    }
    对于(i = 0; I< Y; ​​I)//太正常工作:)
    {
        我++;
    }
    返回0;
}

修改:感谢user3956566我加了一个约predefined macroses链接到GCC的页

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;
}
解决方案

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

这篇关于C / C ++:&QUOT;为&QUOT;宏来跟踪其运行多次迭代循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 01:15