0

I need to do a dot product of two vectors and then display the result. From what I have learned, the best I can do with C++ is return the address of the resulting vector (the pointer). I am currently doing that with this code:

// Dot product.
double * dot(double u[3], double v[3]) {
    double result[3];
    result[0] = u[0] * v[0];
    result[1] = u[1] * v[1];
    result[2] = u[2] * v[2];
    return result;

I then need to print the result, so I have another function that accepts the pointer:

void pvec(double * ptr) {
    cout << "[" << *(ptr + 0) <<", " << *(ptr + 1) << ", " << *(ptr + 2) << "]\n";
}

This does not work. The resulting values are all garbage.

How can I make this work? Coming from python, I really feel like not being able to pass arrays through functions is like have no arms or legs, and I can't manage to play the pointer game right.

Michael
  • 77
  • 6
  • 2
    Function `dot` returns a pointer to a local object. When it is dereferenced, the behaviour is undefined. –  Jun 15 '16 at 09:38
  • 2
    Besides, **don't play pointer game**. Please do things in C++ way. –  Jun 15 '16 at 09:44
  • 1
    You can make the result either static `static double result[3];` or allocate it dynamic (better) `double * result = new double[3];` and free it if not needed `delete[] ptr`. See https://ideone.com/FU8L0I for live demo. – kwarnke Jun 15 '16 at 09:59
  • 1
    @kwarnke Disagree. We should always prefer proper container types. –  Jun 15 '16 at 10:05
  • @kwarnke Thank you for the static suggestion. That worked. Now I'll go read about that and also look at the previously answered question. – Michael Jun 15 '16 at 17:13

1 Answers1

1

The simplest way, in my opinion, is to change the dot prototype to be the following:

  void dot(double u[3], double v[3], double result[3])

and adjust rest of the code to it.

GMichael
  • 2,726
  • 1
  • 20
  • 30
  • 2
    No. The simplest way is to use proper container types, like `std::vector`. –  Jun 15 '16 at 09:48
  • @NickyC I beg to differ. Containers add to much overhead and complexity. Especially for newcomers. – GMichael Jun 15 '16 at 09:50
  • 1
    No. The standard library containers respect the zero-overhead principle. It is easy to treat a container as a normal object, esp. having Python background. The learning is a lot more difficult when it involves complications including but not limited to decay from array to pointer, adjustment from array to pointer in function argument, object/array lifetime and dangling pointer, array size tracking, etc. –  Jun 15 '16 at 10:02
  • @NickyC I looked into vector when I was first deciding how best to implement my spacial vectors, and from the documentation I read, the C++ vector exactly everything you don't need for a spacial vector. For my project, speed is very important, and as Michael said, vector is more costly than array. Thank you all for the info. – Michael Jun 15 '16 at 17:07
  • @Michael `std::vector` is just an example. `std::array` is another one. And there are a lot more proper container types among the standard library and third party libraries. Cost is simply not an excuse because the cost does not come from *using container*. It comes from *choosing the wrong container*. I cannot choose it for you because I don't know your context. It does not mean you have to troll yourself with pointer game. –  Jun 16 '16 at 00:33
  • Ok. Well I found out that just making a class that is an array makes passing arrays through functions no issue. – Michael Jun 16 '16 at 03:33