Linux下__attribute__((aligned(n)))的使用

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

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

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

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

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

关于C++内存对齐介绍可以参考: https://blog.csdn.net/fengbingchun/article/details/81270326

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

#include <iostream>

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: %d\n",
		sizeof(struct S1), sizeof(struct S2), sizeof(struct S5)); // 6, 64, 128

	typedef int more_aligned_int __attribute__((aligned(16)));
	fprintf(stdout, "aligned: %d, %d\n", 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: %d\n", sizeof(struct S3), sizeof(struct S4)); // 16, 4

	int arr[2] __attribute__((aligned(16))) = {1, 2};
	fprintf(stdout, "arr size: %d, arr aligned: %d\n", sizeof(arr), alignof(arr)); // 8, 16

	struct S6 {more_aligned_int f;} __attribute__((packed));
	fprintf(stdout, "S6 size: %d\n", sizeof(struct S6)); // 4

	char c __attribute__((aligned(16))) = 'a';
	fprintf(stdout, "c size: %d, aligned: %d\n", sizeof(c), alignof(c)); // 1, 16

	struct S7 {double f;} __attribute__((aligned(4)));
	fprintf(stdout, "S7 size: %d, algined: %d\n", sizeof(struct S7), alignof(struct S7)); // 8, 8

	struct S8 {double f;} __attribute__((__aligned__(32)));
	fprintf(stdout, "S8 size: %d, algined: %d\n", 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即可。

GitHubhttps://github.com/fengbingchun/Linux_Code_Test 

猜你喜欢

转载自blog.csdn.net/fengbingchun/article/details/81321419