Page 1 of 1
.ctors .dtors c++ runtime
Posted: Thu May 10, 2012 10:52 pm
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!
Re: .ctors .dtors c++ runtime
Posted: Fri May 11, 2012 1:06 am
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.
Re: .ctors .dtors c++ runtime
Posted: Fri May 11, 2012 2:12 am
by Solar
Pffffff...
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.
Re: .ctors .dtors c++ runtime
Posted: Fri May 11, 2012 6:52 am
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.
Re: .ctors .dtors c++ runtime
Posted: Fri May 11, 2012 8:36 am
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?
Re: .ctors .dtors c++ runtime
Posted: Fri May 11, 2012 8:42 am
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.)