2

I saw a piece of code that looked like this sample program:

#include <immintrin.h>

int main() {
  int arr[1024];
  for (int i = 0; i < 1024; i++) {
    arr[i] = i;
  }

  // Declare a pointer to the first element of the array.
  int *ptr = arr;

  // Loop over the array, using _mm_prefetch to prefetch data into the CPU cache.
  for (int i = 0; i < 1024; i++) {
    _mm_prefetch((char *)ptr + 64, _MM_HINT_T0);

    // Do something with the current element.
    // ...

    // Increment the pointer.
    ptr++;
  }

  return 0;
}

Does this line in the program cause a heap buffer overflow towards the end of the loop?

_mm_prefetch((char *)ptr + 64, _MM_HINT_T0);

Or is it safe to prefetch memory outside the bounds of the array?

1
  • Quote: if a memory fault is detected, a bus cycle is not initiated and the instruction is treated like a NOP. Commented May 13, 2023 at 14:12

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.