intmain(int argc,char *argv[]) { printf("start of main.\n"); cout << "Before the main function is called,IO initialization was completed." << endl; printf("a and b is right. b.n = %d\n",b.n); int *p=newint; printf("Heap initialization was completed too.\n"); delete p; //防止内存泄露 printf("argc and argv's value is right,too.\nargc = %d,argv[0]=%s,argv[1]=%s,argv[2]=%s\n",argc,argv[0],argv[1],argv[2]); int n; scanf("%d",&n); atexit(&func2); printf("Input initialization was completed!\nn=%d!\n",n); printf("str + to_string(n) = %s\n",(str+to_string(n)).c_str()); cout << "end of main.\n"; return1; }
其实这个程序的逻辑还是比较清晰的:
定义一个 string 型变量(调用了 string 型变量的构造函数)。
定义一个类 my_class,有两个构造函数,一个析构函数和一个成员变量 n。
定义 my_class 类的两个实例,分别叫 a 和 b,对 a 调用第一个构造函数,对 b 调用第二个构造函数。
default ctor. one arg:int n ctor func() called. start of main. Before the main function is called,IO initialization was completed. a and b is right. b.n = 1 Heap initialization was completed too. argc and argv's value is right,too. argc = 3,argv[0]=a,argv[1]=at,argv[2]=1 121 Input initialization was completed! n=121! str + to_string(n) = aa121 end of main. func2() called. dtor. dtor.
你看,在 start of main 之前和 end of main 之后都有输出,而在 start of main 之前,其实 IO 初始化已经完成(可以用 printf,scanf,cin,cout),a,b 初始化完成(b.n=1),argc 和 argv 被正确传入,str 被正确构造。而在 main 之后,调用了 str,a 和 b 的析构函数,还调用了由 atexit 函数注册的 func2 函数。
所以,铁证如山,程序真的不是从 main 函数开始执行的!
如果你按 F11 无法跳到 exe_common.inl,而是程序直接停止了:
用这个:
1 2 3 4 5 6 7 8 9
intmain() { int* p = newint; for (int* q = p - 1000; q <= p + 1000; q++) { *q = 123123123; } return0; }
static __declspec(noinline) int __cdecl __scrt_common_main_seh() { if (!__scrt_initialize_crt(__scrt_module_type::exe)) __scrt_fastfail(FAST_FAIL_FATAL_APP_EXIT); bool has_cctor = false; __try { boolconst is_nested = __scrt_acquire_startup_lock(); if (__scrt_current_native_startup_state == __scrt_native_startup_state::initializing) { __scrt_fastfail(FAST_FAIL_FATAL_APP_EXIT); } elseif (__scrt_current_native_startup_state == __scrt_native_startup_state::uninitialized) { __scrt_current_native_startup_state = __scrt_native_startup_state::initializing; if (_initterm_e(__xi_a, __xi_z) != 0) return255; _initterm(__xc_a, __xc_z); __scrt_current_native_startup_state = __scrt_native_startup_state::initialized; } else { has_cctor = true; } __scrt_release_startup_lock(is_nested); // If this module has any dynamically initialized __declspec(thread) // variables, then we invoke their initialization for the primary thread // used to start the process: _tls_callback_type const* const tls_init_callback = __scrt_get_dyn_tls_init_callback(); if (*tls_init_callback != nullptr && __scrt_is_nonwritable_in_current_image(tls_init_callback)) { (*tls_init_callback)(nullptr, DLL_THREAD_ATTACH, nullptr); } // If this module has any thread-local destructors, register the // callback function with the Unified CRT to run on exit. _tls_callback_type const * const tls_dtor_callback = __scrt_get_dyn_tls_dtor_callback(); if (*tls_dtor_callback != nullptr && __scrt_is_nonwritable_in_current_image(tls_dtor_callback)) { _register_thread_local_exe_atexit_callback(*tls_dtor_callback); } // // Initialization is complete; invoke main... // intconst main_result = invoke_main(); // // main has returned; exit somehow... // if (!__scrt_is_managed_app()) exit(main_result); if (!has_cctor) _cexit(); // Finally, we terminate the CRT: __scrt_uninitialize_crt(true, false); return main_result; } __except (_seh_filter_exe(GetExceptionCode(), GetExceptionInformation())) { // Note: We should never reach this except clause. intconst main_result = GetExceptionCode(); if (!__scrt_is_managed_app()) _exit(main_result); if (!has_cctor) _c_exit(); return main_result; } }
初始化 CRT
1 2
if (!__scrt_initialize_crt(__scrt_module_type::exe)) __scrt_fastfail(FAST_FAIL_FATAL_APP_EXIT);
可以看出,这里如果初始化未成功,程序就会直接退出。
而 __scrt_initialize_crt 函数,源代码我就不放了,说一下主要执行流程:
参数代表以什么方式初始化 CRT(这里是 exe,即可执行文件)
调用 __isa_available_init,用途不明,但是可以猜出用于初始化。
这里有个注释:// Notify the CRT components of the process attach, bottom-to-top,意思大概是以自下往上的方式继续初始化 CRT。
// If this module has any dynamically initialized __declspec(thread) // variables, then we invoke their initialization for the primary thread // used to start the process: _tls_callback_type const* const tls_init_callback = __scrt_get_dyn_tls_init_callback(); if (*tls_init_callback != nullptr && __scrt_is_nonwritable_in_current_image(tls_init_callback)) { (*tls_init_callback)(nullptr, DLL_THREAD_ATTACH, nullptr); } // If this module has any thread-local destructors, register the // callback function with the Unified CRT to run on exit. _tls_callback_type const * const tls_dtor_callback = __scrt_get_dyn_tls_dtor_callback(); if (*tls_dtor_callback != nullptr && __scrt_is_nonwritable_in_current_image(tls_dtor_callback)) { _register_thread_local_exe_atexit_callback(*tls_dtor_callback); }
// // main has returned; exit somehow... // if (!__scrt_is_managed_app()) exit(main_result); if (!has_cctor) _cexit(); // Finally, we terminate the CRT: __scrt_uninitialize_crt(true, false); return main_result;
// Note: We should never reach this except clause. intconst main_result = GetExceptionCode(); if (!__scrt_is_managed_app()) _exit(main_result); if (!has_cctor) _c_exit(); return main_result;
// First, check to see if we're loaded in a managed app. If we are, try to // call CorExitProcess to let the CLR handle the process termination. If // the call to CorExitProcess is successful, then it will call back through // this function with a return mode of _crt_exit_return_to_caller, at which // point we will run the C termination routines. It will then terminate the // process itself and not return. if (return_mode == _crt_exit_terminate_process && is_managed_app()) { try_cor_exit_process(return_code); }
但是 is_managed_app() 返回 false,因为 is_managed_app():
1 2
if (pe_header->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR].VirtualAddress == 0) returnfalse;
然后是:
1 2
// Run the C termination: bool crt_uninitialization_required = false;
int funny[100005], c[500005]; bool canuse[500005], vis[100005];
voiddfs(int u) { vis[u] = true; for (pair<int, int> line : web[u]) { if (canuse[line.second] && !vis[line.first]) dfs(line.first); } }
boolcheck(int n) { int k = c[0]; for (int i = 1; i <= k; i++) { canuse[c[i]] = false; } dfs(1); for (int i = 1; i <= n; i++) { if (!vis[i]) returnfalse; } returntrue; }
intmain() { int n, m; scanf("%d%d", &n, &m); for (int i = 1; i <= m; i++) { int u, v; scanf("%d%d", &u, &v); web[u].push_back({ v, i }); web[v].push_back({ u, i }); canuse[i] = true; } int q; scanf("%d", &q); for (int i = 1; i <= q; i++) { int cnt = 0; do { scanf("%d", c + cnt++); } while (getchar() == ' '); int k = cnt - 1; int k0 = c[0]; int Si = k ^ k0; //不是硅啊awa funny[i - 1] = Si; for (int i = 0; i < cnt; i++) { c[i] ^= Si; } } for (int i = 1; i < q; i++) { if (funny[i] > funny[i - 1]) printf("Connected\n"); elseprintf("Disconnected\n"); } if (check(n)) printf("Connected\n"); elseprintf("Disconnected\n"); return0; }