- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是一名 C++ 初级程序员。最近,我开始使用 C++ 进行图像处理。我正在尝试定义和使用以下函数:
Matrix MVE(Matrix R, double tolerance)
{
int n = R.Y();
Matrix P(n,4);
for (int i = 0; i < n; ++i)
{
P[i][0] = R[i][0];
P[i][1] = R[i][1];
P[i][2] = R[i][2];
P[i][3] = 1.0;
}
Matrix *Q = P.T();
double err = 1.0;
MatVectorCol u(n);
u.Set(1.0 / n);
MatVectorCol Mv(n);
while (err > tolerance)
{
Matrix uM(n, n);
uM.SetDiagonal(u);
Matrix *X = *Q * uM;
Matrix *X1 = *X * P;
Matrix invX(4, 4);
invX = *X1->Inverse();
delete X; // Error here!
delete X1;
}
return invX;
}
但是当我的程序执行“delete X;”这一行时我得到了这个错误:
Windows 已在 plappd.exe 中触发断点。
这可能是由于堆损坏,这表明 plappd.exe 或它加载的任何 DLL 中存在错误。
这也可能是由于用户在 plappd.exe 具有焦点时按了 F12。
Matrix 类的构造函数和操作定义如下:
Matrix::Matrix(int _y, int _x)
{
x = _x;
y = _y;
M = new double [x * y];
buff = new double [x];
memset0_64(M, sizeof(double) * (x * y));
}
Matrix *Matrix::Transpose()
{
Matrix *b = new Matrix(x, y);
for (int i = 0; i < x; ++i)
{
double *b_line_i = (*b)[i];
for (int j = 0; j < y; ++j)
b_line_i[j] = (*this)[j][i];
}
return b;
}
Matrix *Matrix::operator * (const Matrix &m)
{
if (x == m.y)
{
Matrix *b = new Matrix(y, m.x);
for (int i = 0; i < y; ++i)
{
double *b_line_i = (*b)[i];
double *line_i = (*this)[i];
for (int j = 0; j < m.x; ++j)
for (int k = 0; k < x; ++k)
b_line_i[j] += line_i[k] * m(k, j);
}
return b;
}
else
return 0;
}
当然,代码有问题,但我不知道它在哪里以及如何解决它。
提前致谢。
最佳答案
您的代码的错误之处在于您过度使用/滥用了 operator new 和动态内存分配。除非必须,否则不要动态分配内存——这很容易出错,如果您是“初学者”,您绝对应该避免这样做。 (除非您使用另一种语言使用动态分配的内存并且知道您在做什么)。
而不是用 new
分配 double , 使用 std::vector<double>
或类似的容器。这样您就不必编写析构函数。
如果你的类动态分配内存,它必须有析构函数。 C++ 没有垃圾收集,因此没有人会为您清理烂摊子和释放资源。
如果你的类有析构函数,它应该服从rule of three .您应该定义复制构造函数和赋值运算符。如果不这样做,将导致内存泄漏、未定义的行为和崩溃。
不要从运算符中返回原始指针。不要从运算符返回动态分配的对象(如果必须返回动态分配的对象,请创建单独的函数)。如果您必须返回动态分配的对象,请使用智能指针,这样它们的销毁将是自动的(std::shared_ptr
或 boost::shared_ptr
应该有效)。
传统上,operator*
应该按值返回对象。它应该看起来像这样:
Matrix Matrix::operator* (const Matrix &arg1, const Matrix &arg2){...}|
关于c++ - C++中堆的损坏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18983388/
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 8 年前。 Improve this qu
我目前正在尝试制作一个非常简单的应用程序,它会根据一天中的时间问候。我的代码是: open System let read() = Console.Read() let readLine() = Co
我已经运行Elasticsearch服务很长时间了,但是突然遇到了以下情况 由以下原因导致:org.elasticsearch.index.translog.TranslogCorruptedExce
我对执行以下操作的 php 重定向脚本有一个奇怪的问题: 在用户的浏览器中植入 Cookie,或者读取现有 Cookie(如果有)。 将用户重定向到另一个网址(重定向的网址是原始网址中的参数,例如 h
我正在使用 iText 7.0.0(Java 风格),似乎表格单元格 HorizontalAlignment 被忽略,因为 CENTER 和 RIGHT 都不起作用。你能重现这个吗? see th
简而言之: 我有一个可以从多个线程访问的计数器变量。尽管我已经实现了多线程读/写保护,但该变量似乎仍然以不一致的方式同时写入,导致计数器结果不正确。 深入杂草: 我使用的“for 循环”会在后台触发大
我有一个 REST 项目,在访问控制服务类中保存用户的ArrayList。一切都工作正常,直到 REST Web 服务突然抛出 java.util.NoSuchElementException。单步查
已关闭。此问题不符合Stack Overflow guidelines 。它目前不接受答案。 这个问题似乎不是关于 a specific programming problem, a software
当我刷新页面时,我无法显示 voteUp/Down,因为如果我执行 voteUp/Down(+1 或 -1) 并刷新页面,这会再次返回 voteUp/Down (0)。过去我使用 JSON,但社区推荐
我正在为离散时间 CPU 调度模拟器编写代码。它只是生成流程并相应地安排它们。我目前正在实现 FCFS 计划。我理解离散时间模拟器的本质,但我在用 C++ 实现时遇到了麻烦。 问题出现在handleN
尝试使用 yum 部署包时出现错误: 2016-07-07 14:14:31,296 - ERROR - error: rpmdb: BDB0113 Thread/process 6723/1
我有一个简单的同步队列 template class SynchronisedQueue { public: void Enqueue(const T& d
我正在使用 hadoop 0.20.append 和 hbase 0.90.0。我将少量数据上传到 Hbase,然后出于评估目的杀死了 HMaster 和 Namenode。在此之后,我向 Hbase
我使用 symfony 框架 1.4 创建了一个网站。我正在使用 sfguard 进行身份验证。 现在,这在 WAMP (windows) 上运行良好。我可以在不同的浏览器上登录多个帐户并使用该网站。
目前我已经实现了 HashMap private static Map cached = new HashMap(); 和 Item 是一个具有属性的对象 Date expireTime 和 byte
我试图将 2 个不同的 WPF 控件绑定(bind)到 ViewModel 中的同一属性,即 CheckBox.IsChecked 和 Expander.IsExpanded。我想要实现的行为是让 C
我希望这是一个简单的问题,但我没有找到答案。 我想让 build.gradle 文件通过替换某些变量来设置我的 Spring Boot 应用程序中的版本。这与广告一样有效: def tokens =
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
这个问题在这里已经有了答案: In a fragment shader, why can't I use a flat input integer to index a uniform array o
我已经下载了 OSM 世界地图。解析时出现异常: osm bound changeset (...) changeset Exception in thread "main" org.xml.sax.
我是一名优秀的程序员,十分优秀!