0

Suppose I need a function which generates an array r[3] = {x, y, z} where x[1000], y[1000], z[1000] each one is an array and has 1000 double float points. I made a function which returns the location of r form where we need to access the x, y,z by unpacking the memory location. I have the code like:

double cylinder(double radius, double height)
{
    double x[1000], y[1000], z[1000];
    double theta = 0, dtheta = M_PI / 500, dz = height / 1000;
    z[0] = 0;
    y[0] = 0;
    x[0] = radius;
    for (int i = 1; i < 1000; i++)
    {
        z[i] = z[i - 1] + dz;
        theta = theta + dtheta;
        x[i] = radius * cos(theta);
        y[i] = radius * sin(theta);
    }
    double * r[3] = {x,y,z};
    return **r;
}

now if I use

data = cylinder(5, 10);
cout<<data<<endl;

It should return a location but why it returns 5. I need to have the location of 'data' and from that I will get 3 more memory locations which 3 has all the 1000 points on each location. I will be very thankful to get the solution.

Limey
  • 10,234
  • 2
  • 12
  • 32
Subhra Dey
  • 31
  • 1
  • 5
  • _"It should return a location"_ - why should it? `return **r;` is "value of value of `r`", which is the first element of the first element of `r`. Actually return a pointer would be wrong here, because the arrays it points to won't exist after the function ends. See the duplicate for ways to solve this problem. – Lukas-T Nov 24 '20 at 07:04
  • Does this answer your question? [Return array in a function](https://stackoverflow.com/questions/3473438/return-array-in-a-function) – Lukas-T Nov 24 '20 at 07:06
  • Returning an address of local variables that fall out of scope. *Undefined behaviour*. Since this is C++ use `std::vector` to solve your problem here, return `std::vector>`. – tadman Nov 24 '20 at 07:13
  • There's no way to return an array from a function in C++. Arrays are very poor, use vectors instead. – john Nov 24 '20 at 07:56

1 Answers1

0

This can be easily fixed by using the Standard Library std::vector in conjunction with std::tuple which is like a really lightweight fixed-length array:

#include <vector>
#include <tuple>

std::vector<std::tuple<double,double,double>> cylinder(double radius, double height)
{
    std::vector<std::tuple<double,double,double>> result;

    double theta = 0;
    double dtheta = M_PI / 500;
    double dz = height / 1000;

    // No need for an array or back-references here
    double x = radius;
    double y = 0;
    double z = 0;

    // Add the 0th entry
    result.push_back({ x, y, z });

    for (int i = 1; i < 1000; i++)
    {
        z += dz;
        theta = theta + dtheta;
        x = radius * cos(theta);
        y = radius * sin(theta);

        // Add subsequent entries
        result.push_back({ x, y, z });
    }

    return result;
}

Where now memory management issues are solved by using containers.

If x, y and z are semantically important you might even want to make a small struct with those properties instead to give it more context.

tadman
  • 208,517
  • 23
  • 234
  • 262