为什么我可以在c ++/CLI中获得未处理的异常访问冲突?

And*_*ndy 2 c++-cli tchar visual-studio-2010 visual-c++

我一直在努力编写一个解决方案,从c ++ win32console和c ++ dll中解脱出来.我终于设法让他们说话没有链接器错误(所以我假设两者都是完全托管的c ++/CLI项目)但是当我运行控制台时,我得到以下错误.

Company.Pins.Bank.Win32Console.exe中0x03f71849处的未处理异常:0xC0000005:访问冲突写入位置0x00000001.

控制台还显示以下内容

未处理的异常:System.NullReferenceException:未将对象引用设置为对象的实例.at wmain in c:...\win32console.cpp:第20行_wmainCRTStartup()

但我假设这是因为未处理的异常.

跟踪此错误以及我可以在下面的代码块中执行返回时发生错误.(由返回链接的方法似乎一步一步很好,只是在返回它似乎变坏.)以防万一你没有注意到,我没有自己编写下面的代码,它是由visual studio生成的.

#ifdef WPRFLAG
int wmainCRTStartup(
#else  /* WPRFLAG */
int mainCRTStartup(
#endif  /* WPRFLAG */

#endif  /* _WINMAIN_ */
        void
        )
{
        /*
         * The /GS security cookie must be initialized before any exception
         * handling targetting the current image is registered.  No function
         * using exception handling can be called in the current image until
         * after __security_init_cookie has been called.
         */
        __security_init_cookie();

        return __tmainCRTStartup();
}

#include "stdafx.h"
#include "UInstruction.h"

#define DllExport  __declspec(dllexport)
#define DllImport  __declspec(dllimport)

using namespace System;
Run Code Online (Sandbox Code Playgroud)

编辑:win32console.cpp代码如下.

//int main(array<System::String ^> ^args)
int _tmain(int argc, _TCHAR* argv[])
{
    auto P2 = (TCHAR *)"3 Barrowstead";
    TCHAR* P3 = (TCHAR *)"3 Barrowstead";
    double* P1;
    P1[0] = 13;

    UserInstruction(P1, P2, P3);
}
Run Code Online (Sandbox Code Playgroud)

Jam*_*lis 8

你声明一个指针,不要初始化它,所以它不指向一个对象(它包含一些垃圾地址):

double* P1;     
Run Code Online (Sandbox Code Playgroud)

然后你试着写到这个未初始化的指针指向的地方:

P1[0] = 13;
Run Code Online (Sandbox Code Playgroud)

您不能使用未初始化的变量.P1在取消引用它之前,需要初始化以指向某个对象.