SIOF

SIOF 是static initialization order fiasco的缩写,涉及的问题为初始化顺序;

下面为 stack overflow 中 Hans Passant 的解释:

SIOF is very much a runtime artifact, the compiler and linker don't have much to do with it. Consider the atexit() function, it registers functions to be called at program exit. Many CRT implementations have something similar for program initialization, let's call it atinit().

Initializing these global variables requires executing code, the value cannot be determined by the compiler. So the compiler generates snippets of machine code that execute the expression and assigns the value. These snippets need to be executed before main() runs.

That's where atinit() comes into play. A common CRT implementation walks a list of atinit function pointers and execute the initialization snippets, in order. The problem is the order in which the functions are registered in the atinit() list. While atexit() has a well defined LIFO order, and it is implicitly determined by the order in which the code calls atexit(), such is not the case for atinit functions. The language specification doesn't require an order, there is nothing you could do in your code to specify an order. SIOF is the result.

One possible implementation is the compiler emitting function pointers in a separate section. The linker merges them, producing the atinit list. If your compiler does that then the initialization order will be determined by the order in which you link the object files. Look at the map file, you should see the atinit section if your compiler does this. It won't be called atinit, but some kind of name with "init" is likely. Taking a look at the CRT source code that calls main() should give insight as well.

使用代码测试:
file1.cpp

extern int x;
int y = x + 1;


file2.cpp

extern int y;
int x = y + 1;

main.cpp

#include <stdio.h>
#include <stdlib.h>

extern int x;
extern int y;

int main()
{
    printf("x : %d, y : %d\n", x, y);
    return 0;
}

liuzebo@ubuntu:/mnt/hgfs/Workspace/SnippetTest/siof$ g++ main.o file1.o file2.o -o main
liuzebo@ubuntu:/mnt/hgfs/Workspace/SnippetTest/siof$ ./main
x : 2, y : 1
liuzebo@ubuntu:/mnt/hgfs/Workspace/SnippetTest/siof$ g++ main.o file2.o file1.o -o main
liuzebo@ubuntu:/mnt/hgfs/Workspace/SnippetTest/siof$ ./main
x : 1, y : 2


光纤折射率的模式主要有以下几种: ### 阶跃折射率分布模式 在阶跃折射率光纤(SIOF)中,纤芯的折射率是均匀的,包层的折射率低于纤芯,且在纤芯 - 包层界面处折射率发生阶跃变化。在这种模式下,不同角度的光线轴向速度不同,即不同传播路径的光线在光纤中传播的速度存在差异[^1]。 ### 渐变折射率分布模式 渐变折射率分布光纤(GIOF)中的光线沿 z 轴传播的相速度恒定不变,与光线的轴向夹角 θz 无关。其纤芯的折射率从中心向边缘逐渐减小,形成一种渐变的分布,使得光线在光纤中传播时会沿着弯曲的路径前进,从而减少了模式色散[^1]。 ### 单模与多模 - **单模**:单模光纤只允许一种模式的光在其中传播,通常用于长距离、高速率的通信系统。单模光纤的纤芯直径较小,一般在 8 - 10 微米左右,能够保证光信号以单一模式稳定传输,减少了模式间的干扰,具有较低的色散和衰减。 - **多模**:多模光纤允许多种模式的光同时在其中传播。多模光纤的纤芯直径较大,一般在 50 微米或 62.5 微米。由于多种模式的光在光纤中传播的速度不同,会导致模式色散,因此多模光纤通常适用于短距离的通信系统。 ### 代码示例 以下是一个简单的 Python 示例,用于模拟不同模式下的光线传播: ```python import numpy as np import matplotlib.pyplot as plt # 模拟阶跃折射率光纤 def step_index_fiber(): core_radius = 5 # 纤芯半径 cladding_radius = 6 # 包层半径 core_index = 1.45 # 纤芯折射率 cladding_index = 1.44 # 包层折射率 x = np.linspace(-cladding_radius, cladding_radius, 100) y = np.where(np.abs(x) <= core_radius, core_index, cladding_index) plt.plot(x, y) plt.title('Step Index Fiber Refractive Index Profile') plt.xlabel('Distance from core center (μm)') plt.ylabel('Refractive Index') plt.show() # 模拟渐变折射率光纤 def graded_index_fiber(): core_radius = 5 # 纤芯半径 cladding_radius = 6 # 包层半径 core_index_max = 1.45 # 纤芯中心最大折射率 cladding_index = 1.44 # 包层折射率 x = np.linspace(-cladding_radius, cladding_radius, 100) y = np.where(np.abs(x) <= core_radius, core_index_max - (core_index_max - cladding_index) * (x / core_radius) ** 2, cladding_index) plt.plot(x, y) plt.title('Graded Index Fiber Refractive Index Profile') plt.xlabel('Distance from core center (μm)') plt.ylabel('Refractive Index') plt.show() # 运行模拟 step_index_fiber() graded_index_fiber() ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值