Question:

How can pointers be faster?

by  |  earlier

0 LIKES UnLike

Everywhere it says "Pointers are fast". Simply coz it stores the address and not the value.

Ok.

Here's a counter example.

Lets say CPU has to fetch a value A of add FFFF.

1) Start.

2) It goes to linker

3) Asks the add of A.

4) Goes to FFFF add

5) Fetches The value.

6) Done

Now I use a pointer *ptr whose itself add is AAAA and stores add of A i.e. FFFF

1) Start.

2) It goes to linker

3) Asks the add of *ptr.

4) Goes to AAAA add

5) Finds The add of A

6) Goes to FFFF add

7) Fetches The value.

8) Done

NOTE :- In above i added two more level of execution.

So then how can pointers be faster????

Please explain in detail....

Thanks a lot....

 Tags:

   Report

3 ANSWERS


  1. Dear mayank,

    the both of above  programmes are same but using different approaches, in the first one you completed it in 6 steps while the second one in 8 steps.

    now just think in the first one you can access the variable A only once in whole program if you wish to redo this then how?

    but in case of pointers you just have to assign  only once the variable to its pointer(address referencing) , now its your wish whenever you want to access the variable you can do it by just writing one statement of pointer.

    so the 1st one is although shorter but limited scope, while the second one has its specific advantage. without using pointer your first one program can access the variable A but with too many instructions.

    hmmmmmmm...... lot of programming is there.


  2. Its very simple friend since the address is very well known there is no need to search for certain values and getting them added.

    for example think like this.. you are going to find a number from a list of array and add the number with another number which is in another array.  but this can be easily done when u use pointers since the value is directly fetched using the address thats y pointers were faster comparing to other techniques

  3. Passing by reference doesn't save much time if the variable in question is small (e.g. an int or a char). You pass a pointer/reference to save time when your function takes large objects/structs as parameters. If you try to pass the object/struct itself, then a copy of it has to be written onto the stack. If you pass a pointer to the object/struct, then only the address has to be copied, and the function can use the address to access the data with little to no extra overhead.

Question Stats

Latest activity: earlier.
This question has 3 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.