Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><code>malloc</code> always returns memory that is set to the maximum alignment required by any of the primitive types. This allows <code>malloc</code>'d memory to store any type you may need. My understanding of the description of <code>posix_memalign</code>, is that it returns a memory location who's address will be a multiple of whatever you specify as the alignment.</p> <p>Im not sure how useful this would be when writing a custom memory pool, but I have had a go at providing an example of how this could be implemented. The difference is with my example, anything allocated with <code>malloc_aligned</code> has to be freed with <code>free_aligned</code>; however, with <code>posix_memalign</code> you can use <code>free</code>.</p> <pre><code>#include &lt;stdlib.h&gt; #include &lt;stdio.h&gt; void *malloc_aligned(size_t alignment, size_t bytes) { // we need to allocate enough storage for the requested bytes, some // book-keeping (to store the location returned by malloc) and some extra // padding to allow us to find an aligned byte. im not entirely sure if // 2 * alignment is enough here, its just a guess. const size_t total_size = bytes + (2 * alignment) + sizeof(size_t); // use malloc to allocate the memory. char *data = malloc(sizeof(char) * total_size); if (data) { // store the original start of the malloc'd data. const void * const data_start = data; // dedicate enough space to the book-keeping. data += sizeof(size_t); // find a memory location with correct alignment. the alignment minus // the remainder of this mod operation is how many bytes forward we need // to move to find an aligned byte. const size_t offset = alignment - (((size_t)data) % alignment); // set data to the aligned memory. data += offset; // write the book-keeping. size_t *book_keeping = (size_t*)(data - sizeof(size_t)); *book_keeping = (size_t)data_start; } return data; } void free_aligned(void *raw_data) { if (raw_data) { char *data = raw_data; // we have to assume this memory was allocated with malloc_aligned. // this means the sizeof(size_t) bytes before data are the book-keeping // which points to the location we need to pass to free. data -= sizeof(size_t); // set data to the location stored in book-keeping. data = (char*)(*((size_t*)data)); // free the memory. free(data); } } int main() { char *ptr = malloc_aligned(7, 100); printf("is 5 byte aligned = %s\n", (((size_t)ptr) % 5) ? "no" : "yes"); printf("is 7 byte aligned = %s\n", (((size_t)ptr) % 7) ? "no" : "yes"); free_aligned(ptr); return 0; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload