简体   繁体   English

运行时检查失败#2-变量'result'周围的堆栈已损坏

[英]Run-Time Check Failure #2 - Stack around the variable 'result' was corrupted

I have compiled this code and got Run-Time Check Failure #2 - Stack around the variable 'result' was corrupted exception. 我已经编译了这段代码,并获得运行时检查失败#2-变量'result'周围的堆栈损坏 But when I changed result array size from 2 to 4 exception disappeared. 但是,当我将结果数组大小从2更改为4时,异常消失了。 Can you explain why this happens? 您能解释为什么会这样吗? Sorry, if you found this question too basic. 抱歉,如果您发现这个问题太基础了。

#include "stdafx.h"

string get_cpu_name()
{
    uint32_t data[4] = { 0 };
    _asm
    {
        cpuid;
        mov data[0], ebx;
        mov data[4], edx;
        mov data[8], ecx;
    }
    return string((const char *)data);
}
void assembler()
{
    cout << "CPU is " << get_cpu_name() << endl;

    float f1[] = { 1.f , 22.f};
    float f2[] = { 5.f , 3.f };
    float result[2] = { 0.f };

    /*float f1[] = { 1.f , 22.f , 1.f , 22.f };
    float f2[] = { 5.f , 3.f , 1.f , 22.f };
    float result[4] = { 0.f };*/


    _asm
    {
        movups xmm1, f1;
        movups xmm2, f2;
        mulps xmm1, xmm2;
        movups result, xmm1;
    }

    /*for (size_t i = 0; i < 4; i++)*/
    for (size_t i = 0; i < 2; i++)
    {
        cout << result[i] << "\t";
    }
    cout << endl;

}


int main()
{
    assembler();
    getchar();
    return 0;
}

The movups instruction writes 128 bits (16 bytes) to memory . movups指令将128位(16字节)写入内存 You are writing this to the location of an 8-byte array (2*4 bytes, or 64 bits). 您正在将此写入8字节数组的位置(2 * 4字节或64位)。 The 8 bytes after the array will also be written to. 数组之后的8个字节也将被写入。

You should make sure there are at least 16 bytes of space to write the result, or you should make sure to write less than 16 bytes there. 您应确保至少有16个字节的空间可写入结果,或者应确保在其中写入的空间少于16个字节。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 运行时检查失败 #2 - 变量“IDNumber”周围的堆栈已损坏 - Run-Time Check Failure #2 - Stack around the variable 'IDNumber' was corrupted 运行时检查失败#2-变量&#39;indices&#39;周围的堆栈已损坏 - Run-Time Check Failure #2 - Stack around the variable 'indices' was corrupted 运行时检查失败#2-变量&#39;numberchoices&#39;周围的堆栈已损坏 - Run-Time Check Failure #2 - Stack around the variable 'numberchoices' was corrupted 运行时检查失败#2-变量&#39;ex&#39;周围的堆栈已损坏 - Run-Time Check Failure #2 - Stack around the variable 'ex' was corrupted 运行时检查失败#2-变量&#39;NearID&#39;周围的堆栈已损坏 - Run-Time Check Failure #2 - Stack around the variable 'NearID' was corrupted 运行时检查失败-变量周围的堆栈已损坏 - Run-Time Check Failure - Stack around variable was corrupted 运行时检查失败#2-变量&#39;s&#39;周围的堆栈已损坏 - Run-Time Check Failure #2 - Stack around the variable 's' was corrupted 运行时检查失败#2 - 变量&#39;B&#39;周围的堆栈已损坏 - Run-Time Check Failure #2 - Stack around the variable 'B' was corrupted 运行时检查失败#2-变量周围的堆栈-已损坏 - Run-Time Check Failure #2 - Stack around the variable — was corrupted 运行时检查失败 #2 - 变量“newRow”周围的堆栈已损坏 - Run-Time Check Failure #2 - Stack around the variable 'newRow' was corrupted
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM