bool kDebuggingEnabled = NO;
...
for(i=0; i<length; i++){
...
if (kDebuggingEnabled) {
NSLog (@"Value of variable # %i",$resultingOptions);
}
}
Run Code Online (Sandbox Code Playgroud)
每当我的应用程序处于活动状态时,我的代码每次都会检查NSLog的状态.有没有更好的方法来提高我的代码的性能?
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)
任何建议都会有所帮助,
在阅读我的小组项目的代码时,我遇到了许多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)语句中吗?什么用途?
我已经定义了一个自定义断言宏.这适用于所有其他比较.但是,我得到编译器错误:
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 = ⌖
int* ptr2 = ⌖
//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,但我很好奇为什么会生成此编译器错误.
编译时出现此错误:
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)