.ctors .dtors c++ runtime

Programming, for all ages and all languages.
Post Reply
hojjatrakhshani
Posts: 21
Joined: Sun Feb 19, 2012 7:25 am

.ctors .dtors c++ runtime

Post by hojjatrakhshani »

hi
i have :?:
why we must initialize .ctors and .dtors?what they have,pointers?pointer to what?
if they not run what happen?for example if we have below variable
//global
int x;
///
what happen when .ctors and .dtors run?
thank very much!
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: .ctors .dtors c++ runtime

Post by Solar »

The .ctors and .dtors sections contain the constructors and destructors of static objects.

Consider:

Code: Select all

#include <MyClass.hpp>

int main()
{
    MyClass foo();
    foo.do();
    return 0;
}
In this example, an object of type "MyClass" named "foo" is created by running the "MyClass" constructor. After that, the member function do() is executed.

Code: Select all

#include <MyClass.hpp>

static MyClass bar();

int main()
{
    bar.do();
    return 0;
}
This example does exactly the same, but the "MyClass" constructor is executed before main() is entered. Since this obviously cannot be done by the program itself - which is defined to start with main() - it has to be done by the runtime environment. In order to tell the runtime which constructors (and, eventually, destructors) have to be run, the compiler creates the .ctor and .dtor sections, with pointers to the constructors / destructors to be executed. If they were not run, "bar.do()" would end in desaster, because there would be no "bar" to begin with.
Every good solution is obvious once you've found it.
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: .ctors .dtors c++ runtime

Post by Solar »

Pffffff...

:oops: :oops: :oops: :oops:

I've got to talk to my boss. All this script and maintenance work at the office makes my brain all mushy, so that I make stupid, really stupid mistakes like this one... you are right, of course.
Every good solution is obvious once you've found it.
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: .ctors .dtors c++ runtime

Post by Solar »

Nah, got to tweak these bash scripts so they configure the build process correctly for MSVC 10 on Win7 64bit using up-to-date libraries, while not breaking the build for MSVC 8 on WinXP 32bit using horribly outdated libs. Tossing in some upstream bug reports for Boost and Xerces-C while we're at it. After that, write a Tcl script to read the job definitions from the Oracle RDB and turn them into a PDF representation of job dependencies, using graphviz. After that... you get the idea.

There's some C++ in there still, but not enough to satisfy. :cry:
Every good solution is obvious once you've found it.
hojjatrakhshani
Posts: 21
Joined: Sun Feb 19, 2012 7:25 am

Re: .ctors .dtors c++ runtime

Post by hojjatrakhshani »

thank for your useful help!
if my class have no constructor and Destructive then .ctors and .dtors must be initialize?
indeed what they do(.ctors & .dtors)?what means of initialize exactly?
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: .ctors .dtors c++ runtime

Post by Solar »

hojjatrakhshani wrote:if my class have no constructor and Destructive then .ctors and .dtors must be initialize?
If you have static objects of that class type, then yes. (The compiler generates "default" constructors / destructors in this case, which should still be called.)
indeed what they do(.ctors & .dtors)?
Now we come to the point where lack of language precision becomes a problem. Are you asking what .ctors (a section in a binary file) does, or what a constructor (an element of object-oriented programming languages) does?
what means of initialize exactly?
And now we come to the point where I suspect you would be better off asking these questions in a forum primarily aimed at programming beginner questions (like stackoverflow.com). You should know very precisely what "initialization" means, if you want to follow the discussions on this particular board, which is aimed at rather advanced programming topics. Anyway, you shouldn't worry about issues of the runtime yet. (Don't try to run before you can walk.)
Every good solution is obvious once you've found it.
Post Reply