简体   繁体   中英

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. But when I changed result array size from 2 to 4 exception disappeared. 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 . You are writing this to the location of an 8-byte array (2*4 bytes, or 64 bits). The 8 bytes after the array will also be written to.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM