can I count on my compiler to optimize strlen on const char *?

前端 未结 4 777
后悔当初
后悔当初 2021-01-04 03:34

In my SAX xml parsing callback (XCode 4, LLVM), I am doing a lot of calls to this type of code:

static const char* kFoo = \"Bar\";

void SaxCallBack(char* sa         


        
4条回答
  •  春和景丽
    2021-01-04 04:12

    In general you can't count on it. However, you could use 'sizeof' and apply it to a string literal. Of course, this mean that you can't define 'kFoo' the way it originally was defined.

    The following should work on all compilers and on all optimization levels.

    #define kFoo "..."
    
        ... strcmp(... sizeof(kFoo))
    

提交回复
热议问题