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!
.ctors .dtors c++ runtime
Re: .ctors .dtors c++ runtime
The .ctors and .dtors sections contain the constructors and destructors of static objects.
Consider:
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.
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.
Consider:
Code: Select all
#include <MyClass.hpp>
int main()
{
MyClass foo();
foo.do();
return 0;
}
Code: Select all
#include <MyClass.hpp>
static MyClass bar();
int main()
{
bar.do();
return 0;
}
Every good solution is obvious once you've found it.
Re: .ctors .dtors c++ runtime
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.
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.
Re: .ctors .dtors c++ runtime
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.
There's some C++ in there still, but not enough to satisfy.
Every good solution is obvious once you've found it.
-
- Posts: 21
- Joined: Sun Feb 19, 2012 7:25 am
Re: .ctors .dtors c++ runtime
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?
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
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.)hojjatrakhshani wrote:if my class have no constructor and Destructive then .ctors and .dtors must be initialize?
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?indeed what they do(.ctors & .dtors)?
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.)what means of initialize exactly?
Every good solution is obvious once you've found it.