0

I am new working with new Intel instructions, so until now I just had been working with static memory, so I have a declaration as:

__m128i  pResult[10];

But now I am wondering if it is possible to allocate the memory in a dynamic way, as using malloc or calloc but for this new instructions.

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
  • You can't use malloc, it doesn't have a sufficient alignment guarantee. Check your CRT for a version that allows specifying alignment. For GCC it's memalign() or posix_memalign(), for MSVC it's _aligned_malloc() – Hans Passant Sep 09 '12 at 10:00

1 Answers1

1

Just like any other malloc:

__m128i *p = (__m128i*)malloc(10 * sizeof(__m128i));
Keith Randall
  • 22,985
  • 2
  • 35
  • 54
  • You should use `calloc()` rather than `malloc()` to make the intent more clear. Also `calloc()` zeroes while `malloc()` does not. – Jonathan Grynspan Sep 08 '12 at 23:48
  • 3
    @JonathanGrynspan: you should use `calloc` if and only if you want memory zeroed, IMO. – Keith Randall Sep 08 '12 at 23:49
  • 1
    @JonathanGrynspan calloc makes the intent less clear than malloc unless the intent is to clear the memory, but no such intent was stated. – Jim Balter Sep 09 '12 at 00:13
  • @JimBalter: `calloc()` makes clear the intent is to allocate memory for an array of `M` items of size `N`. `malloc()` does no such thing. And since it is never valid to use uninitialized memory in a C program... – Jonathan Grynspan Sep 09 '12 at 00:18
  • 1
    @JonathanGrynspan malloc is for allocating memory, calloc is for allocating it and clearing it. M*N is just as clear as two arguments of M and N. There is no evidence that the uninitialized memory will be used before being set. I would never hire anyone who routinely uses calloc where malloc is adequate, or who assumes that all allocated memory must be cleared. Goodbye. – Jim Balter Sep 09 '12 at 00:22
  • Casting the return value of `malloc()` is a bad idea too. – mlp Sep 09 '12 at 01:21
  • @mip: what would you do instead? – Keith Randall Sep 09 '12 at 05:29
  • @KeithRandall: I'm guessing not cast it. – Michael Foukarakis Sep 09 '12 at 09:59
  • @JimBalter: The question gives a declaration and mentions a static context; that's probably why memory initialization may be assumed as intended. – Michael Foukarakis Sep 09 '12 at 10:01
  • @MichaelFoukarakis: not casting it gives me a `invalid conversion from ‘void*’ to ‘int*’` error. – Keith Randall Sep 09 '12 at 14:12
  • Are you using a C++ compiler? – Michael Foukarakis Sep 09 '12 at 14:39
  • Yes, that's a good point. You don't need the cast in C. I'm back and forth between C and C++ so often I'm in the habit of always including it. – Keith Randall Sep 09 '12 at 17:00
  • @MichaelFoukarakis The thing is called `pResult`; it's grossly irrational to assume that it needs to be cleared. – Jim Balter Sep 09 '12 at 19:29