Inside the C++ Object Model 读书笔记(六)

本文详细探讨了C++对象模型的运行时语义,包括对象构造与销毁的细节,new和delete操作符的语义,临时对象的处理,以及静态全局和局部对象的初始化策略。重点讲解了全局对象、局部静态对象、对象数组的创建,以及默认构造函数在数组初始化中的作用。同时,深入解析了new运算符在数组分配和放置new操作中的行为,以及delete的特殊用法。

Runtime Semantics

All globally visible objects in C++ are placed within the program data segment.
In C, a global object can be initialized only by a constant expression, that is, one that can be evaluated at compile time.

6.1 Object Construction and Destruction

Global Objects

When cfront was the only C++ implementation and portability across machines was more important than efficiency, the following munch strategy emerged:

  1. Within each file that requires static initialization, generate an __sti() function containing the necessary constructor invocations or inline expansions. identity, for example, would cause the following __sti() function to be generated within the file matrix.c:
__sti__matrix_c__identity() {
   
    
   // Pseudo C++ Code 
   identity.Matrix::Matrix(); 
} 

where __matrix_c is an encoding of the file name and __identity represents the first nonstatic object defined within the file. Appending these two names to __sti provided a unique identifier within the executable. (Andy Koenig and Bjarne worked out this “fake static” encoding scheme in response to name-clash agonies reported by Jim Coplien.)

  1. Similarly, within each file that requires a static deallocation, generate an __std() function containing the necessary destructor invocations or inline expansions. In our example, an __std() function is generated to invoke the Matrix destructor on identity.

  2. Provide a set of runtime library munch functions: a _main() function to invoke all the __sti() functions within the executable and an exit() function to analogously invoke all the __std() functions.

cfront inserted a _main()call as the new first statement within main(). The exit() function rather than the C library exit() function was linked in by cfront’s CC command by placing the C++ standard library first on the command line.

Our solution was to use the nm command. (nm dumps the object file symbol table entries.) An executable was generated from the .o files. nm was then run on the resulting executable. Its output was piped into the munch program. (I think Rob Murray wrote munch, but nobody any longer claims to remember.) munch munched the symbol table names, looking for names that began with __sti or __std. (Yes, periodically to amuse ourselves we would begin a function with __sti such as __sti_ha_fooled_you). It then added the function names to a jump table of __sti() and __std() functions. Next, it wrote the tables into a small program text file. Then, odd as it may sound, the CC co

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值