473,395 Members | 1,679 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Writing Efficient Arguments

Pointer and reference arguments provide C++ programmers with the
ability to modify objects. What is more efficient, passing arguments
via pointer or reference? Avoid the stereotypical urge of debating
effective coding style. Instead, think of particular scenarios:
passing a 512MB object to function. Think behind the scenes, from the
stack and heap to the physical linking and build of the program.

Jul 9 '07 #1
35 1759
sp*******@gmail.com wrote:
Pointer and reference arguments provide C++ programmers with the
ability to modify objects. What is more efficient, passing arguments
via pointer or reference? Avoid the stereotypical urge of debating
effective coding style. Instead, think of particular scenarios:
passing a 512MB object to function. Think behind the scenes, from the
stack and heap to the physical linking and build of the program.

No way to tell. It's all platform-specific. If the implementation uses
pointers behind the scenes, there's likely little difference.

If it's important, profile. If not, then use the construct that most
clearly represents the intent of the program.

Brian
Jul 9 '07 #2
sp*******@gmail.com wrote:
Pointer and reference arguments provide C++ programmers with the
ability to modify objects. What is more efficient, passing arguments
via pointer or reference? Avoid the stereotypical urge of debating
effective coding style. Instead, think of particular scenarios:
passing a 512MB object to function. Think behind the scenes, from the
stack and heap to the physical linking and build of the program.
I propose that we ban the word "efficient" from newsgroup posts unless
the poster has benchmarked and shown that the micro-optimization he
wants actually may do something.
Jul 9 '07 #3
On Jul 9, 7:03 pm, spekyu...@gmail.com wrote:
Pointer and reference arguments provide C++ programmers with the
ability to modify objects. What is more efficient, passing arguments
via pointer or reference? Avoid the stereotypical urge of debating
effective coding style. Instead, think of particular scenarios:
passing a 512MB object to function. Think behind the scenes, from the
stack and heap to the physical linking and build of the program.
>From memory standpoint there is no difference. Assuming the address of
the reference and pointer are coming from the same location, I don't
believe there are additional instructions to set up the stack;
however, as soon as I say that, someone will cite some implementation
where it does.

The 'efficiency' is in the recipient. That is, it does not have to
check if a reference is null - since a reference can never be null.
Finally, I suspect if you declare a pointer parameter as 'void*', you
lose type-checking efficiency, or incur type-checking inefficiency
depending on your point-of-view.

Jul 10 '07 #4
On Jul 9, 7:22 pm, red floyd <no.s...@here.dudewrote:
spekyu...@gmail.com wrote:
Pointer and reference arguments provide C++ programmers with the
ability to modify objects. What is more efficient, passing arguments
via pointer or reference? Avoid the stereotypical urge of debating
effective coding style. Instead, think of particular scenarios:
passing a 512MB object to function. Think behind the scenes, from the
stack and heap to the physical linking and build of the program.

I propose that we ban the word "efficient" from newsgroup posts unless
the poster has benchmarked and shown that the micro-optimization he
wants actually may do something.
Alright, these methods perform the same exact tasks. In each case,
what will the cost in overhead be? Both implementations refer to an
object indirectly, which allows each method to make internal
modifications. Assume the object is referring to intelligence
photography or uncompressed video. Compare this with the an
inefficient implementation, what background details can you construct?

// SIZEOF(pObject) =4 bytes
// SIZEOF(pObject) =500 * 2^20 bytes
void function(myObject* pObject)
{
...
}

// SIZEOF(refObject) =500 * 2^20 bytes
void function(myObject& refObject)
{
...
}

// Inefficient Implementation
myObject function(myObject copy)
{
...
return copy;
}

Jul 10 '07 #5
On Jul 9, 7:11 pm, "Default User" <defaultuse...@yahoo.comwrote:
spekyu...@gmail.com wrote:
Pointer and reference arguments provide C++ programmers with the
ability to modify objects. What is more efficient, passing arguments
via pointer or reference? Avoid the stereotypical urge of debating
effective coding style. Instead, think of particular scenarios:
passing a 512MB object to function. Think behind the scenes, from the
stack and heap to the physical linking and build of the program.

No way to tell. It's all platform-specific. If the implementation uses
pointers behind the scenes, there's likely little difference.

If it's important, profile. If not, then use the construct that most
clearly represents the intent of the program.

Brian
Are you suggesting this is compiler specific?

Jul 10 '07 #6
On Jul 9, 8:06 pm, gpuchtel <gpuch...@gmail.comwrote:
On Jul 9, 7:03 pm, spekyu...@gmail.com wrote:
Pointer and reference arguments provide C++ programmers with the
ability to modify objects. What is more efficient, passing arguments
via pointer or reference? Avoid the stereotypical urge of debating
effective coding style. Instead, think of particular scenarios:
passing a 512MB object to function. Think behind the scenes, from the
stack and heap to the physical linking and build of the program.
From memory standpoint there is no difference. Assuming the address of

the reference and pointer are coming from the same location, I don't
believe there are additional instructions to set up the stack;
however, as soon as I say that, someone will cite some implementation
where it does.

The 'efficiency' is in the recipient. That is, it does not have to
check if a reference is null - since a reference can never be null.
Finally, I suspect if you declare a pointer parameter as 'void*', you
lose type-checking efficiency, or incur type-checking inefficiency
depending on your point-of-view.
Alright, these methods perform the same exact tasks. In each case,
what will the cost in overhead be? Both implementations refer to an
object indirectly, which allows each method to make internal
modifications. Assume the object is referring to intelligence
photography or uncompressed video. Compare this with the an
inefficient implementation, what background details can you construct?

// SIZEOF(pObject) =4 bytes
// SIZEOF(pObject) =500 * 2^20 bytes
void function(myObject* pObject)
{
...
}

// SIZEOF(refObject) =500 * 2^20 bytes
void function(myObject& refObject)
{
...
}

// Inefficient Implementation
myObject function(myObject copy)
{
...
return copy;
}

Jul 10 '07 #7
sp*******@gmail.com wrote:
Pointer and reference arguments provide C++ programmers with the
ability to modify objects. What is more efficient, passing arguments
via pointer or reference? Avoid the stereotypical urge of debating
effective coding style. Instead, think of particular scenarios:
passing a 512MB object to function. Think behind the scenes, from the
stack and heap to the physical linking and build of the program.
Passing by reference automatically assumes only one object, whereas a
pointer may be to part of an array.
void someFnc1(MyObj &inp)
{
inp.doSomething();
inp[1].doSomething(); // This is plain wrong
}

void someFnc2(MyObj *inp)
{
inp->doSomething();
inp[1].doSomething(); // This is valid but maybe wrong and could
cause hard to debug problems
}

As for efficiency, I doubt with modern optimising compilers you would
find one more efficient than the other.

JB
Jul 10 '07 #8
On Jul 10, 1:22 am, red floyd <no.s...@here.dudewrote:
spekyu...@gmail.com wrote:
Pointer and reference arguments provide C++ programmers with the
ability to modify objects. What is more efficient, passing arguments
via pointer or reference? Avoid the stereotypical urge of debating
effective coding style. Instead, think of particular scenarios:
passing a 512MB object to function. Think behind the scenes, from the
stack and heap to the physical linking and build of the program.
I propose that we ban the word "efficient" from newsgroup posts unless
the poster has benchmarked and shown that the micro-optimization he
wants actually may do something.
I totally disagree. Programmer efficiency is very important,
and should only be compromized when absolutely essential. (Most
of the time, of course, we're really concerned with long term
programmer efficiency. We'll invest some additional effort up
front to make the inevitable maintenance easier.)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jul 10 '07 #9
gpuchtel a écrit :
On Jul 9, 7:03 pm, spekyu...@gmail.com wrote:
>Pointer and reference arguments provide C++ programmers with the
ability to modify objects. What is more efficient, passing arguments
via pointer or reference? Avoid the stereotypical urge of debating
effective coding style. Instead, think of particular scenarios:
passing a 512MB object to function. Think behind the scenes, from the
stack and heap to the physical linking and build of the program.
[snip]
The 'efficiency' is in the recipient. That is, it does not have to
check if a reference is null - since a reference can never be null.
A reference can be NULL or invalid all the same. The difference is that
the code indicates a NULL wouldn't be expected (i.e. the code is self
explaining; whereas with pointer, you would have to read the doc/comments).

AFAIK the following code is valid (though UB):

void print(int& i)
{
cout<<&i<<endl;
}

int main()
{
print(*static_cast<int*>(0));

return 0;
}

And prints '0'.

Michael
Jul 10 '07 #10
On Jul 10, 6:41 am, Michael DOUBEZ <michael.dou...@free.frwrote:
>
AFAIK the following code is valid (though UB):

void print(int& i)
{
cout<<&i<<endl;

}

int main()
{
print(*static_cast<int*>(0));

return 0;

}

And prints '0'.

Michael
A reference cannot be 'null'. What you did created a tempory int
variable that was initialized to 0 (zero), then a reference to that
temporary variable was passed to the print function, which in turn
printed '0'.

Jul 10 '07 #11
On 2007-07-10 05:22, sp*******@gmail.com wrote:
On Jul 9, 8:06 pm, gpuchtel <gpuch...@gmail.comwrote:
>On Jul 9, 7:03 pm, spekyu...@gmail.com wrote:
Pointer and reference arguments provide C++ programmers with the
ability to modify objects. What is more efficient, passing arguments
via pointer or reference? Avoid the stereotypical urge of debating
effective coding style. Instead, think of particular scenarios:
passing a 512MB object to function. Think behind the scenes, from the
stack and heap to the physical linking and build of the program.
From memory standpoint there is no difference. Assuming the address of

the reference and pointer are coming from the same location, I don't
believe there are additional instructions to set up the stack;
however, as soon as I say that, someone will cite some implementation
where it does.

The 'efficiency' is in the recipient. That is, it does not have to
check if a reference is null - since a reference can never be null.
Finally, I suspect if you declare a pointer parameter as 'void*', you
lose type-checking efficiency, or incur type-checking inefficiency
depending on your point-of-view.

Alright, these methods perform the same exact tasks. In each case,
what will the cost in overhead be? Both implementations refer to an
object indirectly, which allows each method to make internal
modifications. Assume the object is referring to intelligence
photography or uncompressed video. Compare this with the an
inefficient implementation, what background details can you construct?

// SIZEOF(pObject) =4 bytes
I assume you meant SIZEOF(myObject*)
// SIZEOF(pObject) =500 * 2^20 bytes
SIZEOF(myObject), by the way, what's wrong with sizeof()?
void function(myObject* pObject)
{
...
}

// SIZEOF(refObject) =500 * 2^20 bytes
void function(myObject& refObject)
{
...
}
If myObject is that large it probably means that function() won't be
called to often, so whatever the overhead of the call is, it will be
insignificant to the total running time of the program, you should focus
your energy on trying to optimise the algorithm that works on the data
instead.

I know this might not be the answer you want, but as others have pointed
out there is no one true answer since it depends on a lot of things,
such as how the compiler implements references, what other optimizations
can be made (inlining etc.) and so on.

Write your program with as clear code as possible, then run it with some
representative test data, if it is to slow then profile it and improve
on the slowest parts (and I can guarantee you, unless you have a very
strange design, it will not be the call overhead). Doing anything else
will just waste your time.

--
Erik Wikström
Jul 10 '07 #12
gpuchtel a écrit :
On Jul 10, 6:41 am, Michael DOUBEZ <michael.dou...@free.frwrote:
>AFAIK the following code is valid (though UB):

void print(int& i)
{
cout<<&i<<endl;

}

int main()
{
print(*static_cast<int*>(0));

return 0;

}

And prints '0'.
>
A reference cannot be 'null'. What you did created a tempory int
variable that was initialized to 0 (zero), then a reference to that
temporary variable was passed to the print function, which in turn
printed '0'.
Where did you get that from ? Even if I wanted it, I couldn't pass a
temporary because the ref is not const.

It is perhaps more understandable if you replace by:
int *ptr=NULL;
print(*ptr)

and add in print
++i; //cause segfault;

Michael
Jul 10 '07 #13
On Jul 10, 7:11 am, Erik Wikström <Erik-wikst...@telia.comwrote:
On 2007-07-10 05:22, spekyu...@gmail.com wrote:


On Jul 9, 8:06 pm, gpuchtel <gpuch...@gmail.comwrote:
On Jul 9, 7:03 pm, spekyu...@gmail.com wrote:
Pointer and reference arguments provide C++ programmers with the
ability to modify objects. What is more efficient, passing arguments
via pointer or reference? Avoid the stereotypical urge of debating
effective coding style. Instead, think of particular scenarios:
passing a 512MB object to function. Think behind the scenes, from the
stack and heap to the physical linking and build of the program.
From memory standpoint there is no difference. Assuming the address of
the reference and pointer are coming from the same location, I don't
believe there are additional instructions to set up the stack;
however, as soon as I say that, someone will cite some implementation
where it does.
The 'efficiency' is in the recipient. That is, it does not have to
check if a reference is null - since a reference can never be null.
Finally, I suspect if you declare a pointer parameter as 'void*', you
lose type-checking efficiency, or incur type-checking inefficiency
depending on your point-of-view.
Alright, these methods perform the same exact tasks. In each case,
what will the cost in overhead be? Both implementations refer to an
object indirectly, which allows each method to make internal
modifications. Assume the object is referring to intelligence
photography or uncompressed video. Compare this with the an
inefficient implementation, what background details can you construct?
// SIZEOF(pObject) =4 bytes

I assume you meant SIZEOF(myObject*)
// SIZEOF(pObject) =500 * 2^20 bytes

SIZEOF(myObject), by the way, what's wrong with sizeof()?
void function(myObject* pObject)
{
...
}
// SIZEOF(refObject) =500 * 2^20 bytes
void function(myObject& refObject)
{
...
}

If myObject is that large it probably means that function() won't be
called to often, so whatever the overhead of the call is, it will be
insignificant to the total running time of the program, you should focus
your energy on trying to optimise the algorithm that works on the data
instead.

I know this might not be the answer you want, but as others have pointed
out there is no one true answer since it depends on a lot of things,
such as how the compiler implements references, what other optimizations
can be made (inlining etc.) and so on.

Write your program with as clear code as possible, then run it with some
representative test data, if it is to slow then profile it and improve
on the slowest parts (and I can guarantee you, unless you have a very
strange design, it will not be the call overhead). Doing anything else
will just waste your time.

--
Erik Wikström- Hide quoted text -

- Show quoted text -
In research and development, writing code correctly the first time is
the objective my friend. Prematurely optimizing code and handling data
correctly are two completely different things. Ignorance in favor of
production, is the difference between an easily replacable gear and
computer scientist.

Jul 10 '07 #14
sp*******@gmail.com wrote:
On Jul 9, 7:22 pm, red floyd <no.s...@here.dudewrote:
>spekyu...@gmail.com wrote:
>>Pointer and reference arguments provide C++ programmers with the
ability to modify objects. What is more efficient, passing arguments
via pointer or reference? Avoid the stereotypical urge of debating
effective coding style. Instead, think of particular scenarios:
passing a 512MB object to function. Think behind the scenes, from
the stack and heap to the physical linking and build of the program.

I propose that we ban the word "efficient" from newsgroup posts
unless the poster has benchmarked and shown that the
micro-optimization he wants actually may do something.

Alright, these methods perform the same exact tasks. In each case,
what will the cost in overhead be?
The third method requires making _at least one_ copy. The overhead
of actually *calling* the third method will be the time of making the
copy of a 'myObject' object. What that is depends on the implementation
(of both the compiler and the 'myObject' class).
Both implementations refer to an
object indirectly, which allows each method to make internal
modifications. Assume the object is referring to intelligence
photography or uncompressed video.
Why does it matter what the object is "referring to"?
Compare this with the an
inefficient implementation, what background details can you construct?
What do you mean by "background details"?
>
// SIZEOF(pObject) =4 bytes
// SIZEOF(pObject) =500 * 2^20 bytes
void function(myObject* pObject)
{
...
}

// SIZEOF(refObject) =500 * 2^20 bytes
void function(myObject& refObject)
{
...
}

// Inefficient Implementation
myObject function(myObject copy)
{
...
return copy;
}
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 10 '07 #15
On Jul 10, 8:40 am, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
spekyu...@gmail.com wrote:
On Jul 9, 7:22 pm, red floyd <no.s...@here.dudewrote:
spekyu...@gmail.com wrote:
Pointer and reference arguments provide C++ programmers with the
ability to modify objects. What is more efficient, passing arguments
via pointer or reference? Avoid the stereotypical urge of debating
effective coding style. Instead, think of particular scenarios:
passing a 512MB object to function. Think behind the scenes, from
the stack and heap to the physical linking and build of the program.
I propose that we ban the word "efficient" from newsgroup posts
unless the poster has benchmarked and shown that the
micro-optimization he wants actually may do something.
Alright, these methods perform the same exact tasks. In each case,
what will the cost in overhead be?

The third method requires making _at least one_ copy. The overhead
of actually *calling* the third method will be the time of making the
copy of a 'myObject' object. What that is depends on the implementation
(of both the compiler and the 'myObject' class).
Both implementations refer to an
object indirectly, which allows each method to make internal
modifications. Assume the object is referring to intelligence
photography or uncompressed video.

Why does it matter what the object is "referring to"?
Compare this with the an
inefficient implementation, what background details can you construct?

What do you mean by "background details"?


// SIZEOF(pObject) =4 bytes
// SIZEOF(pObject) =500 * 2^20 bytes
void function(myObject* pObject)
{
...
}
// SIZEOF(refObject) =500 * 2^20 bytes
void function(myObject& refObject)
{
...
}
// Inefficient Implementation
myObject function(myObject copy)
{
...
return copy;
}

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask- Hide quoted text -

- Show quoted text -- Hide quoted text -

- Show quoted text -
Background Details =What processes are occurring during a function
call? How are pointers and references treated during this time? What
exactly happens to their memory and what they reference during this
particular time? You know, the good stuff...

Jul 10 '07 #16
On Jul 10, 8:40 am, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
spekyu...@gmail.com wrote:
On Jul 9, 7:22 pm, red floyd <no.s...@here.dudewrote:
spekyu...@gmail.com wrote:
Pointer and reference arguments provide C++ programmers with the
ability to modify objects. What is more efficient, passing arguments
via pointer or reference? Avoid the stereotypical urge of debating
effective coding style. Instead, think of particular scenarios:
passing a 512MB object to function. Think behind the scenes, from
the stack and heap to the physical linking and build of the program.
I propose that we ban the word "efficient" from newsgroup posts
unless the poster has benchmarked and shown that the
micro-optimization he wants actually may do something.
Alright, these methods perform the same exact tasks. In each case,
what will the cost in overhead be?

The third method requires making _at least one_ copy. The overhead
of actually *calling* the third method will be the time of making the
copy of a 'myObject' object. What that is depends on the implementation
(of both the compiler and the 'myObject' class).
Both implementations refer to an
object indirectly, which allows each method to make internal
modifications. Assume the object is referring to intelligence
photography or uncompressed video.

Why does it matter what the object is "referring to"?
Compare this with the an
inefficient implementation, what background details can you construct?

What do you mean by "background details"?


// SIZEOF(pObject) =4 bytes
// SIZEOF(pObject) =500 * 2^20 bytes
void function(myObject* pObject)
{
...
}
// SIZEOF(refObject) =500 * 2^20 bytes
void function(myObject& refObject)
{
...
}
// Inefficient Implementation
myObject function(myObject copy)
{
...
return copy;
}

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask- Hide quoted text -

- Show quoted text -- Hide quoted text -

- Show quoted text -
Haha, I must complement your question Victor Bazarov. I included what
the object was referring to for people without imaginations. ;-)

Jul 10 '07 #17
On Jul 10, 8:13 am, Michael DOUBEZ <michael.dou...@free.frwrote:
gpuchtel a écrit :


On Jul 10, 6:41 am, Michael DOUBEZ <michael.dou...@free.frwrote:
AFAIK the following code is valid (though UB):
void print(int& i)
{
cout<<&i<<endl;
}
int main()
{
print(*static_cast<int*>(0));
return 0;
}
And prints '0'.
A reference cannot be 'null'. What you did created a tempory int
variable that was initialized to 0 (zero), then a reference to that
temporary variable was passed to the print function, which in turn
printed '0'.

Where did you get that from ? Even if I wanted it, I couldn't pass a
temporary because the ref is not const.

It is perhaps more understandable if you replace by:
int *ptr=NULL;
print(*ptr)

and add in print
++i; //cause segfault;

Michael- Hide quoted text -

- Show quoted text -
I didn't say (or mean) 'you' would pass a temporary, I was implying
what the compiler will do (pass). Simply stated, there is no such
thing as a 'null' reference. All your example does is to create a
reference to a (tempory) int value that contains the value of zero.
Your example of a null pointer is not relevant to the discussion of
the (im)possibility of having a 'null' reference.

Jul 10 '07 #18
gpuchtel wrote:
On Jul 10, 8:13 am, Michael DOUBEZ <michael.dou...@free.frwrote:
>Where did you get that from ? Even if I wanted it, I couldn't pass a
temporary because the ref is not const.

It is perhaps more understandable if you replace by:
int *ptr=NULL;
print(*ptr)

and add in print
++i; //cause segfault;

Michael- Hide quoted text -

- Show quoted text -

I didn't say (or mean) 'you' would pass a temporary, I was implying
what the compiler will do (pass). Simply stated, there is no such
thing as a 'null' reference. All your example does is to create a
reference to a (tempory) int value that contains the value of zero.
Your example of a null pointer is not relevant to the discussion of
the (im)possibility of having a 'null' reference.
You are wrong. The expression
static_cast<int*>(0)
or whatever you saw in Michael's code does not create a temporary integer.

It casts a null pointer constant into the type int*. He (Michael)
dereferenced the null pointer, passing it to a int reference.

The point is, you can have a 'null' reference, but you can't have one
without invoking undefined behaviour.

--
Thomas
http://www.netmeister.org/news/learn2quote.html
Jul 10 '07 #19
gpuchtel wrote:
On Jul 10, 8:13 am, Michael DOUBEZ <michael.dou...@free.frwrote:
>gpuchtel a écrit :


>>On Jul 10, 6:41 am, Michael DOUBEZ <michael.dou...@free.frwrote:
>>>AFAIK the following code is valid (though UB):
>>>void print(int& i)
{
cout<<&i<<endl;
>>>}
>>>int main()
{
print(*static_cast<int*>(0));
>>> return 0;
>>>}
>>>And prints '0'.
>>A reference cannot be 'null'. What you did created a tempory int
variable that was initialized to 0 (zero), then a reference to that
temporary variable was passed to the print function, which in turn
printed '0'.

Where did you get that from ? Even if I wanted it, I couldn't pass a
temporary because the ref is not const.

It is perhaps more understandable if you replace by:
int *ptr=NULL;
print(*ptr)

and add in print
++i; //cause segfault;

Michael- Hide quoted text -

- Show quoted text -

I didn't say (or mean) 'you' would pass a temporary, I was implying
what the compiler will do (pass). Simply stated, there is no such
thing as a 'null' reference. All your example does is to create a
reference to a (tempory) int value that contains the value of zero.
No temporary of type 'int' exists (or created) in the expression

static_cast<int*>(0)

The compiler should see the _integer_literal_ 0 and the static_cast
to a pointer type, and should create a *null pointer value* (of type
'int*', a pointer to int). It's all done in compile time.
Your example of a null pointer is not relevant to the discussion of
the (im)possibility of having a 'null' reference.
Why isn't it? As shown, the example is essentially the same as

...
int *p = 0;
print(*p); // no static cast needed
...

IIRC, most recent proposed corrections to the Standard actually allow
creation of a "null reference" provided that the contents of the
referred object are never evaluated (no lvalue-to-rvalue conversion
is performed). Taking address of that reference (using the operator
& on it) should yield a null pointer.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 10 '07 #20
sp*******@gmail.com wrote:
On Jul 9, 7:11 pm, "Default User" <defaultuse...@yahoo.comwrote:
spekyu...@gmail.com wrote:
Pointer and reference arguments provide C++ programmers with the
ability to modify objects. What is more efficient, passing
arguments via pointer or reference? Avoid the stereotypical urge
of debating effective coding style. Instead, think of particular
scenarios: passing a 512MB object to function. Think behind the
scenes, from the stack and heap to the physical linking and build
of the program.
No way to tell. It's all platform-specific. If the implementation
uses pointers behind the scenes, there's likely little difference.

If it's important, profile. If not, then use the construct that most
clearly represents the intent of the program.
Are you suggesting this is compiler specific?
I'm not suggesting that. I'm stating that. Of course it is, how could
it be anything else? The Standard specifies no performance
characteristics for either.


Brian
Jul 10 '07 #21
sp*******@gmail.com wrote:
:: On Jul 9, 7:22 pm, red floyd <no.s...@here.dudewrote:
::: spekyu...@gmail.com wrote:
:::: Pointer and reference arguments provide C++ programmers with the
:::: ability to modify objects. What is more efficient, passing
:::: arguments via pointer or reference? Avoid the stereotypical urge
:::: of debating effective coding style. Instead, think of particular
:::: scenarios: passing a 512MB object to function. Think behind the
:::: scenes, from the stack and heap to the physical linking and
:::: build of the program.
:::
::: I propose that we ban the word "efficient" from newsgroup posts
::: unless the poster has benchmarked and shown that the
::: micro-optimization he wants actually may do something.
::
:: Alright, these methods perform the same exact tasks. In each case,
:: what will the cost in overhead be? Both implementations refer to an
:: object indirectly, which allows each method to make internal
:: modifications. Assume the object is referring to intelligence
:: photography or uncompressed video. Compare this with the an
:: inefficient implementation, what background details can you
:: construct?
::
:: // SIZEOF(pObject) =4 bytes
:: // SIZEOF(pObject) =500 * 2^20 bytes
:: void function(myObject* pObject)
:: {
:: ...
:: }
::
:: // SIZEOF(refObject) =500 * 2^20 bytes
:: void function(myObject& refObject)

I think you have a problem here - sizeof(refObject) returns the size
of the object, not of the reference. A reference is just an alias for
an object. Anything you do to the reference actually happens to the
object it refers to.
:: {
:: ...
:: }
::
:: // Inefficient Implementation
:: myObject function(myObject copy)
:: {
:: ...
:: return copy;
:: }

This might be very efficient, if you need to return a modified object
without affecting the original.
Bo Persson
Jul 10 '07 #22
On Jul 10, 11:24 am, "Thomas J. Gritzan" <Phygon_ANTIS...@gmx.de>
wrote:
>
You are wrong. The expression
static_cast<int*>(0)
or whatever you saw in Michael's code does not create a temporary integer.
The syntax you quoted is neither the original syntax nor the context
in which I wrote my response. The original syntax and context was:

print(*static_cast<int*>(0));

not:

static_cast<int*>(0)

I may be wrong (I have been wrong before; however I'm still
verifying), but what I am saying is that in the construction of the
call to 'print', a temporary variable of type 'int' is created and a
reference to it passed to 'print', which must be valid because the OP
indicated it printed '0'. IOW, the zero is the value of the temporary
int variable, not the value of the reference.

Let me repeat least I am not clear, I am digging through my reference
from which I am basing my opion, which as I said could be wrong.
Anyway, the syntax being quoted by others is not in the context of
which I was responding.


Jul 10 '07 #23
gpuchtel wrote:
On Jul 10, 11:24 am, "Thomas J. Gritzan" <Phygon_ANTIS...@gmx.de>
wrote:
>>
You are wrong. The expression
static_cast<int*>(0)
or whatever you saw in Michael's code does not create a temporary
integer.

The syntax you quoted is neither the original syntax nor the context
in which I wrote my response. The original syntax and context was:

print(*static_cast<int*>(0));

not:

static_cast<int*>(0)

I may be wrong (I have been wrong before; however I'm still
verifying), but what I am saying is that in the construction of the
call to 'print', a temporary variable of type 'int' is created
You *are* wrong. No temporary is created since 'print' expects
a reference. Only reference binding occurs here (the argument of
'print' is bound to the l-value resulting from dereferencing the
pointer obtained from the static_cast).
and a
reference to it passed to 'print', which must be valid because the OP
indicated it printed '0'.
Of course it printed 0! The "OP" printed the *address* of the object
referred to by the argument. Please make an effort here and re-read
the posts in the thread.
IOW, the zero is the value of the temporary
int variable, not the value of the reference.
<sigh>
>
Let me repeat least I am not clear, I am digging through my reference
from which I am basing my opion, which as I said could be wrong.
Anyway, the syntax being quoted by others is not in the context of
which I was responding.
What context *is* it then? Your own invented context?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 10 '07 #24
On Jul 10, 1:25 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
>
You *are* wrong.
agreed.
Jul 10 '07 #25
gpuchtel a écrit :
On Jul 10, 8:13 am, Michael DOUBEZ <michael.dou...@free.frwrote:
>gpuchtel a écrit :
>>On Jul 10, 6:41 am, Michael DOUBEZ <michael.dou...@free.frwrote:
AFAIK the following code is valid (though UB):
void print(int& i)
{
cout<<&i<<endl;
}
int main()
{
print(*static_cast<int*>(0));
return 0;
}
And prints '0'.
A reference cannot be 'null'. What you did created a tempory int
variable that was initialized to 0 (zero), then a reference to that
temporary variable was passed to the print function, which in turn
printed '0'.
Where did you get that from ? Even if I wanted it, I couldn't pass a
temporary because the ref is not const.

It is perhaps more understandable if you replace by:
int *ptr=NULL;
print(*ptr)

and add in print
++i; //cause segfault;

Michael- Hide quoted text -

- Show quoted text -

I didn't say (or mean) 'you' would pass a temporary, I was implying
what the compiler will do (pass). Simply stated, there is no such
thing as a 'null' reference. All your example does is to create a
reference to a (tempory) int value that contains the value of zero.
Your example of a null pointer is not relevant to the discussion of
the (im)possibility of having a 'null' reference.
I don't understand what you mean and I don't see where is your temporary
or why the compiler will create one.

"*static_cast<int*>(0)" is deferencing an int value at address NULL
(assuming NULL is int promoted to 0). The value is passed into the
function which expect a reference.

If the compiler created a temporary it wouldn't be a reference; it would
be a bug in the compiler because it would not modify my value.

My understanding of a 'null' reference is a reference which reference is
null. As in the following

void print(int& i)
{
assert(&i != NULL ) ; // will trigger in my example

//...
}
The code I originally posted is directly equivalent to:

void print(int* i)
{
cout<<i<<endl;
}
int main()
{
print(static_cast<int*>(0));
return 0;
}
My point is that 'null' reference do exists the same way null pointer
exists.

Michael

Jul 11 '07 #26
On Jul 11, 4:04 am, Michael DOUBEZ <michael.dou...@free.frwrote:
>
I don't understand what you mean and I don't see where is your temporary
or why the compiler will create one.
Michael, I already admitted I was wrong in a previous post. I am still
flipping through pages to find the snippet of code that caused me to
come to my original (wrong) conclusion, to which I admit now for the
second time.
Jul 11 '07 #27
gpuchtel a écrit :
On Jul 11, 4:04 am, Michael DOUBEZ <michael.dou...@free.frwrote:
>I don't understand what you mean and I don't see where is your temporary
or why the compiler will create one.

Michael, I already admitted I was wrong in a previous post. I am still
flipping through pages to find the snippet of code that caused me to
come to my original (wrong) conclusion, to which I admit now for the
second time.
Yes. My news reader had scrambled the thread and I hadn't seen it before
answering.
Sorry for the garbage.

Michael
Jul 11 '07 #28
Michael DOUBEZ <mi************@free.frwrote in news:46948d64$0$3691
$4*******@news.free.fr:
My point is that 'null' reference do exists the same way null pointer
exists.

Not in a well-formed C++ program which doesn't invoke Undefined Behaviour.
NULL pointers can exist, "NULL" references cannot.
Jul 11 '07 #29
Andre Kostur wrote:
Michael DOUBEZ <mi************@free.frwrote in news:46948d64$0$3691
$4*******@news.free.fr:
>My point is that 'null' reference do exists the same way null pointer
exists.


Not in a well-formed C++ program which doesn't invoke Undefined
Behaviour. NULL pointers can exist, "NULL" references cannot.
Please see issue 232 on Committee core language issue list.

http://www.open-std.org/jtc1/sc22/wg...ctive.html#232

I believe the issue is still under discussion.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 11 '07 #30
Andre Kostur a écrit :
Michael DOUBEZ <mi************@free.frwrote in news:46948d64$0$3691
$4*******@news.free.fr:
>My point is that 'null' reference do exists the same way null pointer
exists.


Not in a well-formed C++ program which doesn't invoke Undefined Behaviour.
NULL pointers can exist, "NULL" references cannot.
Putting appart issue 232 of WG21. My point is deferencing a NULL pointer
that is expected to be not NULL is also UB.

If you read the whole thread, the reason I mentionned it is that
reference gives a false sense of security ("reference cannot be NULL").
I don't propose to assert value of reference in code, I only want to
mention that it can happen and it is likely a bug.

For the sake of completeness, reference are also subject to dangling
pointer (commonly by returning the reference to a local object) which is
equally UB.

Michael
Jul 11 '07 #31
On Jul 10, 12:41 pm, Michael DOUBEZ <michael.dou...@free.frwrote:
gpuchtel a écrit :
On Jul 9, 7:03 pm, spekyu...@gmail.com wrote:
Pointer and reference arguments provide C++ programmers with the
ability to modify objects. What is more efficient, passing arguments
via pointer or reference? Avoid the stereotypical urge of debating
effective coding style. Instead, think of particular scenarios:
passing a 512MB object to function. Think behind the scenes, from the
stack and heap to the physical linking and build of the program.
[snip]
The 'efficiency' is in the recipient. That is, it does not have to
check if a reference is null - since a reference can never be null.
A reference can be NULL or invalid all the same.
Only if the code contains undefined behavior, and even then, who
knows. Generally speaking, there is no way to get a null
reference without incurring undefined behavior, and once you've
incurred undefined behavior, there are no guarantees concerning
what you get.
The difference is that the code indicates a NULL wouldn't be
expected (i.e. the code is self explaining; whereas with
pointer, you would have to read the doc/comments).
AFAIK the following code is valid (though UB):
So which is it? Valid, or undefined behavior? It can't be
both.
void print(int& i)
{
cout<<&i<<endl;
}
int main()
{
print(*static_cast<int*>(0));
return 0;
}
And prints '0'.
Maybe. On some implementation. Some days of the month. I've
definitly used an implementation where it would core dump.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jul 11 '07 #32
On Jul 10, 5:16 pm, gpuchtel <gpuch...@gmail.comwrote:
On Jul 10, 8:13 am, Michael DOUBEZ <michael.dou...@free.frwrote:
gpuchtel a écrit :
On Jul 10, 6:41 am, Michael DOUBEZ <michael.dou...@free.frwrote:
>AFAIK the following code is valid (though UB):
>void print(int& i)
>{
> cout<<&i<<endl;
>}
>int main()
>{
> print(*static_cast<int*>(0));
> return 0;
>}
>And prints '0'.
A reference cannot be 'null'. What you did created a tempory int
variable that was initialized to 0 (zero), then a reference to that
temporary variable was passed to the print function, which in turn
printed '0'.
Where did you get that from ? Even if I wanted it, I couldn't pass a
temporary because the ref is not const.
It is perhaps more understandable if you replace by:
int *ptr=NULL;
print(*ptr)
and add in print
++i; //cause segfault;
I didn't say (or mean) 'you' would pass a temporary, I was implying
what the compiler will do (pass).
What the compiler will do is undefined. His code has undefined
behavior, so the compiler can do anything it wants. I've used
compilers that behave as he describes, but I've also used
compilers which will core dump in his initial code.
Simply stated, there is no such
thing as a 'null' reference. All your example does is to create a
reference to a (tempory) int value that contains the value of zero.
That would also be fine. It's undefined behavior, and the
compiler can do whatever it wants. I'm not aware of a compiler
which does as you describe, but it wouldn't surprise me too
much.
Your example of a null pointer is not relevant to the discussion of
the (im)possibility of having a 'null' reference.
Programs with undefined behavior aren't relevent to any
discussion of how C++ behaves.:-)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jul 11 '07 #33
On Jul 11, 4:12 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
Andre Kostur wrote:
Michael DOUBEZ <michael.dou...@free.frwrote in news:46948d64$0$3691
$426a7...@news.free.fr:
My point is that 'null' reference do exists the same way null pointer
exists.
Not in a well-formed C++ program which doesn't invoke Undefined
Behaviour. NULL pointers can exist, "NULL" references cannot.
Please see issue 232 on Committee core language issue list.
http://www.open-std.org/jtc1/sc22/wg...ctive.html#232
I believe the issue is still under discussion.
Certain issues are under discussion, but I don't think that
anyone is proposing a change to the rule that a reference must
bind to a valid object.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jul 11 '07 #34
On Jul 10, 7:40 pm, "Bo Persson" <b...@gmb.dkwrote:
I think you have a problem here - sizeof(refObject) returns the size
of the object, not of the reference. A reference is just an alias for
an object. Anything you do to the reference actually happens to the
object it refers to.
It doesn't return size of the object. It returns size of the type
object. There is a slight difference; here's demonstration:

// begin listing
#include <iostream>

class foo
{
int i;
virtual int value() const { return 1; }
};

class bar : public foo
{
int j;
virtual int value() const { return 2; }
};

void test(foo& f)
{
std::cout << sizeof(f) << std::endl;
}

int main()
{
foo f;
bar b;

test(f);
test(b);
}
// end listing

Jul 11 '07 #35
James Kanze a écrit :
On Jul 10, 12:41 pm, Michael DOUBEZ <michael.dou...@free.frwrote:
>gpuchtel a écrit :
>>On Jul 9, 7:03 pm, spekyu...@gmail.com wrote:
Pointer and reference arguments provide C++ programmers with the
ability to modify objects. What is more efficient, passing arguments
via pointer or reference? Avoid the stereotypical urge of debating
effective coding style. Instead, think of particular scenarios:
passing a 512MB object to function. Think behind the scenes, from the
stack and heap to the physical linking and build of the program.
[snip]
The 'efficiency' is in the recipient. That is, it does not have to
check if a reference is null - since a reference can never be null.
>A reference can be NULL or invalid all the same.

Only if the code contains undefined behavior, and even then, who
knows. Generally speaking, there is no way to get a null
reference without incurring undefined behavior, and once you've
incurred undefined behavior, there are no guarantees concerning
what you get.
True. I see the point. The wording would be: in a well formed program,
no reference can be null. But in practice, you can get UB (a bug)
through the form of null-reference.

The UB stays for deferencing a NULL pointer but the UB is prior to the
function call in the case of null-reference bug.
>The difference is that the code indicates a NULL wouldn't be
expected (i.e. the code is self explaining; whereas with
pointer, you would have to read the doc/comments).
>AFAIK the following code is valid (though UB):

So which is it? Valid, or undefined behavior? It can't be
both.
I mean syntaxically valid but UB.
>void print(int& i)
{
cout<<&i<<endl;
}
>int main()
{
print(*static_cast<int*>(0));
return 0;
}
>And prints '0'.

Maybe. On some implementation. Some days of the month. I've
definitly used an implementation where it would core dump.
Agreed. I am still waiting for the dragons-out-of-the-nose flavor :)

Michael
Jul 12 '07 #36

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Mark Stijnman | last post by:
A while ago I posted a question about how to get operator behave differently for reading and writing. I basically wanted to make a vector that can be queried about whether it is modified recently...
4
by: Peter Ammon | last post by:
Does anyone ever write the arguments to printf()-like functions under the corresponding format specifier? printf("My name is %s and I am %u years old\n", name, age); If the arguments...
12
by: Chris Springer | last post by:
I'd like to get some feedback on the issue of storing data out to disk and where to store it. I've never been in a production environment in programming so you'll have to bear with me... My...
19
by: Marco | last post by:
FYI: Guidelines for writing efficient C/C++ code http://www.embedded.com/showArticle.jhtml?articleID=184417272 any comments?
3
by: patrickdepinguin | last post by:
Hi, I need to write large quantities of data to a file in C. The data comes from statistics that are continuously gathered from a simulator, and in order to not slow the whole thing down I would...
4
by: Luna Moon | last post by:
seeking highly efficient caches scheme for demanding engineering computing? HI all, To same the time of costly function evaluation, I want to explore the possibility of caching. Suppose in...
8
by: blaine | last post by:
Hey guys, For my Network Security class we are designing a project that will, among other things, implement a Diffie Hellman secret key exchange. The rest of the class is doing Java, while myself...
2
by: cj | last post by:
<System.Web.Services.WebService(Name:="CJ's Service", Description:="Testing 123", Namespace:="http://myco.com/cj/one")_...
6
by: L. Ximenes | last post by:
Greetings, I've recently been struggling with the idea of writing an XML document based on user-submitted content using javascript. Using various instances of document.write on a newly opened...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.