Hi, I know that when a variable comes into existence its constructor will be called, and its destructor will be called upon exiting scope, I just wonder what those processes look like in assembly? I've been thinking like crazy!!
What would RAII look like in assembly?
Started by Triad_prague, Dec 30 2011 05:42 PM
8 replies to this topic
Sponsor:
#2 Members - Reputation: 768
Posted 30 December 2011 - 05:47 PM
Do you mean how does the assembly look like that the compiler produces? Then you can simply look at it since pretty much any compiler can output assembly.
Or do you mean what it looks like when you are actually writing assembly? In that case there is the issue that assembly doesn't really know types to begin with, also there are no constructors in the "language", whether you initialise data or not is up to you. You just put "stuff" into registers and the instructions you use it with decide how it is interpreted.
Or do you mean what it looks like when you are actually writing assembly? In that case there is the issue that assembly doesn't really know types to begin with, also there are no constructors in the "language", whether you initialise data or not is up to you. You just put "stuff" into registers and the instructions you use it with decide how it is interpreted.
#3 Members - Reputation: 999
Posted 30 December 2011 - 06:02 PM
Hi, I know that when a variable comes into existence its constructor will be called, and its destructor will be called upon exiting scope, I just wonder what those processes look like in assembly? I've been thinking like crazy!!
It looks like a 'call' or 'branch' instruction to call the constructor at the beginning of scope, and another call to the destructor at the end of the scope. They're just functions.
Of course, there may be optimizations at play that inline the constructor or destructor, so you might not see an actual call/branch/whatever instruction
#4 Members - Reputation: 999
Posted 30 December 2011 - 06:08 PM
Using MSVC, this code:
Looks like this in x86 asm:
You can see the call to constructor and destructor at 00CE1276 and 00CE128C
int main()
{
MyClass c( 12.0f );
std::printf("Hello\n");
}Looks like this in x86 asm:
00CE1260 push ebp 00CE1261 mov ebp,esp 00CE1263 sub esp,44h 00CE1266 push ebx 00CE1267 push esi 00CE1268 push edi 00CE1269 push ecx 00CE126A fld dword ptr [__real@41400000 (0CE4838h)] 00CE1270 fstp dword ptr [esp] 00CE1273 lea ecx,[c] 00CE1276 call MyClass::MyClass (0CE10C3h) 00CE127B push offset string "Hello\n" (0CE4830h) 00CE1280 call dword ptr [__imp__printf (0CE7238h)] 00CE1286 add esp,4 00CE1289 lea ecx,[c] 00CE128C call MyClass::~MyClass (0CE100Fh) 00CE1291 xor eax,eax 00CE1293 pop edi 00CE1294 pop esi 00CE1295 pop ebx 00CE1296 mov esp,ebp 00CE1298 pop ebp 00CE1299 ret
You can see the call to constructor and destructor at 00CE1276 and 00CE128C
#5 Members - Reputation: 129
Posted 30 December 2011 - 06:34 PM
Whoa! btw rdragon1, what tools do you use to generate assembly code from C++ code? I mean what version of MSVC? that's cool. Btw thanks for the response tho. I really need to know what it will look like if the code is like this:
I wonder where a's destructor will be called since there's a 'return' before 'a' goes out of scope....
void test()
{
ClassA a; //a's ctor called
if(1)
{
ClassB b; //b's ctor called
return;
}
//after this a's dtor will be called
}
I wonder where a's destructor will be called since there's a 'return' before 'a' goes out of scope....
the hardest part is the beginning...
#7 Members - Reputation: 999
Posted 30 December 2011 - 07:05 PM
I just ran the program in the debugger and switched to the disassembly window.
For your other question, the destructor will be called whenever the object goes out of scope, so the compiler will add another call to the destructor just before that other return.
For your other question, the destructor will be called whenever the object goes out of scope, so the compiler will add another call to the destructor just before that other return.






