如何创建内存泄漏?

时间:2022-11-15 19:56:38

I want to allocate some memory on the heap that is not reachable from any stack pointer. (This is for test purpose).

我想在堆上分配一些无法从任何堆栈指针访问的内存。 (这是出于测试目的)。

void *ptr = malloc(sizeof(int));
void *ptr2 = malloc(sizeof(int));

ptr = ptr2;

If I do this code, I think that ptr and ptr2 at the start is two pointers on the stack referring to some allocated memory on the heap, right? And then when I do the ptr = ptr2, the first mallocated memory is still on the heap but not reachable in any way from the stack. Is that so?

如果我执行此代码,我认为ptr和ptr2在开始时是堆栈上的两个指针,指的是堆上的一些已分配内存,对吧?然后当我执行ptr = ptr2时,第一个mallocated内存仍然在堆上但不能以任何方式从堆栈中到达。是这样吗?

I have a program that is searching the stack to find all alive objects on the heap, therefore I want to test that it actually works.

我有一个程序正在搜索堆栈以查找堆上的所有活动对象,因此我想测试它实际上是否有效。

5 个解决方案

#1


5  

That works. It's more complex than necessary, though:

这样可行。但它比必要的更复杂:

malloc(4);

The easiest way to leak memory is to just not save a reference to it in the first place.

泄漏内存的最简单方法是首先不保存对它的引用。

#2


0  

if you simply put your code into a function, then you have a memory leak as soon as your function ends and ptr and ptr2 get out of scope.

如果您只是将代码放入函数中,那么一旦函数结束并且ptr和ptr2超出范围,就会出现内存泄漏。

#3


0  

Try using:

 malloc(1);

should suffice

#4


0  

A more subtile leak to find would be introduced by doing

通过这样做可以引入更加微妙的泄漏

malloc(0);

though.

#5


0  

It's even simpler to do than that, here is an example from wikipedia:

它比这更简单,这是*的一个例子:

#include <stdlib.h>

void function_which_allocates(void) {
    /* allocate an array of 45 floats */
    float * a = malloc(sizeof(float) * 45);

    /* additional code making use of 'a' */

    /* return to main, having forgotten to free the memory we malloc'd */
}

int main(void) {
    function_which_allocates();

    /* the pointer 'a' no longer exists, and therefore cannot be freed,
     but the memory is still allocated. a leak has occurred. */
}

As soon as the pointer to 'a' goes out of scope, the memory is leaked, as 'a' was never freed.

一旦指向'a'的指针超出范围,内存就会泄漏,因为'a'从未被释放。

#1


5  

That works. It's more complex than necessary, though:

这样可行。但它比必要的更复杂:

malloc(4);

The easiest way to leak memory is to just not save a reference to it in the first place.

泄漏内存的最简单方法是首先不保存对它的引用。

#2


0  

if you simply put your code into a function, then you have a memory leak as soon as your function ends and ptr and ptr2 get out of scope.

如果您只是将代码放入函数中,那么一旦函数结束并且ptr和ptr2超出范围,就会出现内存泄漏。

#3


0  

Try using:

 malloc(1);

should suffice

#4


0  

A more subtile leak to find would be introduced by doing

通过这样做可以引入更加微妙的泄漏

malloc(0);

though.

#5


0  

It's even simpler to do than that, here is an example from wikipedia:

它比这更简单,这是*的一个例子:

#include <stdlib.h>

void function_which_allocates(void) {
    /* allocate an array of 45 floats */
    float * a = malloc(sizeof(float) * 45);

    /* additional code making use of 'a' */

    /* return to main, having forgotten to free the memory we malloc'd */
}

int main(void) {
    function_which_allocates();

    /* the pointer 'a' no longer exists, and therefore cannot be freed,
     but the memory is still allocated. a leak has occurred. */
}

As soon as the pointer to 'a' goes out of scope, the memory is leaked, as 'a' was never freed.

一旦指向'a'的指针超出范围,内存就会泄漏,因为'a'从未被释放。