Marin Kritik
Marin Kritik

Reputation: 3

Memory overflow with SSE-C++

I have a scenario

     int N3 =7000
    temp =(int*)malloc(sizeof(int )*N3);

    for(unsigned int i=0;i<N3;i++)
    {
              temp[i]=i;
    }



    temp = temp +10;
    __m128i* array= reinterpret_cast<__m128i*>(temp);
   int length = N3/4;

   for(unsigned int i=0;i<length;i++)
   {
      __m128i answer= _mm_loadu_si128(&array[i]);
   }

I think memory overflow is happening here. How we can avoid this error.

Upvotes: 0

Views: 69

Answers (1)

R Sahu
R Sahu

Reputation: 206737

When you execute the block:

temp = temp +10;
__m128i* sse_array = reinterpret_cast<__m128i*>(temp);
int length = N3/4;

for(unsigned int i=0;i<length;i++)
{
   __m128i result = _mm_loadu_si128(&sse_array[i]);
}

You are treating temp+10 like it is temp. By the time the for loop ends, you are accessing more than the memory you allocated by 10*sizeof(int) memory locations.

PS

Don't used a hard coded number, like 4, when computing length. Use:

int length = N3*sizeof(int)/size(__m128i);

Upvotes: 1

Related Questions