如果我有这个代码:
class A { ... };
class B { ... };
void dummy()
{
A a(...);
B b(...);
...
}
Run Code Online (Sandbox Code Playgroud)
我知道,变量a和b将在反向分配顺序(被破坏b将首先被破坏,则a); 但我可以肯定的是,优化器将永不掉的分配和建设a和b?或者我必须volatile用来执行它?
定义some_class是:
class some_class
{
// stuff
public:
~some_class()
{
delete dynamic_three;
}
private:
classA one;
classB two;
classC* dynamic_three;
}
Run Code Online (Sandbox Code Playgroud)
当一个对象的生命周期结束时,它的破坏是:(1)调用它的析构函数和(2)以类定义(=内存中的位置)声明它们的相同顺序销毁它的子对象.
但是,如果我有类似的东西:
auto* ptr = new some_class();
// more stuff
ptr->~some_class(); // l. X
Run Code Online (Sandbox Code Playgroud)
步骤(2)也实现了吗?我的意思是,在第X行,是子对象的析构函数,也被称为或仅执行some_class析构函数的主体?
是以下代码安全(它在DEBUG中工作):
void takesPointer(const Type* v);//this function does read from v, it doesn't alter v in any way
Type getValue();
...
takesPointer(&getValue());//gives warning while compiling "not an lvalue"
...
Type tmp = getValue();
takesPointer(&tmp);//this is safe, and maybe I should just do it, instead of posting here
Run Code Online (Sandbox Code Playgroud)
所以 - 这样安全吗?我应该忘记它并使用显式tmp的代码吗?
但无论如何 - 如果允许优化器在从此调用返回之前杀死临时值,我仍然感兴趣:
takePointer(&getValue())
Run Code Online (Sandbox Code Playgroud)
编辑:谢谢大家!不幸的是我无法更改函数"takePointer"(它是库的一部分),我只能将它包装在一个函数"takeReference"中,它调用takePointer - 这会消除副本,还是编译器仍然可以创建一个副本("类型"是一个int-3x3-Matrix,所以它不会那么糟糕,但仍然......)?
inline void takesReference(const Type& v){ takesPointer(&v); }
Run Code Online (Sandbox Code Playgroud)
关于破坏的时间:在"takePointer"返回之后,还是在它被调用之后它会被销毁?
自动对象(在堆栈上创建的对象)的销毁是否可以保证在它们超出范围之前执行?
澄清:
#include <iostream>
class A {
public:
A() {
std::cout << "1";
}
~A() {
std::cout << "3";
}
};
void test123() {
A a;
std::cout << "2";
}
Run Code Online (Sandbox Code Playgroud)
要打印"2",a不再需要了,所以从理论上讲,编译器可以a在不再需要时尽快进行优化和销毁.
我可以依靠以上功能始终打印123吗?
using (Stuff1 stf1 = new Stuff1(...)) // Allocation of stf1
using (Stuff2 stf2 = new Stuff2(...)) // Allocation of stf2
{
try
{
// ... do stuff with stf1 and stf2 here ...
}
catch (Stuff1Exception ex1)
{
// ...
}
catch (Stuff2Exception ex2)
{
// ...
}
} // Automatic deterministic destruction through Dispose() for stf1/stf2 - but in which order?
Run Code Online (Sandbox Code Playgroud)
换句话说,是否保证首先调用stf2的Dispose()方法,然后保证stf1的Dispose()方法被调用为秒?(基本上:Dispose()方法是按照它们所属对象的分配顺序调用的吗?)
我有3节课
class A
{
A();
virtual ~A();
}
class B : public A
{
B();
~B();
}
class C
{
void *obj;
C() : obj(nullptr) {}
~C() { if (obj) delete obj; }
}
Run Code Online (Sandbox Code Playgroud)
当我使用class C作为类的任何子类的容器A并尝试删除C实例时.A,B析构函数是不是很正常?什么是solutuon?
C* instance = new C();
instance.obj = new B();
//Magic
delete instance; // A and B destructor is not called
Run Code Online (Sandbox Code Playgroud) 我们考虑以下代码:
#include <iostream>
struct A{ virtual void foo(){ } };
struct B : A { virtual void foo(){ } };
A *a = new B;
int main()
{
delete a; //UB?
}
Run Code Online (Sandbox Code Playgroud)
我故意没有定义虚拟析构函数.编译器打印了一条关于导致UB的消息,是真的吗?
我的代码中有这样的代码:
let [a, b, c, d, e] = await component.getState.call(game.gameId);
Run Code Online (Sandbox Code Playgroud)
变量b,c,e下面使用的代码,但不a和d.同时我有一个标记未使用变量的eslint检查.
有没有办法编写更正确的破坏来解决这个问题?我知道esling-disable-line no-unused但更愿意避免它.
我注意到TOpenPictureDialog的奇怪行为.
在创建和执行TOpenPictureDialog时,会创建13个线程,当对话框被销毁时,根据Windows活动监视器,线程仍然存在,除了1个线程消失.
为什么会这样?
我使用的代码如下:
var opd: TOpenPictureDialog;
begin
opd := TOpenPictureDialog.Create(self);
opd.Execute;
if opd.FileName = '' then exit;
opd.Free;
begin;
Run Code Online (Sandbox Code Playgroud)
我在Windows 8.1中使用Delphi XE2
我仍然不熟悉C ++中的手动销毁(来自具有垃圾回收功能的语言)。我的一门课有以下内容:
Input** Inputs;
初始化如下:
this->Inputs = new Input* [totalInputs];
并可以根据用户输入稍后在我的代码中重新分配,类似于:
this->Inputs[inputNumber] = new DigitalInput(params...)
问题在于,由于释放旧对象,在该位置重新分配对象时,它很容易发生内存泄漏。
重新分配旧对象后,删除旧对象的最佳方法是什么?
编辑:我忘了包括在Arduino代码库上运行的AVR微控制器上。
编辑2:之所以这样做,是因为允许用户将命令发送到将更改输入类型的单元(即:发送命令和this->Inputs[inputNumber] = new AnalogInput(params...)。这也是指向指针数组的指针)的原因。这是因为此对象的构造函数将根据totalInputs传入的参数生成该数组。这在一个共享库中使用,该共享库可在几个不同的单元上使用。
我有一个三个类的类结构,其中两个是第三个类的基类,如下所示:
class A {
};
class B {
};
class C : public A, public B {
};
Run Code Online (Sandbox Code Playgroud)
当的实例C是被破坏,以何种顺序是基类A和B破坏?对此有什么规定吗?
destruction ×11
c++ ×8
pointers ×3
allocation ×1
arduino ×1
c# ×1
class ×1
containers ×1
delphi ×1
delphi-xe2 ×1
dialog ×1
eslint ×1
inheritance ×1
javascript ×1
optimization ×1
scope ×1
temporary ×1
using ×1
volatile ×1