Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to align memory in Objective C?

Apple recommends to have your data 16 byte aligned when you use Accelerate Framework. How do you do this in practice?

If I have an array int[10000];, is there a pragma? How do I align this to 16 byte?

like image 285
OMH Avatar asked Mar 02 '14 20:03

OMH


2 Answers

In order to align data you need to use #pragma pack. To get 16 byte alignement you would need to use:

#pragma pack(push,16) ... your data structure here ... #pragma pack(pop)

I'd recommend you to read more about alignment, coz you may end up with corrupted data.

like image 152
VenoMKO Avatar answered Sep 19 '22 03:09

VenoMKO


malloc and friends always have 16 byte alignment. Among other reasons to support vectors.

If you have an int array [10000]; then most likely it shouldn't be fixed size, and not on the stack, but allocated using malloc. And then it has 16 byte alignment.

like image 40
gnasher729 Avatar answered Sep 18 '22 03:09

gnasher729