• Advertisement
Sign in to follow this  

What would RAII look like in assembly?

This topic is 2212 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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!!

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
[quote name='Triad_prague' timestamp='1325288543' post='4898291']
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!!
[/quote]

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

Share this post


Link to post
Share on other sites
Using MSVC, this code:


[CODE]
int main()
{
MyClass c( 12.0f );
std::printf("Hello\n");
}[/CODE]

Looks like this in x86 asm:

[CODE]
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
[/CODE]

You can see the call to constructor and destructor at 00CE1276 and 00CE128C

Share this post


Link to post
Share on other sites
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:

[CODE]
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
}
[/CODE]

I wonder where a's destructor will be called since there's a 'return' before 'a' goes out of scope....

Share this post


Link to post
Share on other sites
MSVC will output assembly for you if you use the /FA family of switches. It's been there since at least MSVC 6.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Okay, thanks!! I think I started to get the hang of this [img]http://public.gamedev.net//public/style_emoticons/default/laugh.png[/img]

Share this post


Link to post
Share on other sites
And when you turn optimizations on, mostly all simple constructors and destructors gets inlined and optimized away entirely.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement