Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/131.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ Unsigned int在释放模式下的行为不同_C++_Visual Studio 2012_Unsigned Integer_Cpuid - Fatal编程技术网

C++ Unsigned int在释放模式下的行为不同

C++ Unsigned int在释放模式下的行为不同,c++,visual-studio-2012,unsigned-integer,cpuid,C++,Visual Studio 2012,Unsigned Integer,Cpuid,我有一个奇怪的问题,在调试模式下,此代码工作正常: char* PCInformation::GetCPUName() { if (CPUName[0] == '\0') { _memset(CPUName, 0, 0x3F); // Get extended ids. int CPUInfo[4] = {-1}; __cpuid(CPUInfo, 0x80000000); unsigned int

我有一个奇怪的问题,在调试模式下,此代码工作正常:

char* PCInformation::GetCPUName()
{
    if (CPUName[0] == '\0')
    {
        _memset(CPUName, 0, 0x3F);
        // Get extended ids.
        int CPUInfo[4] = {-1};
        __cpuid(CPUInfo, 0x80000000);
        unsigned int nExIds = CPUInfo[0];

        printf(3, "%d\n%d\n", nExIds, 0x80000000);

        // Get the information associated with each extended ID.
        for(unsigned int i=0x80000000; i<=nExIds; ++i)
        {
            printf(3, "0x80000000 nExIds: %X, i: %X\n", nExIds, i);
            getchar();
            __cpuid(CPUInfo, i);

            // Interpret CPU brand string and cache information.
            if  (i == 0x80000002)
            {
                _memcpy( CPUName,
                CPUInfo,
                sizeof(CPUInfo));
            }
            else if( i == 0x80000003 )
            {
                _memcpy( CPUName + 16,
                CPUInfo,
                sizeof(CPUInfo));
            }
            else if( i == 0x80000004 )
            {
                _memcpy(CPUName + 32, CPUInfo, sizeof(CPUInfo));
            }
        }
    }

    return CPUName;
}
如果你想知道为什么STD-C函数会有uu,那是因为它们是动态调用的,但这不是问题所在。因为同样的代码过去工作得很好,但我把它放在一个类中,呸

无论如何,如果我选择发布作为构建模式int,我会包含一些奇怪的值或其他东西。有关更多信息,请参见一些图片:

释放

调试 在发行版中,它以无限for循环结束

编辑:我刚刚将代码更改为:

char* PCInformation::GetCPUName()
{
    if (CPUName[0] == '\0')
    {
        _memset(CPUName, 0, 0x3F);

        int CPUInfo[4];
        _memset(CPUInfo, 0, 4);

        __cpuid(CPUInfo, 0x80000000);
        _memcpy(CPUName, CPUInfo, sizeof(CPUInfo));

        __cpuid(CPUInfo, 0x80000002);
        _memcpy(CPUName + 16, CPUInfo, sizeof(CPUInfo));

        __cpuid(CPUInfo, 0x80000003);
        _memcpy(CPUName + 32, CPUInfo, sizeof(CPUInfo));

        __cpuid(CPUInfo, 0x80000004);
        _memcpy(CPUName + 48, CPUInfo, sizeof(CPUInfo));
    }

    return CPUName;
}
虽然我还是想知道是什么引起的


关于,

您的printf函数做什么因为标准的一个没有把int作为第一个参数。那代码是怎么工作的?3未指向有效字符串。@Robᵩ 应该告诉我很抱歉,_printfintargc。。。;第一个参数是参数的数量,定义printfargc。。。calldynamicmsvrt、printf、NULL、argc、VA_ARGS和CallDynamic是一个在运行时调用该函数的函数,运行良好。编辑OP以获取更多信息。您是否有任何其他证据表明int i包含一些奇怪的值?因为我想把一切都归咎于printf。试试这个实验。在现有printf之后添加此行:printf3,0x8000000 i:%X,nExIds:%X\n,i,nExIds@抢劫ᵩ 调试当然是用generatedebug-Info-YES/Debug,我写了一个文件。
char* PCInformation::GetCPUName()
{
    if (CPUName[0] == '\0')
    {
        _memset(CPUName, 0, 0x3F);

        int CPUInfo[4];
        _memset(CPUInfo, 0, 4);

        __cpuid(CPUInfo, 0x80000000);
        _memcpy(CPUName, CPUInfo, sizeof(CPUInfo));

        __cpuid(CPUInfo, 0x80000002);
        _memcpy(CPUName + 16, CPUInfo, sizeof(CPUInfo));

        __cpuid(CPUInfo, 0x80000003);
        _memcpy(CPUName + 32, CPUInfo, sizeof(CPUInfo));

        __cpuid(CPUInfo, 0x80000004);
        _memcpy(CPUName + 48, CPUInfo, sizeof(CPUInfo));
    }

    return CPUName;
}