2

I'm new to vectors and I've been having a read of the gcc documentation trying to get my head around it.

Is it possible to dynamically allocate the size of a vector at run time? It appears as though you have to do this in the typedef like:

typedef double v4sf __attribute__((vector_size (16)));

I want to set the values of the vector to an array of doubles. I've tried like so:

v4sf curr_vect = double_array;

Where double_array is obviously an array of doubles. But this fails to compile. Is it possible to do either of these things?

samturner
  • 2,213
  • 5
  • 25
  • 31

1 Answers1

2

If your platform is POSIX-compliant, you can achieve aligned dynamic memory allocation using the posix_memalign() function:

double *p;
if (posix_memalign((void **)&p, 16, sizeof(*p) * 16) != 0) {
    perror("posix_memalign");
    abort();
}

p[0] = 3.1415927;
// ...

free(p);
  • Sorry for my ignorance but where do I then declare and initialise the vector? – samturner May 21 '13 at 05:22
  • @rheotron You won't then have a `typedef`. It will simply be `double *vector;` as in the example. You allocate aligned memory, then you assign each member a value. –  May 21 '13 at 05:25
  • Ah - I see what you mean. It seems to be allocating the memory correctly. Though, it doesn't seem to be doing vector subtraction. Any ideas? – samturner May 21 '13 at 05:37
  • @rheotron It should. "it doesn't seem to be doing vector subtraction correctly" - precisely what's the error? Where's your code? –  May 21 '13 at 05:38
  • Tried pasting the code here. Didn't work - linked it here: http://pastebin.com/fdTaCkBj – samturner May 21 '13 at 05:40
  • @rheotron Huh? That 1. leaks memory when you assign `array_one` and `array_two` to `a` and `b`, 2. This does pointer subtraction. I don't think that's what you want. I advise you to learn basic C memory management before trying to engange in advanced optimization techniques. –  May 21 '13 at 05:46
  • Ok - I think I understand a bit more now. What I want to be able to do is vector subtraction, sorry, perhaps I should have been more clear in my initial question. Does this solution not address that problem? – samturner May 21 '13 at 05:51
  • @rheotron No, this answer only answers the question you asked. For me it wasn't clear you were also asking about a certain math problem (in fact, you weren't). –  May 21 '13 at 05:55