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

  • Home
  • SEARCH
  • 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 7066695
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:06:43+00:00 2026-05-28T05:06:43+00:00

To avoid maintaining complex data structures, I want to allocate blocks with quite large

  • 0

To avoid maintaining complex data structures, I want to allocate blocks with quite large alignment (say some kilobytes, possibly megabytes, always by power of two). This allows me to mask the lower bits of a pointer to easily retrieve the address of the beginning of the block it points in.

I’d like a method to guarantee the allocation of such a block with specified alignment, eg. to allocate 4096 byte blocks with 4096 byte alignment. For the method to work, the alignment will always be the size of the blocks, so memory waste is expected to be a concern in the long run.

I’m using C++ (so C and C++ techniques are fine), and any solution should be portable across common desktop environments. Should there be no portable solution, Linux has highest priority.

I’m aware of Win32 memory allocation with large alignment, but if there is a common C library which does this with one function call, I’d happily use it.

Background: I’m experimenting with the Vlist structures described there (ultimate goal is a sort of Scheme interpreter), and I’m currently implementing garbage collection for those lists. I need the quite large memory blocks as arenas for the garbage collector. Should I change the GC technique, I still need the VList blocks to have 32 byte alignment (I’m performing my experiments on 64bit machines).

  • 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-28T05:06:43+00:00Added an answer on May 28, 2026 at 5:06 am

    I’m not aware of a fully portable solution. But _mm_malloc() and _mm_free() seem to be supported by ICC, GCC, and MSVC.

    This was added as part of the aligned memory support for SSE intrinsics.


    Otherwise, you could implement your own fairly easily:

    void* my_malloc(size_t bytes,size_t align){
    
        void *ptr = malloc(bytes + align + sizeof(intptr_t));
    
        if (ptr == NULL)
            return NULL;
    
        //  Get aligned return address
        intptr_t *ret = (intptr_t*)((((intptr_t)ptr + sizeof(intptr_t)) & ~(intptr_t)(align - 1)) + align);
    
        //  Save the free pointer
        ret[-1] = (intptr_t)ptr;
    
        return ret;
    }
    
    void my_free(void *ptr){
        if (ptr == NULL)
            return;
    
        //  Get the free pointer
        ptr = (void*)(((intptr_t*)ptr)[-1]);
    
        free(ptr); 
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am maintaining a large code base and am using a combination of forward
I'm having some issues with maintaining scroll bar locations. The Background I have a
I am maintaining some code that has a trigger on a table to increment
What is the best-practice for maintaining the integrity of linked data entities on update?
I want to create a javascript/jquery function which will resize an img (while maintaining
How do I avoid read locks in my database? Answers for multiple databases welcome!
After trying to avoid JavaScript for years, Iv started using Query for validation in
Should developers avoid using continue in C# or its equivalent in other languages to
If I avoid referencing assemblies that don't exist in the silverlight 2.0 runtime, will
How can you avoid circular dependencies when you're designing two classes with a producer/consumer

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.