what does alignment means in c/c++ malloc

Status
Not open for further replies.

syedshan

Advanced Member level 1
Joined
Feb 27, 2012
Messages
463
Helped
27
Reputation
54
Reaction score
26
Trophy points
1,308
Location
Jeonju, South Korea
Activity points
5,134

can any one explain what does alignment means when we are using dynamic memory allocation related functions, like

_alligned_maloc()

for example : while searching for posix_memalign() function I get to know

Alignment refers to the native word size of a processor/system. For example if a system has a native word size of 32-bit or four bytes, ideally a word stored in memory/storage would have a beginning address on a multiple of four, e.g., 0000, 0004, 0008, etc. A word stored at beginning memory/storage address not an even multiple of four, such as 0010, would require significantly more processing to successfully read the misaligned word, i.e., two four byte block reads instead of one byte block read and additional processing to retrieve the four byte word. A misalignment can complicate other system related tasks including caching and DMA.


Typical x86 Alignments:

The posix_memalign() routine ensures the proper alignment during dynamic allocation by specifying an alignment interval/multiple.
In the following example an array of double with a size of 10 elements is aligned on a beginning memory/address with an even multiple of 64:

Code:
#include <stdlib.h>
double *foo(void) 
{
   double *var;//create array of size 10
   int     ok;
 
   ok = posix_memalign((void**)&var, 64, 10*sizeof(double));
 
   if(ok != 0)
     return NULL;
 
   return var;
}

Does this help in your understanding of alignment and the posix routines which facilitate proper alignment?

BigDog
 

ok....

So I partially understand the concept, frankly :|

Can you explain the meaning of this statement used in a sample program I've been trying to understand

Code:
dma_buffer_in = (char *) _alligned_malloc(_MEMORY_SIZE, 4096);

//where this function is used to allocate memory on a specified memory boundary,
// #define _MEMORY_SIZE 4096
// char * dma_buffer_in;//buffer used for DMA transaction
 


Specifics may depend on the the routines implementation, from which library/compiler/system this particular routine originates.

However, as both the size of the allocation and its alignment are the same, an explanation follows:

The _alligned_malloc() routine dynamically allocates a block of memory, dma buffer, with a specified size, 4096 bytes in this case, with a specific alignment of 2^n, also 4096 or 2^12 in this case. The routine return a void pointer (void *), which is cast as a pointer of type char, which facilitates byte by byte access of the buffer.

Usually in these case a larger block of memory is actually allocated from which the small requested block can be properly aligned, therefore call to aligned dynamic allocation routines can consume far more memory resources than originally intended, particularly when the alignment specification is large, a high power of 2.

Does the above explanation help?

BigDog
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…