Solved

Pointers

Posted on 1999-01-04
11
304 Views
Last Modified: 2010-04-15
Hi,
       I know that by using pointers execution speed or performance of application increases.
I know that beacuse pointers holds the address of a variable
so it can directly access that memory location.But first it
has to search for that pointer variable which holds the address of other ordinary variable or pointer.Then how come effeciency increases.
0
Comment
Question by:manjucee
  • 2
  • 2
  • 2
  • +5
11 Comments
 
LVL 4

Accepted Solution

by:
jos010697 earned 50 total points
Comment Utility
Nobody (or nothing) performs a 'search for that pointer'. C code is compiled by
a compiler; it treats a pointer variable as any other variable, i.e. it allocates
an (data) address for it, somewhere in memory. All references to that pointer
variable are directly compiled into a reference to this allocated data address.

BTW, using pointers does not necessarily increase execution speed; it all
depends on the compiler implementation and the bare metal hardware your
code is running on.

kind regards,

Jos aka jos@and.nl
0
 
LVL 10

Expert Comment

by:rbr
Comment Utility
You should post your problem and your code to see if pointers will increase the speed.
0
 
LVL 11

Expert Comment

by:alexo
Comment Utility
>> I know that by using pointers execution speed or performance of application increases.
Somebody supplied you with incorrect information.  Using pointers does not speed up an application (in fact, it can slow it down due to the indirection involved).
0
 
LVL 8

Expert Comment

by:Answers2000
Comment Utility
>> I know that by using pointers execution speed or performance of application increases.

Depends on what you compare them to, which compiler etc.

e.g.
void A( SomeStruct * pParam )
and
void B( SomeStruct Param )

function A is probably faster (the pointer version) under most compilers assume SomeStruct is a reasonably size struct.


But

int x ;
int * y = &x ;
x = 5 ; /* Line 1 */
*y = 5 ; /* Linre 2 */

Line 1 (non-pointer) is probably faster than line 2 (pointer version)

0
 
LVL 51

Expert Comment

by:ahoffmann
Comment Utility
Answers2000,

> void B( SomeStruct Param )
depending on the size of Param, it may be much faster on SPARCs than using a pointer ;-)

> Line 1 (non-pointer) is probably faster than line 2 (pointer version)
*probably*, just if you have a useless optimizer
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 8

Expert Comment

by:Answers2000
Comment Utility
ahoffman - Like I said

>> Depends on what you compare them to, which compiler etc.
(and you can add CPU, etc)
0
 
LVL 4

Expert Comment

by:jos010697
Comment Utility
Just FYI ... I scavenged some old newsgroup articles and found an article
I wrote once in response to someone who claimed that 'pointers are faster',
so one should use pointers instead of, say, array indices etc.

In this article I argued that compilers are very well capable, because they
'know' what hardware they're running on, optimizing the source code, written
without all those cryptic pointer arithmetic trickery-dickery.

I'll try to copy and paste the article in this nasty little window, hang on ...
--------------------------------------------------------------------------------------------------------

   In article <3l6hio$nr8@solutions.solon.com> "Ronald F. Guilmette" <rfg@rahul.net> writes:

   |The biggest mistake that novices make is overusing [].  It is rarely needed.
   |Code which uses [] can almost always be recast into code which uses pointers,
   |and whenever you are simply stepping linearly through an array, the formula-
   |tion which uses pointers instead of subscripting is almost always faster.

   I read your article explaining your guidelines, but I beg to differ. I don't
   really consider myself a novice (C and I had a love/hate relationship way
   back in '79 ;-) but if I have to deal with an indexed set of objects, this
   set simply screams for simple indexes like a[i], a[i+1], ... a[n].

   Even the silliest compilers nowadays, compilers that can hardly spell the
   verb `optimize', know how to deal with the '[]` operator efficiently. Allow
   me to contract two of your guidelines into one example:

           void foo(int *a, int *b, int n) {

                   int i;
                   int x, y;

                   x= some_value();
                   y= some_other_value();

                   for (i= 0; i < n; i++) {
                           bar((x+y)/a[i]);
                           baz((x+y)/b[i]);
                   }
           }

   I'm using the expression `x+y' twice here as well as simple array indexes.
   One of your guidelines states that the expression `x+y' should be replaced
   by a `int sum= x+y' and use `sum' instead. Most (if not all) compilers are
   able to detect common sub-expression and they will modify the code
   accordingly:

           void foo(int *a, int *b, int n) {

                   int i;
                   int x, y;

                   x= some_value();
                   y= some_other_value();

                   for (i= 0; i < n; i++) {
                           int temp= x+y;
                           bar(temp/a[i]);
                           baz(temp/b[i]);
                   }
           }

   The expression `temp= x+y' is invariant to the loop, so it can be lifted
   upwards, i.e. out of the loop:

           void foo(int *a, int *b, int n) {

                   int i;
                   int x, y;
                   int temp;

                   x= some_value();
                   y= some_other_value();

                   temp= x+y;

                   for (i= 0; i < n; i++) {
                           bar(temp/a[i]);
                           baz(temp/b[i]);
                   }
           }

   The two assignments to variables `x' and `y' are so called 'dead store'
   operations, i.e. the value of `x' and `y' are never used further on in
   the code, so they can be eliminated (note that this is all compiler
   internal stuff, we're talking here):

           void foo(int *a, int *b, int n) {

                   int i;
                   int temp= some_value()+some_other_value();

                   for (i= 0; i < n; i++) {
                           bar(temp/a[i]);
                           baz(temp/b[i]);
                   }
           }

   Back to those `[]' operators. Internally, the compiler generates something
   like this:

           void foo(int *a, int *b, int n) {

                   int i;
                   int temp= some_value()+some_other_value();

                   for (i= 0; i < n; i++) {
                           bar(temp/(*(((char*)a)+sizeof(int)*i)));
                           baz(temp/(*(((char*)b)+sizeof(int)*i)));
                   }
           }

   There's two ways the compiler can optimize things here: `sizeof(int)*i'
   happens to be a common sub-expression in the body of the loop, so a
   very near sighted compiler could generate something like this:

          void foo(int *a, int *b, int n) {

                   int i;
                   int temp= some_value()+some_other_value();

                   for (i= 0; i < n; i++) {
                           int temp2= sizeof(int)*i;
                           bar(temp/(*(((char*)a)+temp2)));
                           baz(temp/(*(((char*)b)+temp2)));
                   }
           }

   But there's more a compiler can do here; this is where strength reduction
   comes in. Here the multiplication is the target of the compilers' optimizer:
   The value of variable `temp2' is dependent on the value of the loop counter,
   so, it easily be replaced by:

           void foo(int *a, int *b, int n) {

                   int i;
                   int temp= some_value()+some_other_value();
                   int temp2;

                   temp2= 0;
                   for (i= 0; i < n; i++) {
                           temp2+= sizeof(int);
                           bar(temp/(*(((char*)a)+temp2)));
                           baz(temp/(*(((char*)b)+temp2)));
                   }
           }

   Propagating the value of temp2 further down the DAG to variables `a' and `b'
   eliminates the need for variable `temp2' and yields the following code:

           void foo(int *a, int *b, int n) {

                   int i;
                   int temp= some_value()+some_other_value();

                   for (i= 0; i < n; i++) {
                           bar(temp/ *a);
                           baz(temp/ *b);
                           (*((char**)&a))+= sizeof(int);
                           (*((char**)&b))+= sizeof(int);
                   }
           }

   (note that I had to add a space chracter following the '/' division
   operator, for not so obvious reasons ... ;-)

   Of course those last two assignments in the body of the loop are equivalent
   to the simple `a++' and `b++' alternatives. Note that variable `i' is just
   used for simple counting stuff and that variable `temp' is just used to
   store one of my precious, carefully computed values. The compiler might
   decide to store those in a register:

           void foo(int *a, int *b, int n) {

                   register int i;
                   register int temp= some_value()+some_other_value();

                   for (i= 0; i < n; i++) {
                           bar(temp/ *a);
                           baz(temp/ *b);
                           a++;
                           b++;
                   }
           }

   Have a look at this version and at the original version of function `foo'.
   The latest version is the one your guidelines recommend, the first version
   was mine. My rule of thumb is (I've got zillions of them ;-) to leave the
   dirty work to the compiler, which happens to be very capable of trans-
   forming the first version into the last) and let me and you do all the
   fun stuff (which happens to be more readable, at least to me that is).
   Your guidelines are not applicable to programmers, they're more like the
   Ten Commandments to compiler builders ...

   (I apologize if I overstated this a bit).

   kind regards,

   Jos aka jos@and.nl
~


0
 

Author Comment

by:manjucee
Comment Utility
Hi jos,
  Thanks for infomation. u clarified all my doubts by adding comments.Really its very informative.Thanks a lot.
Regards
Manju
0
 

Expert Comment

by:rsongyl
Comment Utility
Basically.... the address is specified ... and so .. finding it will be simple. And thus it increase efficiency
0
 

Expert Comment

by:pinki
Comment Utility
Pointers found their importance at those points of application  where delegation of responsibilty demanded giving full access
to the object.( basis for 'refernce' as for as c++ is concerned )

It's just like saying here is my house key ,anybody wanting to access householhd items or whatever.. can take this key.

But before giving key make sure that right person( right function) gets it,otherwise you will have to repent!

Pointers increase efficiency is partially true , since it again
depends on the size of the object.Keep it in mind that pointers
have got their overheades in terms of accessing of objects ( because compiler has to sweat it out for the de-reference ..,may be one more addtion or multiplication instructions ..)

Overhead of using a pointer is evident if you can go through the
code complier has to generate ,when
 
      *  a pointer is made to point to an array of simple
         built in data type and indivizual elements are tried
         to access in terms of array syntax using [ ] operator.

against

the code compiler has to generate for the tradidtional array style of accessing the indivizual elements.      
0
 

Author Comment

by:manjucee
Comment Utility
Hi pinki,
                 Thanks for the additional information.Really it helped me lot.
Manju
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now