我试图从一个函数获取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)。请在这里帮助我。
我*知道*有*必须*是您要返回'(rots,trans)'(基本上抛出'rots'并刚刚返回'trans')的特定原因。但对于我的生活,我无法理解*为什么*。我猜内存泄漏是奖金?但真正的问题是,用'std :: vector'完全处于你的处置中,为什么你首先要动手分配*任何东西?你可能会感到震惊*有多少代码会使用流提取和标准lib容器消失。 –
WhozCraig
2013-05-11 05:08:38
我更感兴趣的是你为什么做出假设**如果你还不知道语言,那没问题,但是如果你没有阅读RTFM,那根本就不行。 – 2013-05-11 05:15:36
感谢您的输入。我知道我在C++方面的知识不如urs。对不起,你不像你一样熟练。但我在这里尝试,所以我请求帮助。我很抱歉,如果它激怒你如此愚蠢的东西。 @WhozCraig我想要这两个值,所以我回来了,但现在我明白返回不起作用。从中学到了一些东西,谢谢。我将查看向量并了解如何根据它的工作原理修改此代码。再次感谢您的投入。 – 2013-05-11 05:21:10