相关疑难解决方法(0)

内存是否泄漏了C++中的"未定义行为"类问题?

事实证明,许多无辜的东西都是C++中未定义的行为.例如,一旦一个非空的指针已被delete"D 甚至在打印的是指针的值是未定义的行为.

现在内存泄漏肯定是坏事.但他们是什么类的情况 - 定义,未定义或其他类别的行为?

c++ memory-leaks memory-management undefined-behavior

14
推荐指数
5
解决办法
1898
查看次数

为什么不删除在C++ 11中具有带有副作用未定义行为的析构函数的对象?

这个答案引用了C++ 11 Standard 3.8:

如果没有对析构函数的显式调用或者如果没有使用delete-expression(5.3.5)来释放存储,则不应该隐式调用析构函数,并且任何依赖于析构函数产生的副作用的程序都没有被定义行为.

关于析构函数未被调用的部分很清楚.现在假设跳过的析构函数具有应该影响程序行为的副作用.

为什么程序行为现在未定义?为什么不跳过副作用(因为没有调用析构函数)并且程序正常运行而没有应用副作用?

c++ destructor memory-leaks undefined-behavior c++11

9
推荐指数
2
解决办法
338
查看次数

C++在堆和堆栈上分配内存?

来自Java背景,我仍然对在C++中分配内存感到困惑.我很确定前两个陈述是正确的:

void method() {
    Foo foo;    // allocates foo on the stack, and the memory is freed
                // when the method exits
}

void method2() {
    Foo *foo = new Foo();   // allocates foo on the heap
    delete foo;             // frees the memory used by foo
}
Run Code Online (Sandbox Code Playgroud)

但是这样的事情怎么样?

void method3() {
    Foo foo = *new Foo();   // allocates foo on the heap, and then copies it to the stack?
                            // when the method exits, the stack memory is freed, but …
Run Code Online (Sandbox Code Playgroud)

c++ heap stack memory-management

7
推荐指数
1
解决办法
6178
查看次数

是不是在动态分配的对象上调用delete总是内存泄漏?

这里开始的讨论,我想知道以下代码是否有内存泄漏:

int main()
{
   new int();
   //or
   int* x = new int();
   return 0;
}
Run Code Online (Sandbox Code Playgroud)

我知道操作系统会回收内存,但无论如何它都是泄漏?我相信它是.

什么定义了内存泄漏?我只能在标准中找到一个参考,并没有太大帮助.

编辑:我不想开始辩论 - "我认为......"不是我正在寻找的那种答案.我最感兴趣的是 - C++书籍或网站或者其他任何关于它的内容.

c++ memory-leaks memory-management

6
推荐指数
2
解决办法
543
查看次数