error MSB6006: “CL.exe” exited with code 2

后端 未结 9 1718
刺人心
刺人心 2020-12-31 04:42

I\'m writing with visual c++ and when I compile this error occures:

C:\\Program Files (x86)\\MSBuild\\Microsoft.Cpp\\v4.0\\Platforms\\Win32\\Microsoft.Cpp.Wi         


        
9条回答
  •  青春惊慌失措
    2020-12-31 05:21

    Just wanted to add another case where this happens with Visual C++ 2019 (16.1.4):

    ...

    char *s;
    for(int i = 0; i < n; ++i)
    {
       if(i == 4) // we know n is going to be >= 4 but Visual C++ doesn't
           s = getStringPointerFromSomewhere();
    }
    
    puts(s); 
    

    Visual C++ will print a message in the Output: potentially uninitialized local pointer variable 's' used, but crash instead of reporting it as a normal error or warning (arguably this should be a warning, since it's perfectly legal C code).

    The solution for this one is to just assign s to NULL after declared:

    char *s = NULL;
    

    (So... just use the probably good habit of initializing pointers and other variables when declared I guess.)

提交回复
热议问题