Pergunta

  1. Is there any performance difference when we access a memory location by using a pointer and double pointer?
  2. If so, which one is faster ?
Foi útil?

Solução

There is no simple answer it, as the answer might depend in the actual machine. If I remember correctly some legacy machines (such as PDP11) offered a 'double pointer' access in a single instruction.

However, this is not the situation today. accessing memory is not as simple as it looks and requires a lot of work, due to virtual memory. For this reason - my guess is that double reference should in fact be slower on most modern machines - more work has to be done to translate two addresses from virtual addresses to physical addresses and retrieving them - but that's just educated guess.
Note however, that the compiler might optimize 'redundant' accesses for you already.

For my best knowledge however, there is no machine that has faster 'double access' than 'single access', so we can say that single access is not worse than double access.

As a side note, I believe in real life programs, the difference is neglectable (comparing to anything else done in the program), and unless done in a very performance sensitive loop - just do whatever is more readable. Also, the compiler might optimize it for you already if it can.

Outras dicas

Assuming you are talking about something like

int a = 10;
int *aptr = &a;
int **aptrptr = &aptr;

Then the cost of

*aptr = 20;

Is one dereference. The address pointed to by aptr must first be retrieved and then the address can be stored to.

The cost of

**aptrptr = 30;

Is two dereferences. The address pointed to by aptrptr must first be retrieved. Then the addess stored in that address must be retrieved. Then this address can be stored to.

Is this what you were asking?

Therefore, to conclude using a single pointer is faster if that suits your needs.

Note, that if you access a pointer or double pointer in a loop, for example,

while(some condition)
    *aptr = something;

or

while(some condition)
    **aptrptr = something;

The compiler will likely optimize so that the dereferencing is only done once at the start of the loop, so the cost is only 1 extra address fetch rather than N, where N is the numnber of times the loop executes.

EDIT: (1) As Amit correctly points out the "how" of pointer access is not explicitly a C thing... it does depend on the underlying architecture. If your machine supports a double dereference as a single instruction then there might not be a big difference. He is using the index deferred addressing mode of the PDP11 as an example. You might find out that such an instruction still chews up more cycles... consult the hardware documentation and look at the optimization that your C compiler is able to apply for your specific architecture.

The PDP11 architecture is circa the 1970s. As far as I know (if someone knows are modern architecture that can do this pleas post!), most RISC architectures and don't have such a double dereference and will probably need to do two fetches as far as I am aware.

Therefore, to conclude using a single pointer is probably faster generally, but with the caveat that specific architectures may handle this better than others and compiler optimizations, as I discussed, could make the difference negligible... to be sure you just have to profile your code and read up about your architecture :)

Let's see it in this way:

int var = 5;
int *ptr_to_var = &var;
int **ptr_to_ptr = &ptr;
  • When the variable var is accessed then you need to

    • 1.get the address of the variable
    • 2.fetch its value from that address.
  • In case of pointer ptr_to_var you need to

    • 1.get the address of the pointer variable
    • 2.fetch its value from that address (i.e, address of the variable var)
    • 3.fetch the value at the address pointed to.
  • In third case, pointer to pointer to int variable ptr_to_ptr, you need to

    • 1.get the address of the pointer to pointer variable
    • 2.fetch its value from that address (i.e, address of the pointer to variable ptr_var)
    • 3.again fetch its value from the address fetched in the second step(i.e, address of the variable var)
    • 4.fetch the value at the address pointed to.

So we can say that accessing via pointer to pointer variable is slower than that of pointer variable which in turn slower than that of normal variable accessing.

I got curious and set up the following scenario:

int v = 0;
int *pv = &v;
int **ppv = &pv;

I tried dereferencing the pointers and took a look at the disassembly, which showed the following:

int x;
x = *pv;
    00B33C5B  mov         eax,dword ptr [pv]  
    00B33C5E  mov         ecx,dword ptr [eax]  
    00B33C60  mov         dword ptr [x],ecx
x = **ppv;
    00B33C63  mov         eax,dword ptr [ppv]  
    00B33C66  mov         ecx,dword ptr [eax]  
    00B33C68  mov         edx,dword ptr [ecx]  
    00B33C6A  mov         dword ptr [x],edx

You can see that there is an additional mov instruction for dereferencing there so my best guess is: double dereferencing is inevitably slower.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top