我对使用有疑问std::vector.如果我std::vector在内存中得到一个带有非定义空间的对象的结构 ,如果需要重新分配(std::vector),结构是否也重新分配了?还是只有std::vector?
例如:
struct cluster{
int a;
int b;
struct cluster* parent;
vector<struct cluster> children;
}
struct cluster obj = {2,1,...};
struct cluster *pobj = &obj;
for(int i = 0; i < 100 ; i ++){
children.push_back(i); //The capacity will be increased progressly, no?
}
Run Code Online (Sandbox Code Playgroud)
所以,问题是:是pobj==&obj在for循环结束?或者obj由于子std::vector对象的重新分配而重新分配?
我希望我解释了我的怀疑.谢谢你的时间.
我疯狂地理解valgrind的错误.我有一个名为Matrix的模板类,它有一些重载的运算符等......来做一些数学运算.Matrixes在名为ExtendedKalmanFilter的类中使用.
这是valgrind跟踪:
==3352== Invalid read of size 8
==3352== at 0x804CC8F: BOViL::math::Matrix<double>::operator*(BOViL::math::Matrix<double> const&) const (Matrix.h:285)
==3352== by 0x8051F91: BOViL::algorithms::ExtendedKalmanFilter::forecastStep(double) (ExtendedKalmanFilter.cpp:48)
==3352== by 0x8051F25: BOViL::algorithms::ExtendedKalmanFilter::stepEKF(BOViL::math::Matrix<double> const&, double) (ExtendedKalmanFilter.cpp:39)
==3352== by 0x804B98F: testSegmentation() (TestSegmentation.cpp:53)
==3352== by 0x805266D: main (main.cpp:16)
==3352== Address 0x6a8b3c0 is 0 bytes after a block of size 48 alloc'd
==3352== at 0x402B454: operator new[](unsigned int) (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==3352== by 0x804C986: BOViL::math::Matrix<double>::operator=(BOViL::math::Matrix<double> const&) (Matrix.h:224)
==3352== by 0x8051C62: BOViL::algorithms::ExtendedKalmanFilter::setUpEKF(BOViL::math::Matrix<double>, BOViL::math::Matrix<double>, BOViL::math::Matrix<double>) (ExtendedKalmanFilter.cpp:23)
==3352== by 0x804B74F: testSegmentation() (TestSegmentation.cpp:37)
==3352== by 0x805266D: main …Run Code Online (Sandbox Code Playgroud) 我只是一个问题.我一直在寻找答案,但我可能找不到正确的标签或单词来找到解决方案......
问题是,如标题所示:是否需要释放局部变量(具有保留内存)?我的意思是,例如,如果我有以下功能:
myClass* myFunction(){
myClass* A = new myClass;
return A;
}
Run Code Online (Sandbox Code Playgroud)
如果是"是",我应该在哪里打电话给"删除"?"回归"之前和之后都是无稽之谈......
我对吗?
我正在尝试在不使用其余ROS的情况下编译roscpp(我只需要订阅一个节点,但是拥有一个使用旧版本ROS的节点,由于编译麻烦,我无法将其与其程序集成在一起)。我从git(https://github.com/ros/ros_comm)下载了源代码,现在我需要对其进行编译,但是Cmake抛出了错误:
INFOBuilding GTest from source.
TODO: implement add_roslaunch_check() in rostest-extras.cmake.
CMake Error at CMakeLists.txt:8 (catkin_package_xml):
Unknown CMake command "catkin_package_xml".
Run Code Online (Sandbox Code Playgroud)
我该如何建造?我正在调用cmake CMakeList.txt,但是它不起作用。