attribute java c_Linux下__attribute__((aligned(n)))的使用

关键字__attribute__允许你在定义struct、union、变量等类型时指定特殊属性。此关键字后面是跟着双括号括起来的属性说明。__attribute__不属于标准C语言,它是GCC对C语言的一个扩展用法。

你可以在其关键字之前和之后使用"__"指定这些属性中的一个,这样允许你在头文件中使用这些属性,而不必担心可能的同名宏。例如你可以使用__aligned__代替aligned。

__attribute__((aligned(n))):此属性指定了指定类型的变量的最小对齐(以字节为单位)。如果结构中有成员的长度大于n,则按照最大成员的长度来对齐。

注意:对齐属性的有效性会受到链接器(linker)固有限制的限制,即如果你的链接器仅仅支持8字节对齐,即使你指定16字节对齐,那么它也仅仅提供8字节对齐。

__attribute__((packed)):此属性取消在编译过程中的优化对齐。

以下是测试代码(sample_attribute_aligned.cpp):

#include int main() { struct S1 {short f[3];}; struct S2 {short f[3];} __attribute__((aligned(64))); struct S5 {short f[40];} __attribute__((aligned(64))); fprintf(stdout, "S1 size: %d, S2 size: %d, S5 size: %dn", sizeof(struct S1), sizeof(struct S2), sizeof(struct S5)); // 6, 64, 128 typedef int more_aligned_int __attribute__((aligned(16))); fprintf(stdout, "aligned: %d, %dn", alignof(int), alignof(more_aligned_int)); // 4, 16 struct S3 {more_aligned_int f;}; struct S4 {int f;}; fprintf(stdout, "S3 size: %d, S4 size: %dn", sizeof(struct S3), sizeof(struct S4)); // 16, 4 int arr[2] __attribute__((aligned(16))) = {1, 2}; fprintf(stdout, "arr size: %d, arr aligned: %dn", sizeof(arr), alignof(arr)); // 8, 16 struct S6 {more_aligned_int f;} __attribute__((packed)); fprintf(stdout, "S6 size: %dn", sizeof(struct S6)); // 4 char c __attribute__((aligned(16))) = 'a'; fprintf(stdout, "c size: %d, aligned: %dn", sizeof(c), alignof(c)); // 1, 16 struct S7 {double f;} __attribute__((aligned(4))); fprintf(stdout, "S7 size: %d, algined: %dn", sizeof(struct S7), alignof(struct S7)); // 8, 8 struct S8 {double f;} __attribute__((__aligned__(32))); fprintf(stdout, "S8 size: %d, algined: %dn", sizeof(struct S8), alignof(struct S8)); // 32, 32 return 0; }

CMakeLists.txt文件内容如下:

PROJECT(samples_cplusplus) CMAKE_MINIMUM_REQUIRED(VERSION 3.0) # 支持C++11 SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g -Wall -O2 -std=c11") SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -Wall -O2 -std=c++11") INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR}) FILE(GLOB samples ${PROJECT_SOURCE_DIR}/*.cpp) FOREACH (sample ${samples}) STRING(REGEX MATCH "[^/]+$" sample_file ${sample}) STRING(REPLACE ".cpp" "" sample_basename ${sample_file}) ADD_EXECUTABLE(test_${sample_basename} ${sample}) TARGET_LINK_LIBRARIES(test_${sample_basename} pthread) ENDFOREACH()

build.sh脚本内容如下:

#! /bin/bash real_path=$(realpath $0) dir_name=`dirname "${real_path}"` echo "real_path: ${real_path}, dir_name: ${dir_name}" new_dir_name=${dir_name}/build mkdir -p ${new_dir_name} cd ${new_dir_name} cmake .. make cd -

编译及测试方法如下:首先执行build.sh,然后再执行./build/test_sample_attribute_aligned即可。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值