Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Pass pointers or the things they point to

Reply
Thread Tools

Pass pointers or the things they point to

 
 
Blue Ocean
Guest
Posts: n/a
 
      07-10-2004
I know this is somewhat dependent on the circumstances, but let me ask
anyway. Suppose I have a 100 byte struct or array or something like
that. Which would be more efficient?

void function(struct something foo)
{
foo.this + foo.that;
foo.somethingoranother++;
printf(foo.foostring);
//other operations on the array
}

or

void function(string something *foo)
{
foo->this + foo->that;
foo->somethingoranother++;
printf(foo->foostring);
//other operations on the array
}

I guess the question I'm trying to ask is, how efficient is it to pass
mostly pointers except for primitive types? Is it a better idea to do
that or to pass by value for the most part?
 
Reply With Quote
 
 
 
 
Malcolm
Guest
Posts: n/a
 
      07-10-2004
"Blue Ocean" <(E-Mail Removed)> wrote in message
>
> I guess the question I'm trying to ask is, how efficient is it to pass
> mostly pointers except for primitive types? Is it a better idea to do
> that or to pass by value for the most part?
>

Pass a pointer (a const pointer if the structure doesn't need to be
modified). It is more efficient as you are probably passing a single
register rather than copying data to the stack, and it is more idiomatic - C
programmers expect to see struct arguments passed as pointers.


 
Reply With Quote
 
 
 
 
Jens.Toerring@physik.fu-berlin.de
Guest
Posts: n/a
 
      07-10-2004
Malcolm <(E-Mail Removed)> wrote:
> "Blue Ocean" <(E-Mail Removed)> wrote in message
>>
>> I guess the question I'm trying to ask is, how efficient is it to pass
>> mostly pointers except for primitive types? Is it a better idea to do
>> that or to pass by value for the most part?
>>

> Pass a pointer (a const pointer if the structure doesn't need to be
> modified). It is more efficient as you are probably passing a single
> register rather than copying data to the stack, and it is more idiomatic - C
> programmers expect to see struct arguments passed as pointers.


Well, I would guess it depends. If you need a copy of the structure
in the function anyway (it's not clear to me from the pseudo-code
what exactly "Blue Ocean" had in mind) it might be even at least as
efficient to pass the structure to the function instead of copying
it yourself. And since all accesses to the elements have to be done
by dereferencing the pointer (after adding an offset) it is not im-
possible that the compiler produces more efficient code when it can
already figure out at compile time where exactly the copy of the
structure is going to end up (e.g. at some fixed offset from the
stack pointer). Cache issues might also play some role...

I guess that passing just a pointer instead of the structure can
only be seen as "more efficient" in all possible cases when one
defines efficiency just as "burning less CPU cycles and doing
less memory accesses during the invocation of the function".
But when one looks at the efficiency of the whole function the
picture can become more complicated, even that complicated that
it's more or less impossible to answer it. Anyway, this is no
question that can be answered from a clean clc point of view,
too many details of the implementation are getting into it...
At least I wouldn't cringe at seeing a structure passed as such
to a function instead of just a pointer to it.

Regards, Jens
--
\ Jens Thoms Toerring ___ http://www.velocityreviews.com/forums/(E-Mail Removed)-berlin.de
\__________________________ http://www.toerring.de
 
Reply With Quote
 
jacob navia
Guest
Posts: n/a
 
      07-10-2004

"Blue Ocean" <(E-Mail Removed)> a écrit dans le message de
news:(E-Mail Removed) om...
> I know this is somewhat dependent on the circumstances, but let me ask
> anyway. Suppose I have a 100 byte struct or array or something like
> that. Which would be more efficient?
>
> void function(struct something foo)
> {
> foo.this + foo.that;
> foo.somethingoranother++;
> printf(foo.foostring);
> //other operations on the array
> }
>
> or
>
> void function(string something *foo)
> {
> foo->this + foo->that;
> foo->somethingoranother++;
> printf(foo->foostring);
> //other operations on the array
> }
>
> I guess the question I'm trying to ask is, how efficient is it to pass
> mostly pointers except for primitive types? Is it a better idea to do
> that or to pass by value for the most part?


Do you want to pass a copy of the data or the data itself?

In some situations is better to pass a copy to keep the
original data, and in some others it is better to pass
the data itself with a pointer to it.

This is dependent on the usage of the data and in the
whole structure of the application. There are no
general rules to know when one solution is better
than the other.

The machine doesn't do a copy when passing a pointer, hence
the operation takes less time. If you need a copy
however, it saves you the writing of code to make a copy
passing the structure by value.





 
Reply With Quote
 
Barry Schwarz
Guest
Posts: n/a
 
      07-10-2004
On 10 Jul 2004 12:36:20 -0700, http://www.velocityreviews.com/forums/(E-Mail Removed) (Blue Ocean)
wrote:

>I know this is somewhat dependent on the circumstances, but let me ask
>anyway. Suppose I have a 100 byte struct or array or something like
>that. Which would be more efficient?
>
>void function(struct something foo)
>{
> foo.this + foo.that;
> foo.somethingoranother++;
> printf(foo.foostring);
> //other operations on the array
>}
>
>or
>
>void function(string something *foo)
>{
> foo->this + foo->that;
> foo->somethingoranother++;
> printf(foo->foostring);
> //other operations on the array
>}
>
>I guess the question I'm trying to ask is, how efficient is it to pass
>mostly pointers except for primitive types? Is it a better idea to do
>that or to pass by value for the most part?


Since the two options are not equivalent, questions about efficiency
are irrelevant.

Do you want the original struct in the calling function to be modified
by the activities of function() or not. If the answer is yes, pass
the pointer. If the answer is no, pass the struct.

By the way, what array are you talking about in your comment? Did you
mean struct?


<<Remove the del for email>>
 
Reply With Quote
 
Aguilar, James
Guest
Posts: n/a
 
      07-11-2004
"Barry Schwarz" <(E-Mail Removed)> wrote in message
news:ccpsp7$38u$0@216.39.134.35...
> On 10 Jul 2004 12:36:20 -0700, (E-Mail Removed) (Blue Ocean)
> wrote:
>
> >I know this is somewhat dependent on the circumstances, but let me ask
> >anyway. Suppose I have a 100 byte struct or array or something like
> >that. Which would be more efficient?
> >
> >void function(struct something foo)
> >{
> > foo.this + foo.that;
> > foo.somethingoranother++;
> > printf(foo.foostring);
> > //other operations on the array
> >}
> >
> >or
> >
> >void function(string something *foo)
> >{
> > foo->this + foo->that;
> > foo->somethingoranother++;
> > printf(foo->foostring);
> > //other operations on the array
> >}
> >
> >I guess the question I'm trying to ask is, how efficient is it to pass
> >mostly pointers except for primitive types? Is it a better idea to do
> >that or to pass by value for the most part?

>
> By the way, what array are you talking about in your comment? Did you
> mean struct?


Forgive me for being confusing. I was originally going to talk about an
array, but then I changed my mind and used a struct.

Thanks for the help.


 
Reply With Quote
 
Malcolm
Guest
Posts: n/a
 
      07-11-2004

<(E-Mail Removed)-berlin.de> wrote in message
> Well, I would guess it depends.
>

You can't be absolutely sure about this type of implementation issue, of
course.
>
> If you need a copy of the structure
> in the function anyway (it's not clear to me from the pseudo-code
> what exactly "Blue Ocean" had in mind) it might be even at least as
> efficient to pass the structure to the function instead of copying
> it yourself.
>

This may be the case. But how often do you need a temporary copy of a struct
in real code? There are examples, of course, but not so many. And if
virtually all your functions are taking a struct employee *, it is far
neater to accept a small runtime penalty and make the one function that does
need a temporary copy also take a (const) struct employee *.
>
> And since all accesses to the elements have to be done
> by dereferencing the pointer (after adding an offset) it is not im-
> possible that the compiler produces more efficient code when it can
> already figure out at compile time where exactly the copy of the
> structure is going to end up (e.g. at some fixed offset from the
> stack pointer). Cache issues might also play some role...
>

This isn't impossible. However typically you will pass the structure pointer
in a register, and access will be via read/write through that register.
Access to the stack will be via the stack register. Unless the stack
register is privileged in some way, there won't be a difference. Also, of
course, it is very common to use only some members of the structure passed
in, in which case the pass by value approach causes needless copying.
>
> Anyway, this is no question that can be answered from a clean clc point
> of view, too many details of the implementation are getting into it...
> At least I wouldn't cringe at seeing a structure passed as such
> to a function instead of just a pointer to it.
>

I think what we need to focus on is that often passing the pointer is far
more efficient (eg an access function that just returns a member variable of
a big opaque structure), whilst occasionally the pass by value method may
offer slight improvements in efficiency.
However a consistent interface is to be preferred, and you also have the
problem of structures which contain pointers as members. Two copies of a
pointer is always asking for trouble.

So except in the case of a clear efficiency need to pass a small structure
on the stack, I would always go for passing the pointer.


 
Reply With Quote
 
Dan Pop
Guest
Posts: n/a
 
      07-12-2004
In <ccph6o$gbg$(E-Mail Removed)> "Malcolm" <(E-Mail Removed)> writes:

>"Blue Ocean" <(E-Mail Removed)> wrote in message
>>
>> I guess the question I'm trying to ask is, how efficient is it to pass
>> mostly pointers except for primitive types? Is it a better idea to do
>> that or to pass by value for the most part?
>>

>Pass a pointer (a const pointer if the structure doesn't need to be
>modified).


That would be a pointer to const, not a const pointer.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: (E-Mail Removed)
 
Reply With Quote
 
Dan Pop
Guest
Posts: n/a
 
      07-12-2004
In <(E-Mail Removed) > (E-Mail Removed) (Blue Ocean) writes:

>I know this is somewhat dependent on the circumstances, but let me ask
>anyway. Suppose I have a 100 byte struct or array or something like
>that. Which would be more efficient?
>
>void function(struct something foo)
>{
> foo.this + foo.that;
> foo.somethingoranother++;


You're changing the local copy. The caller's master copy remains
unchanged.

> printf(foo.foostring);
> //other operations on the array
>}
>
>or
>
>void function(struct something *foo)
>{
> foo->this + foo->that;
> foo->somethingoranother++;


You're changing the caller's struct.

> printf(foo->foostring);
> //other operations on the array
>}
>
>I guess the question I'm trying to ask is, how efficient is it to pass
>mostly pointers except for primitive types? Is it a better idea to do
>that or to pass by value for the most part?


It is more than somewhat dependent on the circumstances, it is *entirely*
dependent on the circumstances. The best answer can be obtained only
after a careful analysis of the circumstances.

The main things to consider are:

1. Do you want the local changes to be propagated back to the caller?

2. How much data is involved?

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: (E-Mail Removed)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
pointers to member functions - can they point to operators ? Stephen Howe C++ 3 12-02-2012 12:08 PM
Share-Point-2010 ,Share-Point -2010 Training , Share-point-2010Hyderabad , Share-point-2010 Institute Saraswati lakki ASP .Net 0 01-06-2012 06:39 AM
pointers, pointers, pointers... cerr C Programming 12 04-07-2011 11:17 PM
Scenario 5: IS-IS routing on Frame Relay Multi-point and Point-to-Point David Sudjiman Cisco 0 06-08-2006 09:11 AM
they turn, they power, they make nice pics Keith and Jenn Z. Digital Photography 0 09-21-2003 04:16 AM



Advertisments