1

Here's code which works normally:

char a[100];
for (int i = 0; i < 100; i++)
     a[i] = 0;
__m128i x = _mm_load_si128((__m128i *) a);

But if I dynamically allocate memory, VS 2013 will interrupt:

char *a = new char[100];
for (int i = 0; i < 100; i++)
     a[i] = 0;
__m128i x = _mm_load_si128((__m128i *) a);

How can I use both dynamic memory and aligned load instruction?

KUN
  • 527
  • 4
  • 18

2 Answers2

2

Beacuse of sizeof is returning the size of a char pointer: 4 bytes.

LPs
  • 16,045
  • 8
  • 30
  • 61
0

You could either manipulate the new operator to return aligned memory (which might not be the best idea maintainance-wise), or you might allocate more memory and just operate on the aligned part of it. If, for instance, you need an alignment on an 8-byte even address, allocate 8 bytes more and work on an aligned pointer as follows.

char* a;
uintptr_t unaligned = (uintptr_t)a;
uintptr_t aligned = ( unaligned % 8 ) ? unaligned : ( unaligned % 8 ) + 8;
char* a_aligned = (char*)aligned;

The type uintptr_t is defined in stdint.h.

Codor
  • 17,447
  • 9
  • 29
  • 56