如何申请对齐的动态内存?
来源:互联网 发布:江西网络干部学院app, 编辑:程序博客网 时间:2024/04/28 02:45
windows下有现成的函数:_align_malloc, _align_realloc, _align_free,_align_offset_malloc,_aligned_offset_realloc
见
http://msdn.microsoft.com/zh-tw/data/8z34s9c6
以及
http://msdn.microsoft.com/zh-tw/data/fs9stz4e
下面是一个测试:
http://wenku.baidu.com/view/223fb6737fd5360cba1adba5.html
// crt_aligned_malloc.c#include <malloc.h>#include <stdio.h>int main() { void *ptr; size_t alignment, off_set; // Note alignment should be 2^N where N is any positive int. alignment = 16; off_set = 5; // Using _aligned_malloc ptr = _aligned_malloc(100, alignment); if (ptr == NULL) { printf_s( "Error allocation aligned memory."); 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); // Using _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); // Using _aligned_offset_malloc ptr = _aligned_offset_malloc(200, alignment, off_set); if (ptr == NULL) { printf_s( "Error allocation aligned offset memory."); return -1; } if ( ( (((int)ptr) + off_set) % alignment ) == 0) printf_s( "This pointer, %d, is offset by %d on alignment of %d\n", ptr, off_set, alignment); else printf_s( "This pointer, %d, does not satisfy offset %d " "and alignment %d\n",ptr, off_set, alignment); // Using _aligned_offset_realloc ptr = _aligned_offset_realloc(ptr, 200, alignment, off_set); if (ptr == NULL) { printf_s( "Error reallocation aligned offset memory."); return -1; } if ( ( (((int)ptr) + off_set) % alignment ) == 0) printf_s( "This pointer, %d, is offset by %d on alignment of %d\n", ptr, off_set, alignment); else printf_s( "This pointer, %d, does not satisfy offset %d and " "alignment %d\n", ptr, off_set, alignment); // Note that _aligned_free works for both _aligned_malloc // and _aligned_offset_malloc. Using free is illegal. _aligned_free(ptr);} // Copy Code This pointer, 3280880, is aligned on 16This pointer, 3280880, is aligned on 16This pointer, 3280891, is offset by 5 on alignment of 16This pointer, 3280891, is offset by 5 on alignment of 16
- 如何申请对齐的动态内存?
- 位数对齐的内存申请释放
- 释放动态申请的内存
- c++的动态申请内存
- 指针动态申请内存的错误分析
- 动态申请内存的危险处
- 指针动态申请内存的问题
- Delphi动态申请数组内存的方法
- 内核的几个内存动态申请函数
- 指向指针的指针申请动态内存
- 关于动态申请内存的动机
- 关于动态内存的申请和释放
- 动态申请内存
- 函数动态申请内存
- malloc动态申请内存
- 动态内存申请
- 如何高效的访问内存(数据结构对齐)
- 内存对齐以及如何关闭内存对齐
- c++中类型转换机制
- MFC获得各种视图指针的方法
- nyoj 115
- 异步通知
- 面向对象语言的特性
- 如何申请对齐的动态内存?
- 进程的一些理解
- 数据结构与程序设计第一章编程原则的总结
- 黑马程序员 关于线程的了解一
- volatile和static
- Bash输入输出重定向
- 实现Java对Excel的各种操作
- 智能电视市场是否是有发展?
- 值传递开销大