gcc中__builtin_return_address和__VA_ARGS__

— Built-in Function: void * __builtin_return_address ( unsigned int level)

This function returns the return address of the current function, or of one of its callers. The level argument is number of frames to scan up the call stack. A value of 0 yields the return address of the current function, a value of 1 yields the return address of the caller of the current function, and so forth. When inlining the expected behavior is that the function returns the address of the function that is returned to. To work around this behavior use the noinline function attribute.html

The level argument must be a constant integer.dom

On some machines it may be impossible to determine the return address of any function other than the current one; in such cases, or when the top of the stack has been reached, this function returns 0 or a random value. In addition, __builtin_frame_address may be used to determine if the top of the stack has been reached.ide

Additional post-processing of the returned value may be needed, see __builtin_extract_return_addr.函數

Calling this function with a nonzero argument can have unpredictable effects, including crashing the calling program. As a result, calls that are considered unsafe are diagnosed when the-Wframe-address option is in effect. Such calls should only be made in debugging situations. post

 
 
初步理解,此內建函數根據參數level返回當前函數的返回地址及當前函數調用者的返回地址,能夠一直追溯到整個函數調用棧狀況。
 
 
__VA_ARGS__
c99最新引入的可變參數宏,能夠解決以前宏定義中無法使用可變參數的問題。
 
#define my_print1(fmt, args...)    printf(fmt, ##args)
#define my_print2(fmt, ...)          printf(fmt, ##__VA_ARGS__)
 
這兩個宏定義功能一致,實現稍微有點區別。
相關文章
相關標籤/搜索