作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在用 C++ 编写程序,并且使用 SQLite。我的代码:
if (s == SQLITE_ROW) {
int id = 0;
string stem;
id = sqlite3_column_int (selectStmt, 0);
stem = std::string(
reinterpret_cast<const char*>(sqlite3_column_text (selectStmt, 1))
);
if (id > 0)
StemClass *st = new StemClass(id, stem);
row++;
}
sqlite3_column_text 非常慢并且会造成内存泄漏。
最佳答案
为了速度,你可以试试:
auto const p(reinterpret_cast<const char*>(sqlite3_column_text (selectStmt, 1)));
::std::string stem(p, sqlite3_column_bytes(selectStmt, 1));
对于泄漏,使用智能指针,例如::std::shared_ptr
或::std::unique_ptr
。泄漏的可能是您的 new
。以前,您要创建一个空的 ::std::string
实例,然后将一个新的 ::std::string
复制到其中。馊主意。对每个未被智能指针或某些 RAII 方案(例如 SCOPE_EXIT
)捕获的 new
表达式保持怀疑。
关于c++ - sqlite3_column_text 非常慢 (C) 并造成内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18821609/
我是一个使用 Python 和 PyGTK 的 Linux 软件项目的开发人员。我们正在编写的程序依赖于许多第三方包,这些包可通过所有 mayor 发行版存储库获得。其中之一是 python 绑定(b
我是一名优秀的程序员,十分优秀!