Quantcast
Channel: How to allocate aligned memory only using the standard library? - Stack Overflow
Browsing all 18 articles
Browse latest View live

Answer by J-a-n-u-s for How to allocate aligned memory only using the...

The first thing that popped into my head when reading this question was to define an aligned struct, instantiate it, and then point to it. Is there a fundamental reason I'm missing since no one else...

View Article



Answer by Ian Ollmann for How to allocate aligned memory only using the...

We do this sort of thing all the time for Accelerate.framework, a heavily vectorized OS X / iOS library, where we have to pay attention to alignment all the time. There are quite a few options, one or...

View Article

Answer by user3415603 for How to allocate aligned memory only using the...

For the solution i used a concept of padding which aligns the memory and do not waste the memory of a single byte . If there are constraints that, you cannot waste a single byte. All pointers allocated...

View Article

Answer by Deepthought for How to allocate aligned memory only using the...

If there are constraints that, you cannot waste a single byte, then this solution works: Note: There is a case where this may be executed infinitely :D void *mem; void *ptr; try: mem = malloc(1024); if...

View Article

Answer by Chris for How to allocate aligned memory only using the standard...

MacOS X specific: All pointers allocated with malloc are 16 bytes aligned. C11 is supported, so you can just call aligned_malloc (16, size). MacOS X picks code that is optimised for individual...

View Article


Answer by resultsway for How to allocate aligned memory only using the...

You can also add some 16 bytes and then push the original ptr to 16bit aligned by adding the (16-mod) as below the pointer : main(){ void *mem1 = malloc(1024+16); void *mem = ((char*)mem1)+1; // force...

View Article

Answer by Ramana for How to allocate aligned memory only using the standard...

long add; mem = (void*)malloc(1024 +15); add = (long)mem; add = add - (add % 16);//align to 16 byte boundary ptr = (whatever*)(add);

View Article

Answer by Lutorm for How to allocate aligned memory only using the standard...

I'm surprised noone's voted up Shao's answer that, as I understand it, it is impossible to do what's asked in standard C99, since converting a pointer to an integral type formally is undefined...

View Article


Answer by neuron for How to allocate aligned memory only using the standard...

usage of memalign, Aligned-Memory-Blocks might be a good solution for the problem.

View Article


Answer by Shao for How to allocate aligned memory only using the standard...

Unfortunately, in C99 it seems pretty tough to guarantee alignment of any sort in a way which would be portable across any C implementation conforming to C99. Why? Because a pointer is not guaranteed...

View Article

Answer by Adisak for How to allocate aligned memory only using the standard...

On the 16 vs 15 byte-count padding front, the actual number you need to add to get an alignment of N is max(0,N-M) where M is the natural alignment of the memory allocator (and both are powers of 2)....

View Article

Answer by An̲̳̳drew for How to allocate aligned memory only using the...

Here's an alternate approach to the 'round up' part. Not the most brilliantly coded solution but it gets the job done, and this type of syntax is a bit easier to remember (plus would work for alignment...

View Article

Answer by Steve Jessop for How to allocate aligned memory only using the...

Three slightly different answers depending how you look at the question: 1) Good enough for the exact question asked is Jonathan Leffler's solution, except that to round up to 16-aligned, you only need...

View Article


Answer by Don Wakefield for How to allocate aligned memory only using the...

Perhaps they would have been satisfied with a knowledge of memalign? And as Jonathan Leffler points out, there are two newer preferable functions to know about. Oops, florin beat me to it. However, if...

View Article

Answer by florin for How to allocate aligned memory only using the standard...

You could also try posix_memalign() (on POSIX platforms, of course).

View Article


Answer by Jonathan Leffler for How to allocate aligned memory only using the...

Original answer { void *mem = malloc(1024+16); void *ptr = ((char *)mem+16) & ~ 0x0F; memset_16aligned(ptr, 0, 1024); free(mem); } Fixed answer { void *mem = malloc(1024+15); void *ptr =...

View Article

How to allocate aligned memory only using the standard library?

I just finished a test as part of a job interview, and one question stumped me, even using Google for reference. I'd like to see what the StackOverflow crew can do with it: The memset_16aligned...

View Article


Answer by stackguy for How to allocate aligned memory only using the standard...

size =1024; alignment = 16; aligned_size = size +(alignment -(size % alignment)); mem = malloc(aligned_size); memset_16aligned(mem, 0, 1024); free(mem); Hope this one is the simplest implementation,...

View Article
Browsing all 18 articles
Browse latest View live




Latest Images