2013-05-11 116 views
0

我试图从一个函数获取2个指针并在主打印它。模糊的东西是一个指针似乎已经恢复了它的值,而另一个指针却没有。而且这两个指针在调用函数内部都有正确的值,就在返回之前。请告诉我,您是否可以识别阻止我获得正确答案的程序错误。从函数返回指针。值不更新为一个指针

#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()中。但腐烂正在给出命令的垃圾值(e^-42)。请在这里帮助我。

+2

我*知道*有*必须*是您要返回'(rots,trans)'(基本上抛出'rots'并刚刚返回'trans')的特定原因。但对于我的生活,我无法理解*为什么*。我猜内存泄漏是奖金?但真正的问题是,用'std :: vector '完全处于你的处置中,为什么你首先要动手分配*任何东西?你可能会感到震惊*有多少代码会使用流提取和标准lib容器消失。 – WhozCraig 2013-05-11 05:08:38

+1

我更感兴趣的是你为什么做出假设**如果你还不知道语言,那没问题,但是如果你没有阅读RTFM,那根本就不行。 – 2013-05-11 05:15:36

+0

感谢您的输入。我知道我在C++方面的知识不如urs。对不起,你不像你一样熟练。但我在这里尝试,所以我请求帮助。我很抱歉,如果它激怒你如此愚蠢的东西。 @WhozCraig我想要这两个值,所以我回来了,但现在我明白返回不起作用。从中学到了一些东西,谢谢。我将查看向量并了解如何根据它的工作原理修改此代码。再次感谢您的投入。 – 2013-05-11 05:21:10

回答

3

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. 不要使用原始数组。在C++中,std::vector<T>通常优于T *

  2. 这是超级浪费读取整个文件只是为了计算行数,然后再次阅读它实际解析其内容。如果你使用的是std::vector<double>,那么你可以只用vector.push_back(some_double);,这样你就不必两次浏览文件(你知道,I/O是昂贵的,尤其是如果文件很大的话)。

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

+0

非常感谢这个答案!从这里得到了很多输入。感谢您的耐心。 – 2013-05-11 05:27:54