如果程序退出,C ++库实现如何分配内存但不释放它?(How does a C++ library implementation allocate memory but not free it when the program exits?)
代码很简单:
#include <vector> int main() { std::vector<int> v; }
然后我在Linux上使用Valgrind构建并运行它:
g++ test.cc && valgrind ./a.out ==8511== Memcheck, a memory error detector ... ==8511== HEAP SUMMARY: ==8511== in use at exit: 72,704 bytes in 1 blocks ==8511== total heap usage: 1 allocs, 0 frees, 72,704 bytes allocated ==8511== ==8511== LEAK SUMMARY: ==8511== definitely lost: 0 bytes in 0 blocks ==8511== indirectly lost: 0 bytes in 0 blocks ==8511== possibly lost: 0 bytes in 0 blocks ==8511== still reachable: 72,704 bytes in 1 blocks ==8511== suppressed: 0 bytes in 0 blocks ... ==8511== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
这里没有内存泄漏,即使有1个alloc和0个free。 这个问题的答案在Valgrind的常见问题解答中引用了这一段的解释 -
许多C ++标准库的实现都使用自己的内存池分配器。 很多被破坏对象的内存不会立即被释放并返回给操作系统,而是保存在池中以供以后重复使用。
我的主要问题是:
C ++库实现如何实现? 它是否在后台处理一个单独的进程来处理来自其标准模板的所有分配请求,以便当程序退出时(此处为
a.out
),内存不会立即返回给操作系统? 如果是这样,它什么时候会回馈,如何检查过程确实存在? 如果不是,幕后背后的“魔力”是什么?另一个问题:
分配了71 KB。 为什么这个号码?
谢谢:)
The code is fairly simple:
#include <vector> int main() { std::vector<int> v; }
Then I build and run it with Valgrind on Linux:
g++ test.cc && valgrind ./a.out ==8511== Memcheck, a memory error detector ... ==8511== HEAP SUMMARY: ==8511== in use at exit: 72,704 bytes in 1 blocks ==8511== total heap usage: 1 allocs, 0 frees, 72,704 bytes allocated ==8511== ==8511== LEAK SUMMARY: ==8511== definitely lost: 0 bytes in 0 blocks ==8511== indirectly lost: 0 bytes in 0 blocks ==8511== possibly lost: 0 bytes in 0 blocks ==8511== still reachable: 72,704 bytes in 1 blocks ==8511== suppressed: 0 bytes in 0 blocks ... ==8511== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
Here, there is no memory leak, even though there is 1 alloc and 0 free. The answer to this question quotes this paragraph from Valgrind's FAQ for explanation -
Many implementations of the C++ standard libraries use their own memory pool allocators. Memory for quite a number of destructed objects is not immediately freed and given back to the OS, but kept in the pool(s) for later re-use.
My main question is:
How does the C++ library implementation achieve that? Does it keep around a separate process in the background that handles all allocation requests from its standard templates, so that when the program exits (
a.out
here), the memory is not immediately given back to the OS? If so, when will it give back, and how can I check the process indeed exists? If not, what is the "magic" behind the scene?Another question:
There is 71 KB allocated. Why this number?
Thanks:)
原文:https://stackoverflow.com/questions/45537965
最满意答案
如何将QLineEdit的当前内容添加到下拉菜单(ComboBox)?
假设您有一个具有
QLineEdit
和QComboBox
作为成员的类,并且还有一个连接到updateDropdown()
方法的插槽,如class MainWindow : public QMainWindow { Q_OBJECT; QLineEdit* lineEdit; QComboBox* dropDown; // ... public slots: void updateDropdown(); };
你可以简单地使用
void MainWindow::updateDropdown() { dropDown->addItem(lineEdit->text()); }
How can the current content of the QLineEdit be added to the drop down menu (ComboBox)?
Assumed that you have a class which has the
QLineEdit
and theQComboBox
as members, and which also has a slot which is connected to theupdateDropdown()
method, likeclass MainWindow : public QMainWindow { Q_OBJECT; QLineEdit* lineEdit; QComboBox* dropDown; // ... public slots: void updateDropdown(); };
you can simply use
void MainWindow::updateDropdown() { dropDown->addItem(lineEdit->text()); }
相关问答
更多-
Qt ComboBox-> addItem()整数到Qstring转换错误?(Qt ComboBox->addItem() Integer to Qstring conversion error?)[2022-07-25]
尝试使用combobox->addItem(QString::number(class.value)); Try combobox->addItem(QString::number(class.value)); -
如何将QLineEdit的当前内容添加到下拉菜单(ComboBox)? 假设您有一个具有QLineEdit和QComboBox作为成员的类,并且还有一个连接到updateDropdown()方法的插槽,如 class MainWindow : public QMainWindow { Q_OBJECT; QLineEdit* lineEdit; QComboBox* dropDown; // ... public slots: void updateDropdown() ...
-
如果要在全局函数和MainWindow的全局函数之间消除歧义,可以使用范围解析运算符: ::connect(/* ... arguments go here ... */) 使用:: here告诉编译器查看该函数的全局范围,而不是使用标准名称查找技术,该技术可以首先在类中找到它。 If you want to disambiguate between the global function and the one in MainWindow, you can use the scope resolutio ...
-
您需要做的是,使用DataTable绑定ComboBox,其name为DisplayMember , price为ValueMember 。 这样可以在选择更改时采用相应的值。 让BindingSoure成为从数据库中填充的数据表; 绑定将是: BindingSoure= d.GetQuery("SELECT name,price from Cards").Tables[0] comboBox2.DataSource = BindingSoure; comboBox2.DisplayMember = "na ...
-
Qt:如何连接到SQLite?(Qt: How to connect to SQLite?)[2022-10-11]
得到了答案。 这里是: QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE"); db.setDatabaseName("C:\\sqlite\\mzsales"); bool db_ok = db.open(); 无需连接,只需指定路径,全部完成。 Got the answer. Here it is: QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE"); db.setDatabaseName ... -
如何在qt中的QTableWidget中的comboBox项之后获取行号(how to get row number after comboBox item in QTableWidget in qt)[2021-04-11]
在这种情况下,您不应该使用QComboBox的父级,您必须使用相同的sender() void MainWindow::metto_stringa(int index) { QWidget *w = qobject_cast(sender()); if(w) { int row = ui->tableWidget->indexAt(w->pos()).row(); ui->lineEdit->setText(QString::nu ... -
在Qt 5中,您可以连接到常规成员函数,自由函数或lambda表达式,但您必须使用新的语法 connect(ExampleAction, &QAction::triggered, exfunction); In Qt 5 you can connect to a regular member function, to a free function or a lambda expression, but you must use the new syntax connect(ExampleAction, ...
-
如何将QString类型的项插入Qt中的ComboBox?(How to insert item which type is QString to a ComboBox in Qt?)[2023-05-29]
我没有看到文档的问题。 做就是了 myComboBox->addItem(myString); I don't see the problem with the docs. Just do myComboBox->addItem(myString); -
你应该写 ::connect(...); 要使用正确的功能。 这可确保从全局命名空间发生解析。 You should write ::connect(...); To use the proper function. This ensures that resolution occurs from the global namespace.
-
好。 我找到了解决方案 文档说 设置INCLUDE =%INCLUDE%; c:\ oracle \ oci \ include 设置LIB =%LIB%; c:\ oracle \ oci \ lib \ msvc cd%QTDIR%\ src \ plugins \ sqldrivers \ oci qmake oci.pro NMAKE 如果您不使用Microsoft编译器,请在上面的行中使用make替换nmake。 但是make或nmake对我不起作用。 因为我没有在我的机器上安装Microsoft ...