DEV Community

Discussion on: Pointers

Collapse
 
pauljlucas profile image
Paul J. Lucas

By the way:

But in case of pointers, multiple trips are required to the RAM from CPU to get a value.

That's simply not true. For example, consider this. Even though *p is in a loop, the compiler reads the value once and is clever enough to convert the loop into a multiplication. Even in a simpler case like:

int n = *p;
n += *p;
Enter fullscreen mode Exit fullscreen mode

the compiler will still read from *p only once because it assumes that there are no data races. (If your program is single-threaded, then there can't be any data races; if your program is multi-threaded and there are data races, then your program has undefined behavior and the compiler isn't responsible.)

I don't know where you got the idea that reading from a pointer requires multiple trips to RAM; but in the future, I respectfully suggest that perhaps you should really be 100% sure about a topic before writing about it and spreading incorrect information.