Vin*_*nce 1 c++ memory string ode-library
我现在正致力于一个涉及模拟机器人手臂控制的相当复杂的项目.我完成了项目的第一个版本,它工作正常.我刚刚添加了一些新代码,它们在每次迭代时收集有关系统的一些信息,将其保存在某些数组中,最后将所有内容打印在文件中以供以后分析.
现在,真正奇怪的事情正在发生.如果我定义将保存数据的文件如下:
const std::string SAVEFILE = "C:\\Users\\Vincent\\Desktop\\ta";
Run Code Online (Sandbox Code Playgroud)
一切正常,与我添加新代码之前完全相同(加上保存数据).
但如果我将其定义如下:
const std::string SAVEFILE = "C:\\Users\\Vincent\\Desktop\\tacit.txt";
Run Code Online (Sandbox Code Playgroud)
然后系统以另一种方式运行.不会崩溃,但机械臂的移动方式不同.
我试图评论使用SAVEFILE的所有代码,甚至是与数据保存相关的任何新代码,但问题仍然存在.
我知道只有这些信息,任何人都不可能告诉我什么是错的,但有人会有什么建议吗?认为长字符串会覆盖其他变量的值会有意义吗?怎么可能?干净的C++编程的一些准则我可能已经破坏了?
有些阵列是行为不端的声音,这是我检查的第一件事.我想它应该来自保存数据的数组,因为它们是唯一的新数据.事情就是,即使我评论所有相应的代码,也没有变化.
我尝试提供有关我的代码的更多信息.这里我首先使用SAVEFILE(runExperiment函数的最后一个参数)
int main(int argc, char *argv[]) {
std::vector<Controller*> controllers;
controllers.push_back(getConstrainedPDT(0,true));
controllers.push_back(getConstrainedPDT(1,true));
controllers.push_back(getConstrainedPDT(2,true));
runExperiment(controllers,LENGTHS,WEIGHTS,RADIUS,ANGLEMIN,ANGLEMAX,MAXTORQUES,PUSHVECTOR,GRAVITY,RUNTIME,TIMESTEP,XTARGET,YTARGET,ITERATIONSAVEDATA,SAVEFILE);
return 1;
}
Run Code Online (Sandbox Code Playgroud)
这里是函数的代码:
void runExperiment(std::vector<Controller*> controllers,const double * lengths, const double* weights, const double radius, const double* angleMin, const double* angleMax, const double* maxTorques,const double* pushVector,const dReal gravity,const dReal runTime,const dReal tstep,const dReal targetX,const dReal targetY,const int itSaveData,const std::string saveFile){
endTime = runTime;
simTime = 0.0;
timeStep = tstep;
dInitODE();
world = dWorldCreate();
space = dHashSpaceCreate(0);
contactgroup = dJointGroupCreate(0);
ground = dCreatePlane(space, 0, 0, 1, 0);
dWorldSetGravity(world, 0, 0, gravity);
createTargetObject(targetX,targetY);
int nbData = static_cast<int>( ( endTime / timeStep ) / static_cast<double>(itSaveData) );
robot = new R2DRobot(&world,controllers.size(),lengths,weights,radius,angleMin,angleMax,maxTorques,pushVector,controllers,itSaveData,nbData);
dsFunctions fn;
fn.version = DS_VERSION;
fn.start = &setViewPoint;
fn.step = &loop;
fn.stop = &stopSim;
fn.path_to_textures = PATH_TO_TEXTURES;
dsSimulationLoop(0, 0, 1280, 960, &fn);
dWorldDestroy(world);
dCloseODE();
// NOTE: commenting the next three lines does not fix the problem !
// it is the only place saveFile is used, except in the code of printData
// I do not show the code of printData as commenting it does not change anything
if (itSaveData>0){
robot->printData(saveFile);
}
delete robot;
Run Code Online (Sandbox Code Playgroud)
}
希望找到未指定的变量(对于具有大量类的项目而言很容易,其中一些是虚拟的),我使用const参数并观察机器人的行为.我遇到了一个情况:
工作正常:
const std::string SAVEFILE = "C:\\Users\\Vincent\\Desktop\\tacit.txt";
Run Code Online (Sandbox Code Playgroud)
崩溃程序:
const std::string SAVEFILE = "C:\\Users\\Vincent\\Desktop\\ta";
Run Code Online (Sandbox Code Playgroud)
现在的问题是,如果我在runExperiment的代码中添加一行(调用printf添加):
printf("experiment.cpp - 1 \n");
robot = new R2DRobot(&world,controllers.size(),lengths,weights,radius,angleMin,angleMax,maxTorques,pushVector,controllers,itSaveData,nbData);
Run Code Online (Sandbox Code Playgroud)
那么SAVEFILE的两个版本都可以正常工作,并且可以给出完全相似的结果.
现在,如果我删除对printf的调用并添加R2DRobot的构造函数:
R2DRobot::R2DRobot(dWorldID * world, const int nbLinks, const double * lengths, const double * weights, const double radius,const double* angleMin,const double* angleMax,const double* maxTorques,const double* pushVector, std::vector<Controller*> controllers,int saveData,int nbData):
Robot(3*nbLinks+3,controllers),pushVector(pushVector),nbLinks(nbLinks),weights(weights),angleMin(angleMin),angleMax(angleMax),maxTorques(maxTorques),itSaveData(saveData){
printf("experiment.cpp - 1 \n");
// rest of the code
Run Code Online (Sandbox Code Playgroud)
然后程序崩溃(如果使用SAVEFILE的简短版本)但在控制台中打印"experiment.cpp -1"之后.
如果我将对printf的调用移动到Robot的构造函数(R2DRobot的母类),也是一样的.
这可能表明您的程序没有正确初始化变量.当字符串较短时,编译器会创建某些内存布局,并且在堆栈(或堆上)上创建的变量具有某些值.纯粹的运气,这些价值观似乎适合你.
现在,由于字符串变得更长,编译器稍微改变了内存布局,这导致了稍微不同的布局.现在,这些未初始化的变量可能会略有不同.它不一定会崩溃,但工作方式不同.
| 归档时间: |
|
| 查看次数: |
355 次 |
| 最近记录: |