What does posix_memalign/memalign do

后端 未结 7 971
醉梦人生
醉梦人生 2020-12-04 06:30

I\'m trying to understand what functions memalign() and posix_memalign() do. Reading the available documentation didn\'t help.

Can someone

7条回答
  •  失恋的感觉
    2020-12-04 06:59

    As memalign is obsolete (ref: man page), only the difference between malloc() and posix_memalign() will be described here. malloc() is 8-byte aligned (eg, for RHEL 32-bit), but for posix_memalign(), alignment is user-definable. To know the use of this, perhaps one good example is setting memory attribute using mprotect(). To use mprotect(), the memory pointer must be PAGE aligned. And so if you call posix_memalign() with pagesize as the alignment, then the returned pointer can easily submit to mprotect() to set the read-write-executable attributes. (for example, after you copy the data into the memory pointer, you can set it to read-only attribute to protect it from being modified). "malloc()"'s returned pointer cannot be use here.

提交回复
热议问题