《Windows核心编程》---C运行库的内存对齐函数
来源:互联网 发布:逆火采集软件 编辑:程序博客网 时间:2024/05/22 23:51
C运行库提供了一系列函数用于分配对齐过的内存:
1)_aligned_malloc函数的功能是分配一块对齐过的内存:
void * _aligned_malloc(
size_t size, //要分配的字节数
size_t alignment //要对齐到的字节边界,传给alignment的值必须是2的整数幂次方
);
2)_aligned_offset_malloc函数用于在指定的内存对齐边界上分配内存:
void * _aligned_offset_malloc(
size_t size, //要分配的字节数
size_t alignment, //要对齐到的字节边界,传给alignment的值必须是2的整数幂次方
size_t offset //为达到内存对齐所指定的偏移量
);
3)_aligned_realloc函数和_aligned_offset_realloc函数用于改变由_aligned_malloc或者_aligned_offset_malloc分配的内存的大小:
void * _aligned_realloc(
void *memblock, //要改变大小的内存地址
size_t size, //新分配的字节数
size_t alignment //要对齐到的字节边界,传给alignment的值必须是2的整数幂次方
);
void * _aligned_offset_realloc(
void *memblock,
size_t size,
size_t alignment,
size_t offset
);
4)_aligned_free函数用于释放由上述函数申请的内存资源:
void _aligned_free (
void *memblock
);
实例代码如下:
#include <iostream>
#include <malloc.h>
#include <stdio.h>
using namespace std;
int main()
{
void *ptr;
size_t alignment;
size_t offset;
//注意alignment必须是2的整数幂次方
alignment = 16;
offset = 5;
//使用_aligned_malloc
ptr = _aligned_malloc(100, alignment);
if(ptr == NULL)
{
printf_s("Error allocation aligned memory/n");
return -1;
}
if(((int)ptr % alignment) == 0)
{
printf_s("This pointer, %d, is aligned on %d/n",
ptr, alignment);
}
else
{
printf_s("This pointer, %d, is not aligned on %d/n",
ptr, alignment);
}
//使用_aligned_realloc
ptr = _aligned_realloc(ptr, 200, alignment);
if(((int)ptr % alignment) == 0)
{
printf_s("This pointer, %d, is aligned on %d/n",
ptr, alignment);
}
else
{
printf_s("This pointer, %d, is not aligned on %d/n",
ptr, alignment);
}
//释放
_aligned_free(ptr);
//使用_aligned_offset_malloc
ptr = _aligned_offset_malloc(200, alignment, offset);
if(ptr == NULL)
{
printf_s("Error allocation aligned offset memory/n");
return -1;
}
if((((int)ptr + offset) % alignment) == 0)
{
printf_s("This pointer, %d, is offset by %d on alignment of %d/n",
ptr, offset, alignment);
}
else
{
printf_s("This pointer, %d, does not satisfy offset %d and alignment %d/n",
ptr, offset, alignment);
}
//使用_aligned_offset_realloc
ptr = _aligned_offset_realloc(ptr, 200, alignment, offset);
if(ptr == NULL)
{
printf_s("Error reallocation aligned offset memory/n");
return -1;
}
if((((int)ptr + offset) % alignment) == 0)
{
printf_s("This pointer, %d, is offset by %d on alignment of %d/n",
ptr, offset, alignment);
}
else
{
printf_s("This pointer, %d, does not satisfy offset %d and alignment %d/n",
ptr, offset, alignment);
}
//_aligned_free同时与_aligned_malloc和_aligned_realloc配合使用
_aligned_free(ptr);
system("pause");
return 0;
}
- 《Windows核心编程》---C运行库的内存对齐函数
- windows核心编程笔记第10篇 (线程创建的过程和基于C/C++运行库的_beginthreadex)
- Windows核心编程读书笔记3——线程(3) 正确编写C/C++运行库下的线程
- Windows核心编程<读书笔记十三>Windows的内存结构
- windows核心编程--内存笔记
- Windows核心编程:内存体系结构
- Windows核心编程:内存体系结构
- Windows核心编程--Windows内存体系结构
- windows核心编程---windows内存体系结构
- windows核心编程---CompareString函数
- [C++]《Windows核心编程》读书笔记
- [C++]《Windows核心编程》读书笔记
- 读书笔记——Windows核心编程(2)禁止C运行时触发的所有Debug Assertion Failed对话框
- C语言的内存对齐
- c语言的内存对齐
- C语言的内存对齐
- 《c专家编程》笔记--linux内存对齐
- windows核心编程--内存结构
- 智能上海道闸特点
- 动态库的创建及使用
- HNU ACM: Cheering up the Cows (kruscal)
- 什么是IDOC,以及IDOC的步骤
- 网络编程常见问题总结
- 《Windows核心编程》---C运行库的内存对齐函数
- 我们的纪念日
- 多线程之volatile
- 《Symbian 因何没落》评论区诟詈公案后记
- Doxygen使用简介(3)
- API HOOK
- 【动态规划】【RQNOJ】纪念品分组【恶搞版】
- Android源码获取
- CActive类介绍