David Lefaivre
David Lefaivre

Reputation: 211

malloc with aligned memory in newLib

I'm currently working on a project using an Atmel board (SAM4C ARM Cortex-M4) and I noticed that when I set the bit "trap unaligned word accesses", I always got a "Unaligned Access Usage Fault".

After some investigation, I realized that malloc return block of memory that are unaligned. So, I was wondering if there was a way to configure malloc so it will allocate memory at an align pointer? I know that memalign can do the trick, but since there is already too many place where I use malloc, it would be simpler if I could keep using malloc instead.

I'm using the library "newLib".

Upvotes: 0

Views: 697

Answers (1)

Russ Schultz
Russ Schultz

Reputation: 2689

The ISO spec states malloc() always returns a memory address that's suitable for a pointer to any object that fits within the size specified. In practice, this generally means it should be aligned on a 8 byte boundary.

If it isn't, then it's a non-conformant implementation and should be spanked.

That being said, I'd be really, really, really, surprised if newLib wasn't conformant.

Upvotes: 2

Related Questions