Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/156.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如果std::vector是通过值返回的,那么何时释放它? P>我很难理解C++ STD::vector容器如何在下面的脚本中被释放 void DoSomething() { cv::Point ** curves; int * curve_sizes; num_curves = m_curves.size(); //member variable holdings curves curves = new cv::Point*[num_curves]; curve_size = new int[num_curves]; std::vector<cv::Point> cur_points; for(int i = 0; i < num_curves; ++i) { cur_points = CreatePolyPoints(m_curves[i]); curves[i] = &cur_points[0]; curve_sizes = cur_points.size(); } cv::fillPoly(m_roi, curves, curve_sizes, num_curves, ... ); //Clear the dynamic data // Do i do aything here? delete [] curves; delete [] curve_sizes; } std::vector<cv::Point> CreatePolyPoints(Curve curve) { std::vector<cv::Point> points; //Do work here here while(something) { cv::Point cur_point; points.push_back(cur_point); } return points; } void DoSomething() { cv::点**曲线; int*曲线大小; num_curves=m_curves.size();//成员变量持有曲线 曲线=新cv::点*[num_曲线]; 曲线大小=新整数[num_曲线]; std::向量cur_点; 对于(int i=0;i_C++_Opencv_Stl - Fatal编程技术网

如果std::vector是通过值返回的,那么何时释放它? P>我很难理解C++ STD::vector容器如何在下面的脚本中被释放 void DoSomething() { cv::Point ** curves; int * curve_sizes; num_curves = m_curves.size(); //member variable holdings curves curves = new cv::Point*[num_curves]; curve_size = new int[num_curves]; std::vector<cv::Point> cur_points; for(int i = 0; i < num_curves; ++i) { cur_points = CreatePolyPoints(m_curves[i]); curves[i] = &cur_points[0]; curve_sizes = cur_points.size(); } cv::fillPoly(m_roi, curves, curve_sizes, num_curves, ... ); //Clear the dynamic data // Do i do aything here? delete [] curves; delete [] curve_sizes; } std::vector<cv::Point> CreatePolyPoints(Curve curve) { std::vector<cv::Point> points; //Do work here here while(something) { cv::Point cur_point; points.push_back(cur_point); } return points; } void DoSomething() { cv::点**曲线; int*曲线大小; num_curves=m_curves.size();//成员变量持有曲线 曲线=新cv::点*[num_曲线]; 曲线大小=新整数[num_曲线]; std::向量cur_点; 对于(int i=0;i

如果std::vector是通过值返回的,那么何时释放它? P>我很难理解C++ STD::vector容器如何在下面的脚本中被释放 void DoSomething() { cv::Point ** curves; int * curve_sizes; num_curves = m_curves.size(); //member variable holdings curves curves = new cv::Point*[num_curves]; curve_size = new int[num_curves]; std::vector<cv::Point> cur_points; for(int i = 0; i < num_curves; ++i) { cur_points = CreatePolyPoints(m_curves[i]); curves[i] = &cur_points[0]; curve_sizes = cur_points.size(); } cv::fillPoly(m_roi, curves, curve_sizes, num_curves, ... ); //Clear the dynamic data // Do i do aything here? delete [] curves; delete [] curve_sizes; } std::vector<cv::Point> CreatePolyPoints(Curve curve) { std::vector<cv::Point> points; //Do work here here while(something) { cv::Point cur_point; points.push_back(cur_point); } return points; } void DoSomething() { cv::点**曲线; int*曲线大小; num_curves=m_curves.size();//成员变量持有曲线 曲线=新cv::点*[num_曲线]; 曲线大小=新整数[num_曲线]; std::向量cur_点; 对于(int i=0;i,c++,opencv,stl,C++,Opencv,Stl,提前谢谢。如果有人对我的目标感兴趣:生成一个由“n”曲线定义的给定多边形的ROI。它不是动态分配的,因此一旦超出范围就会被销毁。在您的情况下,DoSomething()函数退出后,您的变量将超出范围。局部变量cur_points获取循环中分配给它的CreatePolyPoints()的返回值。发生这种情况时,cur_points的旧内容将不再存在-您不必担心这一点,因为每次重新分配向量时,该向量中的Curve对象的实例将被销毁。类似地,当cur_points超出范围时 编辑:你看起来确实有问题

提前谢谢。如果有人对我的目标感兴趣:生成一个由“n”曲线定义的给定多边形的ROI。

它不是动态分配的,因此一旦超出范围就会被销毁。在您的情况下,
DoSomething()
函数退出后,您的变量将超出范围。局部变量
cur_points
获取循环中分配给它的
CreatePolyPoints()
的返回值。发生这种情况时,
cur_points
的旧内容将不再存在-您不必担心这一点,因为每次重新分配向量时,该向量中的
Curve
对象的实例将被销毁。类似地,当
cur_points
超出范围时

编辑:你看起来确实有问题

std::vector<cv::Point> cur_points;
for(int i = 0; i < num_curves; ++i)
{
   cur_points = CreatePolyPoints(m_curves[i]);
   curves[i] = &cur_points[0];    // <-- problem
   curve_sizes = cur_points.size();
}
std::矢量电流点;
对于(int i=0;i曲线[i]=&cur_points[0];//它不是动态分配的,因此一旦超出范围就会被销毁。在您的情况下,您的变量将在
DoSomething()
函数退出后超出范围。您的局部变量
cur_points
获取
CreatePolyPoints()的返回值
在循环中分配给它。发生这种情况时,
cur_points
的旧内容将不再存在-您不必担心这一点,因为该向量中的
Curve
对象的实例将在每次重新分配向量时被销毁。同样,当
cur_points
超出范围时

编辑:你看起来确实有问题

std::vector<cv::Point> cur_points;
for(int i = 0; i < num_curves; ++i)
{
   cur_points = CreatePolyPoints(m_curves[i]);
   curves[i] = &cur_points[0];    // <-- problem
   curve_sizes = cur_points.size();
}
std::矢量电流点;
对于(int i=0;i曲线[i]=&cur_points[0];/
cur_points
是一个具有自动存储持续时间的变量。在声明时,它即存在,如下所示:

std::vector<cv::Point> cur_points;
std::矢量电流点;
…并且只要它的封闭范围持续存在,它就会持续存在

由于本例中的“封闭范围”是函数
void DoSomething()
本身,
cur\u points
将超出范围,并在函数返回时(自动)销毁

编辑现在,以上是简单的答案。完整的答案实际上有点复杂,因为还涉及临时人员

函数
CreatePolyPoints
按值返回此
向量。严格遵守此函数的语义和返回值意味着当
CreatePolyPoints
返回时,在该函数中本地创建的
对象将被销毁,并将创建一个新的临时
向量
构造。该临时值是函数实际返回的值。然后将该临时值复制到
cur_points
变量(通过copy assignemnt),然后销毁该临时值

这是一个浪费很多的复制,但故事还有一点。C++标准也有一个规则(称为“AS-IF”规则)。这表明编译器可以以它认为合适的任何方式更改代码,只要程序的行为与编译器在考虑副作用的情况下没有做任何更改一样


上面的意思是,编译器有一个主要的优化机会,可以在许多情况下消除所有这些临时变量,包括本例,方法是将返回值直接复制到
cur_points
变量,而不是通过临时变量。这种最一般形式的优化称为“返回值优化”,以及most(all?)现代编译器经常这样做。当这种情况发生时(这里通常会发生),其行为与我在此处编辑之前所做的完全相同。

cur\u points
是一个具有自动存储持续时间的变量。它在声明时即存在,如下所示:

std::vector<cv::Point> cur_points;
std::矢量电流点;
…并且只要它的封闭范围持续存在,它就会持续存在

由于本例中的“封闭范围”是函数
void DoSomething()
本身,
cur\u points
将超出范围,并在函数返回时(自动)销毁

编辑现在,以上是简单的答案。完整的答案实际上有点复杂,因为还涉及临时人员

函数
CreatePolyPoints
按值返回此
向量。严格遵守此函数的语义和返回值意味着当
CreatePolyPoints
返回时,在该函数中本地创建的
对象将被销毁,并将创建一个新的临时
向量
构造。该临时值是函数实际返回的值。然后将该临时值复制到
cur_points
变量(通过copy assignemnt),然后销毁该临时值

这是一个l