且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

C++:当常驻内存缓慢增加时是否存在内存泄漏?

更新时间:2023-02-12 13:13:12

你没有提到你的平台.在像 Linux 这样的系统上,有一个惰性内存分配器.这意味着如果您调用 new 来分配未初始化的内存(与您的 int 数组一样),则将分配虚拟内存,但不会分配物理内存.稍后,如果您确实为内存分配了一个值,它确实会被分配.

You don't mention your platform. On systems like Linux, there is a lazy memory allocator. This means that if you call new to allocate memory that is not initialized (as with your int array), then virtual memory will be allocated but not physical memory. Later, if you do assign a value to the memory, it does get allocated.

正如 nwp 所说,尝试为您分配的内存分配值(通过使用类似 memset 的东西或使用带有初始化类成员的构造函数的类).

As nwp says, try assigning values to your allocated memory (either by using something like memset or using a class with a constructor that initializes the class members).