I also want to know whether glibc malloc() does this.
相关问题
- How should I malloc/realloc with a struct that inc
- Segmentation fault on initializing a 2D array
- invalid application of 'sizeof' to incompl
- New vs. Malloc, when overloading New
- Correct use of free() when deallocating a 2d matri
相关文章
- How may I create a matrix in C using malloc and av
- Sample example program to get the malloc consolida
- Defining a Structure in C with Malloc
- malloc undefined
- Why is the return value of malloc(0) implementatio
- Understanding “corrupted size vs. prev_size” glibc
- returning string from function without malloc
- Outputting to stderr whenever malloc/free is calle
Suppose that you have the structure.
Without alignment, it would be laid out in memory like this (assuming a 32-bit architecture):
The problem is that on some CPU architectures, the instruction to load a 4-byte integer from memory only works on word boundaries. So your program would have to fetch each half of
b
with separate instructions.But if the memory was laid out as:
Then access to
b
becomes straightforward. (The disadvantage is that more memory is required, because of the padding bytes.)Different data types have different alignment requirements. It's common for
char
to be 1-byte aligned,short
to be 2-byte aligned, and 4-byte types (int
,float
, and pointers on 32-bit systems) to be 4-byte aligned.malloc
is required by the C standard to return a pointer that's properly aligned for any data type.glibc
malloc
on x86-64 returns 16-byte-aligned pointers.Alignment requirements specify what address offsets can be assigned to what types. This is completely implementation-dependent, but is generally based on word size. For instance, some 32-bit architectures require all
int
variables start on a multiple of four. On some architectures, alignment requirements are absolute. On others (e.g. x86) flouting them only comes with a performance penalty.malloc
is required to return an address suitable for any alignment requirement. In other words, the returned address can be assigned to a pointer of any type. From C99 §7.20.3 (Memory management functions):The
malloc()
documentation says:Which is true for most everything you do in C/C++. However, as pointed out by others, many special cases exist and require a specific alignment. For example, Intel processors support a 256 bit type:
__m256
, which is most certainly not taken in account bymalloc()
.Similarly, if you want to allocate a memory buffer for data that is to be paged (similar to addresses returned by
mmap()
, etc.) then you need a possibly very large alignment which would waste a lot of memory ifmalloc()
was to return buffers always aligned to such boundaries.Under Linux or other Unix systems, I suggest you use the
posix_memalign()
function:This is the most current function that one wants to use for such needs.
If you have particular memory alignemnt needs (for particular hardware or libraries), you can check out non-portable memory allocators such as
_aligned_malloc()
andmemalign()
. These can easily be abstracted behind a "portable" interface, but are unfortunately non-standard.