从函数返回指针.一个指针的值未更新

return pointers from function. Value not updated for one pointer

本文关键字:指针 一个 更新 函数 返回      更新时间:2023-10-16

我试图从函数中获得2个指针并在main中打印它。模糊的事情是一个指针似乎已经恢复了它的值,而另一个没有。两个指针,在调用函数中都有正确的值,就在返回之前。请告诉我,如果你能找出任何程序错误,阻止我得到正确的答案。

    #include<iostream>
    #include<fstream>
    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    using namespace std;
    double* readctrls()
    {
         fstream inputs;
         inputs.open("input_coods.txt");
         int nol = 0,i = 0;
         string line,temp,subtemptrans,subtemprots;
         while(getline(inputs,line))
         {
                ++nol;
         }
//      cout<<nol<<endl;
        inputs.close();
        inputs.open("input_coods.txt");
        string *lines = new (nothrow) string[nol];
        double* trans = new double[nol];
        double* rots = new double[nol];
        trans[0] =float(nol);
        for(int i = 0; i<nol ; i++)
        {
                getline(inputs,lines[i]);
        //      cout<<lines[i]<<endl;
                temp = lines[i];
//              cout<<temp<<endl;
                for(int j = 0; j<temp.length() ; j++)
                {
                        if(temp.at(j) == ' ')
                        {
                                subtemptrans = temp.substr(0,j);
                                subtemprots = temp.substr(j+1,temp.length()-j);
        //                      cout<<subtemprots<<endl;
                                *(trans+i+1) = ::atof(subtemptrans.c_str());
                                *(rots+i) = float(atoi(subtemprots.c_str()));
                        //      cout<<rots[i]<<endl;
                        }
                }
        }                       
        inputs.close();         
//      cout<<rots[2]<<endl;    
        return(rots,trans);                                    
}                               
int main()                              
{                               
        double *trans,*rots;                                   
        (rots,trans) = readctrls();                            
//      cout<<sizeof(trans)<<endl;
        for(int i=0;i<trans[0];i++)
        {                                                      
                cout<<*(trans+i)<<endl;
                cout<<*(rots+i)<<endl;
        }                                                      
}                       

Trans的值在内存中写得很好,并且从main()中完全保留。但是rot给出的是(e^-42)数量级的垃圾值。

c++既不是Python也不是Lua。

一个函数不能返回多个值。

return rots, trans;

这是逗号操作符-计算其操作数并产生最后一个(最右边的)操作数。

(rots, trans) = readctrls();

同样地,这只赋值给trans, rots将不初始化。

解决方案:您可以返回包含两个指针的结构体,或通过引用传递它们,或其他…

struct Foo {
    double *rots;
    double *trans;
};
Foo readctrls()
{
    // ...
    Foo r;
    r.rots = rots;
    r.trans = trans;
    return r;
}

或:

void readctrls(double *&r, double *&t)
{
    // ...
    r = rots;
    t = trans;
}

其他备注:

  1. 不要使用原始数组。

  2. 在c++中std::vector<T>一般优于T *
  3. 读取整个文件只是为了计算行数,然后再次读取它以实际解析其内容,这是超级浪费。如果您使用std::vector<double>,那么您可以在执行过程中只使用vector.push_back(some_double);,这样您就不必遍历文件两次(您知道,I/O非常昂贵,特别是当文件很大时)。

  4. 你从来没有delete你使用new分配的指针-这里你的程序泄漏内存