Link to home
Start Free TrialLog in
Avatar of Sandra-24
Sandra-24

asked on

Does malloc give you memory aligned on a 32 bit boundary?

Fairly easy to test with a loop and if(memptr % 4 == 0) still I'm lazy, and I'll bet most of the experts here already know the answer to that.

And if malloc doesn't do that, wouldn't it make sense to use a memory allocator that does?

Thanks,
-Sandra
ASKER CERTIFIED SOLUTION
Avatar of chensu
chensu
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of AlexFM
AlexFM

dimitry, you need to add also custom free function.
I am sorry. You are right, thank you.
The simplest solution will be to align the pointer that is received from usual normal with bigger size.
Then there is no need of custom free().
dimitry,
> The simplest solution will be to align the pointer that is received
> from usual normal with bigger size.
> Then there is no need of custom free().

You'll end up carrying around two pointers, one aligned and one the original needed for free().

memalign doesn't work?

Stefan
Stefan, I don't understand your frustration I feel from your comment...
We are trying to help and trying to exchange our experience.
memalign() will work, and if it is portable function that can be used not only under Unix (and maybe Linux)
and it is good for Sandra-24, let her choose it.
Avatar of Sandra-24

ASKER

Stephan is correct, you would need to carry around two pointers that way, you'd need the original so you could free the memory later.

You could provide your own new and delete operators to hide the fact that you have two pointers.

I use a fixed size memory pool in front of malloc in order to speed up some routines.
It might be worth it to use memalign() on those blocks, since it only needs to be done once.

malloc uses HeapAlloc and VirtualAlloc though so I think, atleast on later windows versions, that it will return aligned memory.

Thanks for your input,
-Sandra

Also, I've heard the doug lea allocator returns aligned memory.
Yes - you  can define arbitrary (well, power-of-two-arbitrary) alignments when you redefine MALLOC_ALIGNMENT in dlmalloc. By default, alignment is 8.

But dlmalloc is pretty good, it has very little overhead.