我正在编写一个在C ++ 14的Xcode 9.4上缓存颠簸的示例。
我的函数使用std :: array创建矩阵。在本地函数范围内创建矩阵时,该函数将无法调用中止(EXC_BAD_ACCESS)。全局范围内的矩阵成功执行,没有终止。为什么?
/*
* Fails:
*/
static void
cache_thrashing_example()
{
const auto l1_capacity = 32768;
const auto len = l1_capacity / sizeof(int);
array<array<int, len>, len> matrix;
// sysctl -a hw.l1icachesize = 32768
auto counter = 0;
for (auto x = 0; x < len; ++x) // cache block taken here
for (auto j = 0; j < len; ++j)
matrix[x][j] = counter++;
}
/*
* Succeeds:
*/
const auto l1_capacity = 32768;
const auto len = l1_capacity / sizeof(int);
array<array<int, len>, len> matrix;
static void
cache_thrashing_example()
{
// sysctl -a hw.l1icachesize = 32768
auto counter = 0; // Abort called here
for (auto x = 0; x < len; ++x) // cache block taken here
for (auto j = 0; j < len; ++j)
matrix[x][j] = counter++;
}