相关疑难解决方法(0)

条件NSLog,不降低性能

bool kDebuggingEnabled = NO;
...
for(i=0; i<length; i++){
 ...
 if (kDebuggingEnabled) {
  NSLog (@"Value of variable # %i",$resultingOptions);
 }
}
Run Code Online (Sandbox Code Playgroud)

每当我的应用程序处于活动状态时,我的代码每次都会检查NSLog的状态.有没有更好的方法来提高我的代码的性能?

debugging cocoa objective-c nslog

2
推荐指数
1
解决办法
1187
查看次数

创建类似函数的宏

gcc 4.4.2 c89

我有这个代码片段,我必须在很多代码中重复.我只是想知道有没有办法通过使用宏功能来缩短它?

有我想改变的代码.

ERR_INFO error_info; /* create error object */
ErrorInfo(&error_info); /* pass the address for it to be filled with error info */
fprintf(stderr, "And the error is? [ %s ]\n", error_info.msg); /* display the error msg */
Run Code Online (Sandbox Code Playgroud)

我试图创建一个宏功能来使用它.

#define DISPLAY_ERR(error_info) ErrorInfo(&error_info) error_info.msg
fprintf(stderr, "And the error is? [ %s ]\n", DISPLAY_ERR); /* display the error
Run Code Online (Sandbox Code Playgroud)

任何建议都会有所帮助,

c

1
推荐指数
1
解决办法
614
查看次数

想知道一些#define技巧

在阅读我的小组项目的代码时,我遇到了许多DEFINE,其中一些似乎很奇怪.要概括它,请查看以下2个示例.

例1:

#define SNPRINTF(dst, fmt, arg...)  snprintf(dst, sizeof(dst), fmt, ##arg)
Run Code Online (Sandbox Code Playgroud)

"##"在这种情况下意味着什么?我试图删除他们两个,并写这样的代码 "字符BUF [1024]的snprintf(buf中," %S%S " "ABCD", "EFG");" 这产生了相同的结果.所以"##"似乎毫无用处,对我没有任何伤害.

例2:

#define CLOSE(fd)   do  {   \
        if (-1 != (fd)) {   \
                    close(fd);  \
                    (fd)    = -1;   \
                }   \
} while (0)
Run Code Online (Sandbox Code Playgroud)

有必要将内部代码填充到do{}while(0)语句中吗?什么用途?

c c-preprocessor

1
推荐指数
1
解决办法
949
查看次数

C++:使用宏比较指针时的编译器错误

我已经定义了一个自定义断言宏.这适用于所有其他比较.但是,我得到编译器错误:

ISO C++ forbids comparison between pointer and integer
Run Code Online (Sandbox Code Playgroud)

当使用下面显示的宏(DWASSERT)来比较指针时,如下面的代码所示.

#define DWASSERT(condition,printstatement)  if(!condition){ printf(printstatement); assert(condition); }

#include <stdio.h>

int main()
{
    int target = 0;
    int* ptr1 = &target;
    int* ptr2 = &target;

    //Normal comparison works fine
    if(ptr1 == ptr2)
        printf("Equal");

    //Comparison using Macro generates compiler
    //error on the next line
    DWASSERT(ptr1 == ptr2, "Pointers not equal!\n");
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

虽然我可以简单地避免在这种情况下使用DWASSERT,但我很好奇为什么会生成此编译器错误.

c++ macros assert

1
推荐指数
1
解决办法
433
查看次数

宏 - 表达式不可分配

编译时出现此错误:

macro.c:11:2: error: expression is not assignable
    ProdottoAumentato(10, 20);
Run Code Online (Sandbox Code Playgroud)

我不知道为什么会这样说,我找不到任何错误.这是代码:

#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>

#define ProdottoAumentato(X, Y)    X++; X * Y;

int main(void) {

    ProdottoAumentato(10, 20);

    printf("\nEnd\n");
    return(0);
}
Run Code Online (Sandbox Code Playgroud)

c macros c-preprocessor

-2
推荐指数
2
解决办法
575
查看次数

标签 统计

c ×3

c-preprocessor ×2

macros ×2

assert ×1

c++ ×1

cocoa ×1

debugging ×1

nslog ×1

objective-c ×1