1

I am not very used to C and what I am trying to do seems a bit complicated.

I would like to create two or more double-precision arrays that are 4096 bytes aligned. Here is what I tried and the various responses I get:

double *pp[2];

// Here  gcc warns about "cast to pointer from integer of different size"
    pp[0]=(double *)aligned_alloc(4096, 10*4096*sizeof(double) );

   *(pp[0]+1) = 55.55;  // This compiles but segfaults

// or

   *pp[0][1] = 55.55; //  This gives compilation error.

Has anyone any suggestions about doing this right?

Thank you.

PS: is there a way of doing the same in fortran?

FvKHB
  • 11
  • 1
  • 1
    Assuming you have a version of gcc that supports C11, you need to include stdlib.h – 2501 Oct 21 '16 at 09:04
  • 1
    [Don't cast the result of `malloc` in C](http://stackoverflow.com/q/605845/995714) – phuclv Oct 21 '16 at 09:07
  • adding -std=c11 did the trick. it now seems to be running ok. thanks again. – FvKHB Oct 21 '16 at 09:44
  • `*(pp[0]+1)` can be written as just `pp[0][1]` (with no `*` operator needed). However, you need to provide a _complete, verifiable_ example - an [MCVE]. – davmac Oct 21 '16 at 09:48
  • i am testing all this in a prog of few lines before i use it elsewhere. – FvKHB Oct 21 '16 at 09:57

1 Answers1

0

Add the necessary header:

#include <stdlib.h>

Then, the usage for the first array should be:

double *pp[2];
pp[0] = aligned_alloc(...);
pp[0][0] = 55.55;
pp[0][1] = 100;

Remember that a[0] is the same as *(a + 0), you don't need the asterisk and the brackets.

unwind
  • 391,730
  • 64
  • 469
  • 606