Friday, March 26, 2010

new() really is just an alloc and a function call

While reading Eric Lippert’s blog some time ago he mentions that the constructor is just a function like any other function.

This was serendipitously reinforced for me while stepping into a constructor call.  Since Visual Studio ships the CRT source (and it’s been installed), stepping into a constructor call first takes you into new().

I’m pretty sure new’s OK so I immediately step out and find myself executing the constructor.

I guess Eric's right: an object constructor really is just another method.

1 comment :

  1. Yep. Take a look at the generated assembly code some time and it becomes very clear. Typically (on x86) we first do a call to some method that returns a memory block in eax. Then we stuff that into ecx (traditionally used for the "this") and call the constructor. The same is true in C#: http://stackoverflow.com/questions/2559271/stack-heap-understanding-question/2561622#2561622

    ReplyDelete