在 Visual Studio 中模拟简单的内存泄漏,但结果有点令人困惑

发布于 2024-12-12 21:42:51 字数 290 浏览 5 评论 0原文

这是一个非常简单的问题(或者至少乍一看)。假设我们有以下过程:

void procedure(void)
{
          int x = new int;
          x=42;
}

当我在程序中调用这个过程时,它应该会导致所谓的内存泄漏,对吗?我遇到过这样的情况在处理一个小项目时出现错误,然后编译器输出类似以下内容:检测到内存泄漏。转储内存块...现在我尝试模拟内存泄漏并查看编译器输出什么,但它根本不抱怨(我使用 Visual工作室)。
我的问题是真的存在内存泄漏吗?

Here is one very simple question(or at least at first sight).Let's say we have the following procedure:

void procedure(void)
{
          int x = new int;
          x=42;
}

When I call to this procedure in my program it should lead to the so called memory leak, right?I've been encountering such bugs while working on a small project and then compiler was outputting something like : detected memory leak.Dumping memory block ...Now I try to simulate a memory leak and see what compiler outputs but it doesn't complain at all(I use Visual Studio).
Is there really a memory leak that's my question .

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

苍白女子 2024-12-19 21:42:55

是的,这是内存泄漏。您使用 C++ 并调用 new 运算符将在堆上保留 4 个字节的内存。 - 不调用删除。
如果您将此函数调用 200 万次,大约会泄漏 8 MB 的内存。

在这个简短的程序部分中 - 它分配内存并将指针转换为 int (隐藏转换)。有些编译器可能无法检测到这种情况,而另一些编译器可能会。

Yes it is memory leak. You use C++ and calling new operator will reserve 4 bytes of memory on the heap. - delete is not called.
If you will cal this function 2 million times, about 8 MB of memory will leak.

In this short program part - it allocates memory and pointer is converted to int (hidden conversion). Some compilers may not detect tis situation, another may.

谁的年少不轻狂 2024-12-19 21:42:54

您将无法调用它,因为它无法编译。想必您的真正意思是:

void procedure(void) { 
    int *x = new int;
    *x = 42;
}

是的,这有一个内存泄漏,我希望 VC++ 会抱怨(在调试模式下编译时)。在发布模式下,它通常不会检查泄漏,但即使这样做,如果编译器检测到这些都没有任何实际效果,因此调用它被完全优化(从而消除了内存泄漏)。我完全不确定这是否会发生,但如果确实如此,也不会令人感到惊讶。

You won't be able to call this, because it won't compile. Presumably you really meant something like:

void procedure(void) { 
    int *x = new int;
    *x = 42;
}

Yes, this has a memory leak which I'd expect VC++ to complain about (when compiled in debug mode). In release mode, it won't normally check for leaks anyway, but even if it did, it wouldn't surprise me if the compiler detected that none of this had any real effect, so invoking it was optimized out completely (thus eliminating the memory leak). I'm not at all sure that would happen, but it wouldn't be a huge surprise if it did either.

清晰传感 2024-12-19 21:42:54

你也做错了编辑我:)我需要先学会阅读

你可能想要的:

void procedure(void)
{
          int* px = new int;
          *px = 42;
}

它分配 42 到x指向整数

,并且确实是一个(小)内存泄露。如果该函数被调用多次,则会出现更大的内存泄漏:)

You're doing it wrong Edit me too :) I need to learn to read first

you probably wanted:

void procedure(void)
{
          int* px = new int;
          *px = 42;
}

Which assigns the value 42 to the integer pointed to by x

And yes that really is a (small) memory leak. If the function gets called many times, you'll have a bigger memory leak :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文