如何在C ++中创建内存泄漏? [英] How to create a memory leak in C++?

查看:132
本文介绍了如何在C ++中创建内存泄漏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想知道如何使用C ++创建系统内存泄漏。我已经做了一些谷歌搜索,但没有太多出现,我知道,这是不是真的可行的在C#,因为它是托管代码,但想知道是否有一个简单的方法来做这个与C + +?我只是认为这将是有趣的,看看系统遭受多少,因为代码没有正确写。感谢。

I was just wondering how you could create a system memory leak using C++. I have done some googling on this but not much came up, I am aware that it is not really feasible to do it in C# as it is managed code but wondered if there was a simple way to do this with C++? I just thought it would be interesting to see how much the system suffers because of code not being written properly. Thanks.

推荐答案

调用 new 稍后再对应 delete 。如示例代码所示:

A memory leak occurs when you call new without calling a corresponding delete later. As illustrated in this sample code:

int main() {
    // OK
    int * p = new int;
    delete p; 

    // Memory leak
    int * q = new int;
    // no delete
}

这篇关于如何在C ++中创建内存泄漏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆