gpt4 book ai didi

c++ - sqlite3_column_text 非常慢 (C) 并造成内存泄漏

转载 作者:行者123 更新时间:2023-11-30 04:14:35 26 4
gpt4 key购买 nike

我正在用 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com