c++中一些特殊用法
向量
typedef int v4si attribute ((vector_size (16)));
将v4si定义为一个4个int的数组,每个元素是4字节,这种方式是为了使用向量计算,以下是一个简单的示例:
#include <stdio.h>
typedef int v4si __attribute__ ((vector_size (16)));
int main(int argc, char* argv[])
{
v4si c;
v4si a = {1, 2, 3, 4};
v4si b = {1, 2, 3, 4};
c = a + b;
int *p = (int*)&c;
printf("%d,%d,%d,%d\n", *p, *(p+1), *(p+2), *(p+3));
return 0;
}
参考:https://gcc.gnu.org/onlinedocs/gcc/Vector-Extensions.html
进程的构造和析构函数
void attribute((constructor(101))) initl()
void attribute((destructor)) deinit()
进程在调用main函数之前会先调用constructor的函数,在进程退出时调用main函数后会调用destructor的函数
参考:
http://blog.chinaunix.net/uid-24512513-id-3195102.html
https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-constructor-function-attribute
清理函数
int avar attribute ((cleanup(clean_up))) = 1;
在定义某个局部变量时,会在后面使用一个__attribute__ ((cleanup(xxx)))的标记,在该变量被销毁的时候同时会调用此函数
参考:
https://echorand.me/site/notes/articles/c_cleanup/cleanup_attribute_c.html
https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html#index-cleanup-variable-attribute
always_inline
inline void test_func() attribute((always_inline));
将test_func()强制作为inline函数
参考:
https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-always_005finline-function-attribute
unused
attribute((unused))
参考:
https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-unused-function-attribute
fallthrough
attribute((fallthrough))
参考:
https://gcc.gnu.org/onlinedocs/gcc/Statement-Attributes.html#index-fallthrough-statement-attribute
aligned
attribute((aligned(CACHE_ALIGN_SIZE)))
指定该变量的最小对齐长度
参考:
https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html#index-aligned-variable-attribute
weak
void attribute((weak)) xxx()
声明xxx为弱符号,一般用在动态库中代码中,不会影响引用库的函数被覆盖
参考:
https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-weak-function-attribute
浙公网安备 33010602011771号