Array Error - Access violation reading location 0xffffffff

和自甴很熟 提交于 2019-12-07 18:04:49

问题


I have previously used SIMD operators to improve the efficiency of my code, however I am now facing a new error which I cannot resolve. For this task, speed is paramount.

The size of the array will not be known until the data is imported, and may be very small (100 values) or enormous (10 million values). For the latter case, the code works fine, however I am encountering an error when I use fewer than 130036 array values.

Does anyone know what is causing this issue and how to resolve it?

I have attached the (tested) code involved, which will be used later in a more complicated function. The error occurs at "arg1List[i] = ..."

#include <iostream>
#include <xmmintrin.h>
#include <emmintrin.h>

void main()
{
    int j;
    const int loop = 130036;
    const int SIMDloop = (int)(loop/4);
    __m128 *arg1List = new __m128[SIMDloop];

    printf("sizeof(arg1List)= %d, alignof(Arg1List)= %d, pointer= %p", sizeof(arg1List), __alignof(arg1List), arg1List);
    std::cout << std::endl;

    for (int i = 0; i < SIMDloop; i++)
    {
        j = 4*i;
        arg1List[i] = _mm_set_ps((j+1)/100.0f, (j+2)/100.0f, (j+3)/100.0f, (j+4)/100.0f);
    }
}

回答1:


Alignment is the reason.

MOVAPS--Move Aligned Packed Single-Precision Floating-Point Values

[...] The operand must be aligned on a 16-byte boundary or a general-protection exception (#GP) will be generated.

You can see the issue is gone as soon as you align your pointer:

__m128 *arg1List = new __m128[SIMDloop + 1];
arg1List = (__m128*) (((int) arg1List + 15) & ~15);


来源:https://stackoverflow.com/questions/13013717/array-error-access-violation-reading-location-0xffffffff

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!