【发布时间】:2017-03-22 01:56:42
【问题描述】:
我已经考虑过了。 这是代码:
#define IP_VAL(id) ({ \
char ip[32] = {0}; \
sprintf(ip,"192.168.0.%d",id&0xff); \
(char *)ip; \
})
确实有效,不知道是不是宏会出问题?
我是这样使用的:
function(IP_VAL(id));
但我不确定这绝对没问题。 ip[32] 是否超出操作范围?
非常感谢您编辑我的问题:)
我已经测试了这样的简单示例:
main(){
int a = 1;
int b = 2;
a = b;
a = ({
int c = 3;
c;
});
a = ({
int d = 4;
d;
});
a = ({
int c = 5;
c;
});
({
int c = 6;
c;
});
({
int c = 7;
c;
});
({
int c = 8;
c;
});
}
然后 objdump 它:
080483ed <main>:
80483ed: 55 push %ebp
80483ee: 89 e5 mov %esp,%ebp
80483f0: 83 ec 20 sub $0x20,%esp
80483f3: c7 45 e0 01 00 00 00 movl $0x1,-0x20(%ebp)
80483fa: c7 45 e4 02 00 00 00 movl $0x2,-0x1c(%ebp)
8048401: 8b 45 e4 mov -0x1c(%ebp),%eax
8048404: 89 45 e0 mov %eax,-0x20(%ebp)
8048407: c7 45 e8 03 00 00 00 movl $0x3,-0x18(%ebp)
804840e: 8b 45 e8 mov -0x18(%ebp),%eax
8048411: 89 45 e0 mov %eax,-0x20(%ebp)
8048414: c7 45 ec 04 00 00 00 movl $0x4,-0x14(%ebp)
804841b: 8b 45 ec mov -0x14(%ebp),%eax
804841e: 89 45 e0 mov %eax,-0x20(%ebp)
8048421: c7 45 f0 05 00 00 00 movl $0x5,-0x10(%ebp)
8048428: 8b 45 f0 mov -0x10(%ebp),%eax
804842b: 89 45 e0 mov %eax,-0x20(%ebp)
804842e: c7 45 f4 06 00 00 00 movl $0x6,-0xc(%ebp)
8048435: c7 45 f8 07 00 00 00 movl $0x7,-0x8(%ebp)
804843c: c7 45 fc 08 00 00 00 movl $0x8,-0x4(%ebp)
8048443: c9 leave
8048444: c3 ret
所以我认为'c'和'd'就像'a'和'b'一样,它们是'main()'堆栈中的变量。不同之处在于它们的范围。在示例中,每个“c”都有不同的地址。
我认为宏 'IP_VAL' 有效,但我不知道 gcc 编译选项是否会影响它?
【问题讨论】:
-
C 还是 C++?择其一,而不是两者兼而有之。
-
肯定不行。使用函数。
-
宏不是函数!
-
我删除了 C++ 标签,因为您要求使用 C 并且您的代码没有显示任何 C++。 C 和 C++ 是不同的语言
-
还要注意 compound statements 是一个 GCC 扩展。这不是标准的 C 功能。
标签: c arrays gcc macros c-preprocessor