【c语言】memalign
在GNU系统中,malloc或realloc返回的内存块地址都是8的倍数(如果是64位系统,则为16的倍数)。如果你需要更大的粒度,请使用 memalign 或valloc。这些函数在头文件“stdlib.h”中声明。
man memalign
POSIX_MEMALIGN(3) Linux Programmer's Manual POSIX_MEMALIGN(3)
NAME
posix_memalign, aligned_alloc, memalign, valloc, pvalloc - allocate aligned memory
SYNOPSIS
#include <stdlib.h>
int posix_memalign(void **memptr, size_t alignment, size_t size);
void *aligned_alloc(size_t alignment, size_t size);
void *valloc(size_t size);
#include <malloc.h>
void *memalign(size_t alignment, size_t size);
void *pvalloc(size_t size);
Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
posix_memalign(): _POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600
aligned_alloc(): _ISOC11_SOURCE
valloc():
Since glibc 2.12:
_BSD_SOURCE ||
(_XOPEN_SOURCE >= 500 ||
_XOPEN_SOURCE && _XOPEN_SOURCE_EXTENDED) &&
!(_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600)
Before glibc 2.12:
_BSD_SOURCE || _XOPEN_SOURCE >= 500 || _XOPEN_SOURCE && _XOPEN_SOURCE_EXTENDED
(The (nonstandard) header file <malloc.h> also exposes the declaration of valloc(); no feature test macros are required.)
DESCRIPTION
The function posix_memalign() allocates size bytes and places the address of the allocated memory in *memptr. The address of the allocated memory
will be a multiple of alignment, which must be a power of two and a multiple of sizeof(void *). If size is 0, then the value placed in *memptr is
either NULL, or a unique pointer value that can later be successfully passed to free(3).
The obsolete function memalign() allocates size bytes and returns a pointer to the allocated memory. The memory address will be a multiple of
alignment, which must be a power of two.
The function aligned_alloc() is the same as memalign(), except for the added restriction that size should be a multiple of alignment.
The obsolete function valloc() allocates size bytes and returns a pointer to the allocated memory. The memory address will be a multiple of the
page size. It is equivalent to memalign(sysconf(_SC_PAGESIZE),size).
The obsolete function pvalloc() is similar to valloc(), but rounds the size of the allocation up to the next multiple of the system page size.
For all of these functions, the memory is not zeroed.
RETURN VALUE
aligned_alloc(), memalign(), valloc(), and pvalloc() return a pointer to the allocated memory, or NULL if the request fails.
posix_memalign() returns zero on success, or one of the error values listed in the next section on failure. The value of errno is indeterminate
after a call to posix_memalign().
ERRORS
EINVAL The alignment argument was not a power of two, or was not a multiple of sizeof(void *).
ENOMEM There was insufficient memory to fulfill the allocation request.
VERSIONS
The functions memalign(), valloc(), and pvalloc() have been available in all Linux libc libraries.
The function aligned_alloc() was added to glibc in version 2.16.
The function posix_memalign() is available since glibc 2.1.91.
CONFORMING TO
The function valloc() appeared in 3.0BSD. It is documented as being obsolete in 4.3BSD, and as legacy in SUSv2. It does not appear in
POSIX.1-2001.
The function pvalloc() is a GNU extension.
The function memalign() appears in SunOS 4.1.3 but not in 4.4BSD.
The function posix_memalign() comes from POSIX.1d.
The function aligned_alloc() is specified in the C11 standard.
Headers
Everybody agrees that posix_memalign() is declared in <stdlib.h>.
On some systems memalign() is declared in <stdlib.h> instead of <malloc.h>.
According to SUSv2, valloc() is declared in <stdlib.h>. Libc4,5 and glibc declare it in <malloc.h>, and also in <stdlib.h> if suitable feature
test macros are defined (see above).
NOTES
On many systems there are alignment restrictions, for example, on buffers used for direct block device I/O. POSIX specifies the path‐
conf(path,_PC_REC_XFER_ALIGN) call that tells what alignment is needed. Now one can use posix_memalign() to satisfy this requirement.
posix_memalign() verifies that alignment matches the requirements detailed above. memalign() may not check that the alignment argument is correct.
POSIX requires that memory obtained from posix_memalign() can be freed using free(3). Some systems provide no way to reclaim memory allocated with
memalign() or valloc() (because one can pass to free(3) only a pointer obtained from malloc(3), while, for example, memalign() would call malloc(3)
and then align the obtained value). The glibc implementation allows memory obtained from any of these functions to be reclaimed with free(3).
The glibc malloc(3) always returns 8-byte aligned memory addresses, so these functions are needed only if you require larger alignment values.
SEE ALSO
brk(2), getpagesize(2), free(3), malloc(3)
COLOPHON
This page is part of release 3.74 of the Linux man-pages project. A description of the project, information about reporting bugs, and the latest
version of this page, can be found at http://www.kernel.org/doc/man-pages/.
GNU 2013-09-02 POSIX_MEMALIGN(3)
参考:
linux memalign()_junlon2006的博客-CSDN博客
【c语言】memalign相关推荐
- 【嵌入式C语言】内存分配 malloc 和 free
文章目录 永不分离的 malloc 和 free 1. 简介 2. void * 3. malloc 4. free 5. malloc.free 小结 5.1.连续内存块 5.2.双宿双飞才好 5. ...
- Go语言的错误异常处理机制及其应用
一.背景 在日常编写golang程序或阅读别人的golang代码时,我们总会看到如下的一堆代码块: xx, err = func(xx) if err != nil {//do sth. to tac ...
- Go 知识点(19)— Go 语言中的野指针
野指针是一种指向内存位置是不可知的指针,一般是由于指针变量在声明时没有初始化所导致的.在 Go语言中,布尔类型的零值为 false,数值类型的零值为 0,字符串类型的零值为 "", ...
- gcc 自动识别的文件扩展名,gcc/g++ -x 选项指定语言,不同 gcc 版本 -std 编译选项支持列表
对于执行 C 或者 C++ 程序,需要借助 gcc(g++)指令来调用 GCC 编译器. 对于以 .c 为扩展名的文件,GCC 会自动将其视为 C 源代码文件 对于以 .cpp 为扩展名的文件,GCC ...
- OpenCV 笔记(07)— Mat 对象输出格式设置(Python 格式、CSV 格式、NumPy 格式、C 语言格式)
首先是下面代码中将要使用的 r 矩阵的定义.需要注意,我们可以通过用 randu 函数产生的随机值来填充矩阵, 需要给定一个上限和下限来确保随机值在期望的范围内. Mat r = Mat(2, 3, ...
- 利用牛顿法求平方根-Go语言实现
牛顿法解释 百度的解释如下: 通俗的解释就是:多数方程不存在求根公式,牛顿提出了一种用迭代来求方程近似根的方法.思路就是不断取切线,用线性方程的根逼近非线性方程f(x)=0f(x)=0f(x)=0的根 ...
- 翻转二叉树 c语言实现 递归 栈 队列
前言 题目比较好理解,就是翻转二叉树 代码 c语言实现 #include<stdio.h> #include<stdlib.h> #include<string.h> ...
- 字符串全排列的问题 python和c语言实现
前言 这是一个的经典的问题 设计一个算法,输出一个字符串字符的全排列. 比如,String = "abc" 输出是"abc","bac",& ...
- 快速排序的递归和非递归实现 c语言版本
代码 挖坑法 解释 选取一个关键字(key)作为枢轴,一般取整组记录的第一个数/最后一个,这里采用选取序列第一个数为枢轴,也是初始的坑位. 设置两个变量i = l;j = r;其中l = 0, r = ...
最新文章
- linux下杀死进程的10种方法
- Kail Linux渗透测试教程之网络扫描和嗅探工具Nmap
- Linux之不删除指定文件夹,其他都删(文件和文件夹)
- 二次创业成功人士的19个经验与教训
- 什么是分布式_终于搞懂分布式锁是什么了
- 易语言 设置屏幕刷新率 源码_一块好的手机屏幕应具备什么条件?现在了解还不晚...
- ES6中的模板字符串---反引号``
- 字符串之字符判断以及字母的大小写转换
- vue修改计算属性的值_「Vue学习记录五」计算属性和侦听器
- android自动化测试--appium运行的坑问题及解决方法
- docker安装gamit_ubuntun10.10中安装gamit 10.40
- Python的Numpy库简述
- python计算器算法_Python数学运算入门把Python当作计算器
- 新版个人信息安全规范正式发布:收集生物识别信息前需告知
- 【C++】1079:计算分数加减表达式的值(信息学奥赛)
- js / vue 批量打印二维码图片、PDF、文档
- (CSS)3.三种基本选择器(标签,类,id)
- 狼性教育——让孩子成为主宰命运地强者
- matlab怎么读txt文件字符串,Matlab中读取txt文件的几种方法
- 分布式:分布式系统设计实践。