Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7400365
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T04:14:21+00:00 2026-05-29T04:14:21+00:00

Aligned malloc is posix_memalign , that’s OK, but what about the aligned realloc ?

  • 0

Aligned malloc is posix_memalign, that’s OK, but what about the aligned realloc? Does realloc retain the alignment or how to assure that reallocated memory has the same alignment? Assume Linux and x86_64.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T04:14:22+00:00Added an answer on May 29, 2026 at 4:14 am

    No, realloc on the memory returned from posix_memalign is not guaranteed by either ISO or POSIX to maintain the same alignment. A realloc may simply expand the current block at the same address but it may also move the block to a different address whose alignment is less strict than the original.

    If you want the same alignment, it’s probably best to allocate another block and copy the data over.

    There is, unfortunately, no posix_memalign_realloc function in the Single UNIX Specification either.

    If you don’t want to go through the hassle of copying data every time, you could try the realloc (a) and, if the alignment of that was not as expected, then and only then call posix_memalign to get a correctly aligned address and copy the data in to there, freeing the old address when done.

    This may result in:

    • zero copies (if the current block can be expanded in-place);
    • one copy (if realloc copies but happens to give you a correctly aligned block); or
    • two copies (if realloc copies and then you also have to copy due to misalignment).

    It may also result in less copying than indicated depending on the underlying memory management implementation. For example, a “copy” may simply involve remapping memory blocks rather than physically moving the data.

    So you may want to keep some statistics to see if this scheme is worthwhile.


    (a) Just keep in mind that neither POSIX nor Linux man pages specify whether or not you even can pass these pointers to realloc, only that you can pass them to free.

    However, based on the current GNU libc source code, it appears to work, although that’s no guarantee it will continue to work in future 🙂

    My fear was that it would allocate memory normally (standard alignment) and pass back an offset address (ie, not the actual address allocated, but one N bytes beyond that) which free was intelligent enough to turn back into the actual address before weaving its magic.

    One way of doing that would be to store the actual address immediately before the returned address though this of course would lead to wastage even for regular allocations.

    In that case, free may have been made intelligent (since the specs say it must be able to handle the allocations done by posix_memalign) but realloc may not have been given the same intelligence (since the docs are silent on that matter).

    However, based on GNU glibc 2.14.1, it actually allocates more memory than needed then fiddles with the arena to free up the pre-space and post-space, so that the address returned is a “real” address, usable by free or realloc.

    But, as stated, the documentation doesn’t guarantee this.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Systems demand that certain primitives be aligned to certain points within the memory (ints
By default the expander has a left aligned toggle button but in my WPF
I have a few related questions about managing aligned memory blocks. Cross-platform answers would
What is the difference between aligned and unaligned memory access? I work on an
I have a UILabel with right-aligned text that may vary from between three to
I have a website that is perfectely centered aligned. The CSS code works fine.
I have problems getting some of my views aligned in a relative layout that
I want 'Duration' and 'Cost' Right aligned. I tried this code but it's not
I want to check whether an allocated memory is aligned or not. I am
In my Visual C++ program I use a custom operator new that uses malloc()

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.