【问题标题】:How can I understand these symbols in a C++ code? [duplicate]如何在 C++ 代码中理解这些符号? [复制]
【发布时间】:2019-03-30 10:35:22
【问题描述】:
  1. 为什么posix_memalign 写成::posix_memalign
  2. 这里的memory 是什么?

我希望对我的高速缓存和 RAM 的读写速度进行基准测试。为此,我想使用谷歌基准库,我看到了一个使用它的示例代码。或多或少我明白了代码的概念,但memory 站在这里是什么?为什么我们将它作为指向 void 的指针?另外,为什么这个例子写posix_memalign::?是不是因为我们引用的是google benchmark class?

#include <cstddef>
#include <cstdlib>
#include <string.h>
#include <emmintrin.h>
#include <immintrin.h>

#include "benchmark/benchmark.h"

#define ARGS \
  ->RangeMultiplier(2)->Range(1024, 2*1024*1024) \
  ->UseRealTime()

template <class Word>
void BM_write_seq(benchmark::State& state) {
  void* memory; 
  if (::posix_memalign(&memory, 64, state.range_x()) != 0) return;
  void* const end = static_cast<char*>(memory) + state.range_x();
  Word* const p0 = static_cast<Word*>(memory);
  Word* const p1 = static_cast<Word*>(end);
  Word fill; ::memset(&fill, 0xab, sizeof(fill));
  while (state.KeepRunning()) {
    for (Word* p = p0; p < p1; ++p) {
      benchmark::DoNotOptimize(*p = fill);
    }
  }
  ::free(memory);
}

【问题讨论】:

  • 这个问题怎么重复?

标签: c++ microbenchmark google-benchmark


【解决方案1】:

为什么 posix_memalign 写成 ::posix_memalign

:: 左边没有命名空间是指全局命名空间

为什么

可能你在一个命名空间内,你需要一个全局函数。从sn-p我看不出来

这里的记忆是什么?

在 ::posix_memalign 中分配并在 ::free(memory) 中释放的原始指针;

为什么我们将它作为指向 void 的指针?

因为它只是没有类型的原始内存,所以它适合原始指针。 简单的旧 C 风格。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-08-08
    • 1970-01-01
    • 1970-01-01
    • 2021-03-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-08
    • 2013-10-09
    相关资源
    最近更新 更多