Is there a way to force a variable to be stored in the cache in C?

前端 未结 5 819
误落风尘
误落风尘 2021-02-05 23:55

I just had a phone interview where I was asked this question. I am aware of ways to store in register or heap or stack, but cache specifically?

5条回答
  •  孤街浪徒
    2021-02-06 00:44

    Not in C as a language. In GCC as a compiler - look for __builtin_prefetch.

    You might be interested in reading What every programmer should know about memory.

    Edit:

    Just to clear some confusion - caches are physically separate memories in hardware, but not in software abstraction of the machine. A word in a cache is always associated with address in main memory. This is different from the CPU registers, which are named/addressed separately from the RAM.

提交回复
热议问题