程序是从main开始执行,最后main返回就结束了吗?

本文详细探讨了C/C++程序的执行流程,从main函数之前的操作到main函数执行,再到main结束后如何关闭进程。通过示例代码展示了如何在main之前执行自定义初始化代码,揭示了入口点函数的作用,包括变量初始化、运行库准备等操作。

前几天看到一个朋友,想要在main执行之前执行一段代码,当时记得不太清,就不敢贸然回答,怕误导别人。等弄清楚了,却发现那个帖子不见了,汗。

 

先看几个小例子:

 

1.

 

#include <stdio.h>

int a = 3;

 

int main(int argc, char* argv[])
{
    printf("The value of a is %d./n", a);
    getchar();
    return 0;
}

 

2.

 

#include <iostream>
#include <string>

using namespace std;

class Test
{
public:
    Test()
    {
        cout << "This is a test" << endl;
    }
};

Test t;

int main(int argc, char* argv[])
{
    getchar();
    return 0;
}

 

3.

 

#include <iostream>
#include <string>

using namespace std;

void foo()
{
    cout << "After main...." << endl;
}

int main(int argc, char* argv[])
{
    atexit(&foo);
    cout << "End of main" << endl;
    return 0;
}

 

4. 在上面任意一个例子中的main函数中下一个断点,执行,等中断的时候打开Call Stack。

 

-----------------------------------

 

C/C++课上,老师都告诉我们,C/C++程序从main函数开始执行,main函数返回时,程序就结束了。

执行完上面的几个小程序,想必诸位可能有点被愚弄了许久的感觉吧?

1. 在main执行之前,变量a就已经是我们初始化的值3了,谁帮我们做的?

2. main什么都没做,只是在等待输入并退出,而t却被实例化了。

3. main已经结束了,foo函数居然被执行了!

 

显然,操作系统在装入我们编译出来的程序时,最先执行的代码,并不是main函数的第一行,而是另外一些代码,这些代码为程序的执行作好了一切准备:准备好输入输出,准备好运行库,这也就是为什么main还没有执行的时候,我们的全局变量就已经初始化好了,printf,cout,*alloc之类的运行时库都已经可以使用,命令行参数已经等着我们获取的原因。main函数返回时,它又记录main的返回值,执行登记的atexit函数,做一下大扫除,比如卸载运行库,释放内存之后结束进程并将main的返回值交给操作系统。

 

一般而言,我们将上述代码称为入口点函数。对MS VC++而言,这个函数的名称是mainCRTStartup,这个函数干了两件事,一是初始化security cookie,主要是为了防止栈溢出;二是调用__tmainCRTStartup,在这里作进一步的初始化操作,有兴趣地话,可以看看VS 2010附带的crt源代码,位置在Microsoft Visual Studio 10.0/VC/crt/src/crtexe.c,这里贴上部分代码。

 

__declspec(noinline)
int
__tmainCRTStartup(
        void
        )
{
#ifdef _WINMAIN_
        _TUCHAR *lpszCommandLine;
        STARTUPINFOW StartupInfo;
        BOOL inDoubleQuote=FALSE;

        GetStartupInfoW( &StartupInfo );
#endif  /* _WINMAIN_ */

#ifdef _M_IX86
        /*
         * Enable app termination when heap corruption is detected on
         * Windows Vista and above. This is a no-op on down-level OS's
         * and enabled by default for 64-bit processes.
         */

        if (!_NoHeapEnableTerminationOnCorruption)
        {
            HeapSetInformation(NULL, HeapEnableTerminationOnCorruption, NULL, 0);
        }
#endif  /* _M_IX86 */

        /*
         * Guard the initialization code and the call to user's main, or
         * WinMain, function in a __try/__except statement.
         */

        __try
        {
            /*
             * There is a possiblity that the module where this object is
             * linked into is a mixed module. In all the cases we gurantee that
             * native initialization will occur before managed initialization.
             * Also in anycase this code should never be called when some other
             * code is initializing native code, that's why we exit in that case.
             *
             * Do runtime startup initializers.
             *
             * Note: the only possible entry we'll be executing here is for
             * __lconv_init, pulled in from charmax.obj only if the EXE was
             * compiled with -J.  All other .CRT$XI* initializers are only
             * run as part of the CRT itself, and so for the CRT DLL model
             * are not found in the EXE.  For that reason, we call _initterm,
             * not _initterm_e, because __lconv_init will never return failure,
             * and _initterm_e is not exported from the CRT DLL.
             *
             * Note further that, when using the CRT DLL, executing the
             * .CRT$XI* initializers is only done for an EXE, not for a DLL
             * using the CRT DLL.  That is to make sure the -J setting for
             * the EXE is not overriden by that of any DLL.
             */
            void *lock_free=0;
            void *fiberid=((PNT_TIB)NtCurrentTeb())->StackBase;
            int nested=FALSE;
            while((lock_free=InterlockedCompareExchangePointer((volatile PVOID *)&__native_startup_lock, fiberid, 0))!=0)
            {
                if(lock_free==fiberid)
                {
                    nested=TRUE;
                    break;
                }

                /* some other thread is running native startup/shutdown during a cctor/domain unload.
                    Should only happen if this DLL was built using the Everett-compat loader lock fix in vcclrit.h
                */
                /* wait for the other thread to complete init before we return */
                Sleep(1000);
            }

            if (__native_startup_state == __initializing)
            {
                _amsg_exit( _RT_CRT_INIT_CONFLICT);
            }
            else if (__native_startup_state == __uninitialized)
            {
                __native_startup_state = __initializing;
#ifndef _SYSCRT
                if (_initterm_e( __xi_a, __xi_z ) != 0)
                {
                    return 255;
                }
#else  /* _SYSCRT */
                _initterm((_PVFV *)(void *)__xi_a, (_PVFV *)(void *)__xi_z);
#endif  /* _SYSCRT */
            }
            else
            {
                has_cctor = 1;
            }

            /*
            * do C++ constructors (initializers) specific to this EXE
            */
            if (__native_startup_state == __initializing)
            {
                _initterm( __xc_a, __xc_z );
                __native_startup_state = __initialized;
            }
            _ASSERTE(__native_startup_state == __initialized);
            if(!nested)
            {
                /* For X86, the definition of InterlockedExchangePointer wrongly causes warning C4312 */
#pragma warning(push)
#pragma warning(disable:4312)
                InterlockedExchangePointer((volatile PVOID *)&__native_startup_lock, 0);
#pragma warning(pop)
            }

            /*
             * If we have any dynamically initialized __declspec(thread)
             * variables, then invoke their initialization for the primary
             * thread used to start the process, by calling __dyn_tls_init
             * through a callback defined in tlsdyn.obj.
             */
            if (__dyn_tls_init_callback != NULL &&
                _IsNonwritableInCurrentImage((PBYTE)&__dyn_tls_init_callback))
            {
                __dyn_tls_init_callback(NULL, DLL_THREAD_ATTACH, NULL);
            }

            /* Enable buffer count checking if linking against static lib */
            _CrtSetCheckCount(TRUE);

#ifdef _WINMAIN_
            /*
             * Skip past program name (first token in command line).
             * Check for and handle quoted program name.
             */
#ifdef WPRFLAG
            lpszCommandLine = (wchar_t *)_wcmdln;
#else  /* WPRFLAG */
            lpszCommandLine = (unsigned char *)_acmdln;
#endif  /* WPRFLAG */

            while (*lpszCommandLine > SPACECHAR ||
                   (*lpszCommandLine&&inDoubleQuote)) {
                /*
                 * Flip the count from 1 to 0 or 0 to 1 if current character
                 * is DOUBLEQUOTE
                 */
                if (*lpszCommandLine==DQUOTECHAR) inDoubleQuote=!inDoubleQuote;
#ifdef _MBCS
                if (_ismbblead(*lpszCommandLine)) {
                    if (lpszCommandLine) {
                        lpszCommandLine++;
                    }
                }
#endif  /* _MBCS */
                ++lpszCommandLine;
            }

            /*
             * Skip past any white space preceeding the second token.
             */
            while (*lpszCommandLine && (*lpszCommandLine <= SPACECHAR)) {
                lpszCommandLine++;
            }

#ifdef WPRFLAG
            mainret = wWinMain(
#else  /* WPRFLAG */
            mainret = WinMain(
#endif  /* WPRFLAG */
                       (HINSTANCE)&__ImageBase,
                       NULL,
                       lpszCommandLine,
                       StartupInfo.dwFlags & STARTF_USESHOWWINDOW
                        ? StartupInfo.wShowWindow
                        : SW_SHOWDEFAULT
                      );
#else  /* _WINMAIN_ */

#ifdef WPRFLAG
            __winitenv = envp;
            mainret = wmain(argc, argv, envp);
#else  /* WPRFLAG */
            __initenv = envp;
            mainret = main(argc, argv, envp);
#endif  /* WPRFLAG */

#endif  /* _WINMAIN_ */

            /*
             * Note that if the exe is managed app, we don't really need to
             * call exit or _c_exit. .cctor should be able to take care of
             * this.
             */
            if ( !managedapp )
                exit(mainret);

            if (has_cctor == 0)
                _cexit();

        }
        __except ( _XcptFilter(GetExceptionCode(), GetExceptionInformation()) )
        {
            /*
             * Should never reach here
             */

            mainret = GetExceptionCode();

            /*
             * Note that if the exe is managed app, we don't really need to
             * call exit or _c_exit. .cctor should be able to take care of
             * this.
             */
            if ( !managedapp )
                _exit(mainret);

            if (has_cctor == 0)
                _cexit();
        } /* end of try - except */

        return mainret;
}

 

现在回到最开始的话题,如何在main函数之前执行代码,办法是有的,那就是自己添加初始化函数。

下面是一个在VC++ 2010中添加自定义初始化代码的例子:

 

#include <iostream>

 

// Defines the name of the section in which our init code stored.
#define SCNAME ".CRT$XCI"

 

#pragma section(SCNAME, long, read)

 

using namespace std;

 

void foo()
{
    cout << "Initializing..." << endl;
}

 

typedef  void (__cdecl *_PVFV)();
__declspec(allocate(SCNAME)) _PVFV dummy[] = { foo };

 

int main(int argc, char* argv[])
{
    getchar();
    return 0;
}

 

 

_PVFV的原始定义在crt/internal.h中,为指向无参数无返回值的函数指针。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值