Rabu, 23 September 2009

Konstruktor dan Destructors global dalam C + +

Global constructors and destructors in C++ have to be handled very carefully to meet the language specification. Constructors have to be called before the main function. Global konstruktor dan destructors di C + + harus ditangani sangat hati-hati untuk memenuhi spesifikasi bahasa. Konstruktor harus dipanggil sebelum fungsi utama. Destructors have to be executed after it returns. Under ELF, this can be treated gracefully by the compiler. Destructors harus dieksekusi setelah kembali. Di bawah ELF, hal ini dapat diobati dengan anggun oleh kompilator. For example, the GNU C/C++ compiler, gcc , provides two auxiliary start up files called crtbegin.o and crtend.o , in addition to two normal auxiliary files crti.o and crtn.o . Together with the .ctors and .dtors sections described below, the C++ global constructors and destructors can be executed in the proper order with minimal run-time overhead. Sebagai contoh, GNU C / C + + compiler, gcc, menyediakan dua bantu start up file bernama crtbegin.o dan crtend.o, selain dua file bantu normal crti.o dan crtn.o. Bersama-sama dengan. Ctors dan. Dtors bagian yang dijelaskan di bawah ini, C + + global destructors konstruktor dan dapat dilaksanakan dalam urutan yang tepat dengan sedikit run-time overhead.
.ctors . ctors

This section holds an array of the global constructor function pointers of a program. Bagian ini memegang sebuah array dari fungsi constructor global pointer dari sebuah program.
.dtors . dtors

This section holds an array of the global destructor function pointers of a program. Bagian ini memegang sebuah array dari fungsi destructor global pointer dari sebuah program.
crtbegin.o crtbegin.o

There are four sections: Ada empat bagian:
• The .ctors section. The. Ctors bagian. It has a local symbol, __CTOR_LIST__ , which is the head of the global constructor function pointer array. Memiliki simbol lokal, __CTOR_LIST__ yang adalah kepala fungsi constructor global pointer array. This array in crtbegin.o only has one dummy element. Array dalam crtbegin.o ini hanya memiliki satu elemen dummy.
• The .dtors section. The. Dtors bagian. It has a local symbol, __DTOR_LIST__ , which is the head of the global destructor function pointer array. Memiliki simbol lokal, __DTOR_LIST__ yang adalah kepala dari fungsi destructor global pointer array. This array in crtbegin.o only has only one dummy element. Array dalam crtbegin.o ini hanya memiliki hanya satu elemen dummy.
• The .text section. Yang. Teks bagian. It contains only one function, __do_global_dtors_aux , which goes through __DTOR_LIST__ from the head and calls each destructor function on the list. Hanya berisi satu fungsi, __do_global_dtors_aux yang berjalan melalui __DTOR_LIST__ dari kepala dan panggilan masing-masing fungsi destructor pada daftar.
• The .fini section. The. Fini bagian. It contains only a call to __do_global_dtors_aux . Hanya berisi ajakan untuk __do_global_dtors_aux Please remember it has just a function call without return since the .fini section in crtbegin.o is part of the body of a function. Harap diingat itu baru saja fungsi panggilan tanpa kembali sejak. Fini crtbegin.o bagian dalam adalah bagian dari tubuh fungsi.
crtend.o crtend.o

There are also four sections: Ada juga empat bagian:
• The .ctors section. The. Ctors bagian. It has a local symbol, __CTOR_END__ , which is the label for the tail of the global constructor function pointer array. Memiliki simbol lokal, __CTOR_END__ yang merupakan label untuk ekor fungsi constructor global pointer array.
• The .dtors section. The. Dtors bagian. It has a local symbol, __DTOR_END__ , which is the label for the tail of the global destructor function pointer array. Memiliki simbol lokal, __DTOR_END__ yang merupakan label untuk ekor destructor global fungsi pointer array.
• The .text section. Yang. Teks bagian. It contains only one function, __do_global_ctors_aux , which goes through __CTOR_LIST__ from the tail and calls each constructor function on the list. Hanya berisi satu fungsi, __do_global_ctors_aux yang berjalan melalui __CTOR_LIST__ dari ekor dan panggilan masing-masing pembina fungsi dalam daftar.
• The .init section. The. Init bagian. It contains only a function call to __do_global_ctors_aux . Hanya berisi fungsi panggil ke __do_global_ctors_aux Please remember it has just a function call without return since the .init section in crtend.o is part of the body of a function. Harap diingat itu baru saja fungsi panggilan tanpa kembali sejak. Init bagian dalam crtend.o adalah bagian dari tubuh fungsi.
crti.o crti.o

It has only a function label _init in the .init section and a function label _fini in the .fini section. Itu hanya memiliki fungsi label _init dalam. Init bagian dan fungsi label _fini dalam. Fini bagian.
crtn.o crtn.o

It has only a return instruction each in the .init and .fini sections. Itu hanya memiliki instruksi kembali masing-masing dalam. Init dan. Fini bagian.
At compile time while generating the relocatable files, gcc puts each global constructor on __CTOR_LIST__ by putting a pointer to the constructor function in the .ctors section. It also puts each global destructor on __DTOR_LIST__ by putting a pointer to the destructor function in the .dtors section. Pada saat waktu kompilasi relocatable menghasilkan file, gcc global yang menempatkan masing-masing konstruktor pada __CTOR_LIST__ dengan meletakkan sebuah pointer ke dalam fungsi konstruktor. Ctors bagian. Hal itu juga menempatkan masing-masing destructor global pada __DTOR_LIST__ dengan meletakkan sebuah pointer ke dalam fungsi destructor. Dtors bagian.
At link time, the gcc driver places crtbegin.o immediately before all the relocatable files and crtend.o immediately after all the relocatable files. Link pada waktu, tempat-tempat sopir gcc crtbegin.o segera sebelum semua relocatable file dan crtend.o segera setelah semua file relocatable. In addition, crti.o was placed before crtbegin.o and crtn.o was placed after crtend.o . Selain itu, crti.o ditempatkan sebelum crtbegin.o dan crtn.o ditempatkan setelah crtend.o.
While generating the executable file, the link editor, ld , concatenates the .ctors sections and the .dtors sections from all the relocatable files to form __CTOR_LIST__ and __DTOR_LIST__ , respectively. Sementara menghasilkan file eksekusi, link editor, ld, yang concatenates. Ctors bagian dan. Dtors bagian dari semua file relocatable membentuk __CTOR_LIST__ dan __DTOR_LIST__ masing-masing. The .init sections from all the relocatable files form the _init function and the .fini sections form the _fini function. The. Init bagian dari semua file relocatable membentuk _init fungsi dan. Fini membentuk bagian _fini fungsi.
At run time, the system will execute the _init function before the main function and execute the _fini function after the main function returns. Pada saat run time, sistem akan mengeksekusi _init fungsi sebelum fungsi utama dan jalankan _fini fungsi setelah fungsi utama kembali.


SOURCE CODE

#include
class CRectangle {
int width, height;
public:
CRectangle (int,int);
int area (void) {return (width*height);}
};
CRectangle::CRectangle (int a, int b) {
width = a;
height = b;
}
int main () {
CRectangle rect (3,4);
CRectangle rectb (5,6);
cout << "rect area: " << rect.area() << endl;
cout << "rectb area: " << rectb.area() << endl;
}


OUTPUT

Tidak ada komentar:

Posting Komentar