Is accessing a pointer directly faster than accessing it through a structure?

Say I have a C program that moves around a directory and saves the metadata of a directory entry in struct dirent *with a name dir. The program accesses the field several times dir->d_name. I am wondering if setting an auxiliary character pointer (e.g.char *str = dir->d_name) make the program faster. I know dereferencing pointers are a relatively expensive operation. The fact is that if I set a helper variable, I still dereference the pointer; the only difference in one case is I dereference a pointer to a structure, and in the second case I dereference a pointer to a string. Therefore, I assume that the main question is how expensive is access to individual fields of the structure? I assume that at the machine level, this will be due to the first dereferencing of the pointer to the structure in order to get its address, and then increasing this address by shifting the desired field. In the case of an auxiliary pointer, you will dereference, and at this point you already have the starting address for the string.

+4
source share
1

, , . 70 ( K & R C...) .

. - "-", -, , , ( ref n1570 draft C11 5.1.2.3 ). , ( ). , . , , .

+1

Source: https://habr.com/ru/post/1692721/


All Articles