使用GCC"人工"函数属性的用例

sun*_*ide 8 c attributes gcc

我刚刚阅读了GCC函数属性,artificial但没有完全得到描述.你能给我一些有用的例子吗?

Per*_*son 12

另一个答案没有错,但也许我可以更好地解释一下.

想象一下这个函数foo.c,用行号:

10: static inline int foo(struct q *x)
11: {
12:     return bar(x + 1);
13: }
Run Code Online (Sandbox Code Playgroud)

这是从另一个函数调用两次:

20: void baz(void)
21: {
22:     x = foo(qa);
23:     x = foo(qb);
24: }
Run Code Online (Sandbox Code Playgroud)

不幸的是,bar()崩溃了.这是回溯:
#0 0x00000000004b1a2a in bar (x=0x8) at foo.c:5 #1 0x0000000000416ee0 in baz () at foo.c:12 #2 0x0000000000413fab in main () at foo.c:30

既然foo是内联的,那么它不是回溯的一部分,而是等待,foo.c:12进入foo,低于它只是在线main.没有什么可以告诉我们baz导致崩溃的线路.

如果我们将foo标记为人为的,我们就会得到这个回溯:
#0 0x00000000004b1a2a in bar (x=0x8) at foo.c:5 #1 0x0000000000416ee0 in baz () at foo.c:22 #2 0x0000000000413fab in main () at foo.c:30

它不再指向foo.相反,它向我们展示了foo从哪里调用foo.c:22.Suddendly很容易说出这qa是一个有问题的变量.