简体   繁体   English

从C ++中的函数返回一个指向数组的指针?

[英]Return a pointer to array from a function in C++?

I am a beginning programmer and I have a question about a function that returns a pointer to array of doubles in C++. 我是一个初学程序员,我有一个关于一个函数的问题,该函数返回一个指向C ++中双精度数组的指针。 The function takes two arrays and adds up each element, like in a sum of vectors. 该函数采用两个数组并将每个元素相加,就像向量之和一样。

I think the correct way to do is.... 我认为正确的方法是....

double *myfunction(double *x, double *y, int n){
  double *r = new double[n];
  for(int i=0;i<n;i++){
    r[i] = x[i]+y[i];
  }
  return r;
}

The problem is that I use that function in a while-loop in the main function like this 问题是我在main函数的while循环中使用该函数,就像这样

int main(){
  double *x, *y, *s;
  x = new double[2];
  y = new double[2];
  x = {1,1};
  y = {2,2};

  while(/*some condition */){
    /*some process...*/

    s = myfunction(x,y, 2);

    /*some process...*/
  }

  delete[] x;
  delete[] y;
  delete[] s;
}

My question is what about the memory leak? 我的问题是内存泄漏怎么办? Each time I use "myfunction" (inside the while-loop) I reserve memory for the variable "s", that means that if the while-loop is executed 5 times, then the program reserves 5 times the memory for the variable "s"? 每次我使用“myfunction”(在while循环中)我为变量“s”保留内存,这意味着如果while循环执行5次,那么程序为变量“s”保留5倍的内存“?

Is there exists a way to do this (return a pointer to arrays from a function and use that function inside a loop)?? 是否存在这样做的方法(从函数返回指向数组的指针并在循环中使用该函数)?

Thank you in advanced. 先谢谢你。

To avoid memory leak you need to use s as soon as you get it and delete it once you are done. 为了避免内存泄漏,您需要在获得后立即使用s ,并在完成后将其删除。

 int main(){
      double *x, *y, *s;
      x = new double[2];
      y = new double[2];
      x = {1,1};
      y = {2,2};

      while(//some condition ){
        s = myfunction(x,y, 2);
        //do some process here
        delete[] s;
      }
      delete[] x;
      delete[] y;

    }

I'd say the more correct way to write myfunction is: 我会说更正确的写myfunction是:

std::vector<double> myfunction(double *x, double *y, int n){
  std::vector<double> r;
  r.reserve(n);
  for(int i=0;i<n;i++){
    r.push_back(x[i]+y[i]);
  }
  return r;
}

That way, you don't have to worry about memory leaks, and your while loop can just be: 这样,您不必担心内存泄漏,而您的while循环可能只是:

while (/* some condition*/) {
    std::vector<double> s = myfunction(x, y, 2);
    // whatever
}

You asked: 您询问:

Each time I use "myfunction" (inside the while-loop) I reserve memory for the variable "s", that means that if the while-loop is executed 5 times, then the program reserves 5 times the memory for the variable "s"? 每次我使用“myfunction”(在while循环中)我为变量“s”保留内存,这意味着如果while循环执行5次,那么程序为变量“s”保留5倍的内存“?

The answer is Yes . 答案是肯定的

You also asked: 你还问:

Is there exists a way to do this (return a pointer to arrays from a function and use that function inside a loop)?? 是否存在这样做的方法(从函数返回指向数组的指针并在循环中使用该函数)?

The answer is Yes . 答案是肯定的 You need to add code to delete the returned memory, 你需要添加代码来删除返回的内存,

  while( /*some condition */){
    s = myfunction(x,y, 2);

    // Use s

    // Now delete s.
    delete[] s;
  }

A better solution than having to deal with new and delete is to return a std::vector from myfunction . 比处理newdelete更好的解决方案是从myfunction返回一个std::vector Then, you don't need worry about managing memory. 然后,您不必担心管理内存。 That is the answer by Barray 这是巴瑞的答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM