首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >OpenCV (Cpp接口)-内存管理

OpenCV (Cpp接口)-内存管理
EN

Stack Overflow用户
提问于 2011-10-27 23:28:10
回答 2查看 1.2K关注 0票数 1

我发现OpenCV内存管理非常混乱。我读了这里的文档http://opencv.itseez.com/modules/core/doc/intro.html#automatic-memory-management,但我真的不认为它提供了足够的信息来完全理解它。

例如,考虑以下代码片段

代码语言:javascript
复制
Mat_<float> a,b,c;

a = b; // The header of b is copied into a and they share the data
b = c; // Now b refers to c and a != b
b = a + 1; // b still shares data with c and s.t. b = c;

这有什么意义吗?有人能解释一下它背后的想法吗?

EN

回答 2

Stack Overflow用户

发布于 2011-10-28 00:17:59

您需要与声明矩阵a、b和c分开分配内存。

代码语言:javascript
复制
cv::Mat b(10, 10, CV8U_C1);    //this allocates 10 rows and 10 columns of 8 bit data to matrix b
cv::Mat a;    //This defines a matrix with an empty header.  You *cannot* yet assign data to it - trying to do so will give a segmentation fault
a = b;    //matrix a is now equal to matrix b.  The underlying data (a pointer to 10 x 10 uints) is shared by both so it is a shallow copy (and thus very efficient).  However modifying the data in martix a will now modify the data in matrix b
cv::Mat c(10, 10, CV8U_C1);
b = c;      //This will change matrix b to point to the newly allocated data in matrix c.  Matrix a now has the sole access to its data as matrix b no longer shares it.  Matrix b and c share the same data;
b = a + 1    //This statement makes no sense.  Even if it is valid you should never use it - it is completely unclear what it does
票数 5
EN

Stack Overflow用户

发布于 2011-10-28 13:42:41

要对该问题有一个大致的理解,您必须阅读有关智能指针http://en.wikipedia.org/wiki/Smart_pointer的一些理论。

OpenCV中的许多对象,包括Mat,都是作为智能指针实现的。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7918332

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档