473,887 Members | 2,371 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Pass-by-reference instead of pass-by-pointer = a bad idea?

Hi!
I've been thinking about passing parameteras using references instead
of pointers in order to emphasize that the parameter must be an
object.

Exemple:
void func(Objec& object); //object must be an object

instead of

void func(Object* object); //object can be NULL

I belive that this is a good idea since , in the reference case, it's
clear that NULL is not an option. It's also clear that NULL is an
option when a pointer is expected (stating the obvious :-) ). The code
becomes somewhat more self-documenting.

Any comments on why this could be a bad idea or do you think it's just
a matter of taste?

/H
Jul 23 '05
110 10008
Well you CAN pass a reference to nothing though:

void f(int& i)
{
i ++;
}

int main()
{
int* i = new int;
int& ref = *i;
delete i;

f(ref); // tell me what does ref referencing to?
}

ben
Jul 23 '05 #21
David White wrote:
Steven T. Hatton wrote:
John Carson wrote:
Dereferencing a NULL pointer is undefined behaviour by section
8.3.2/4. When I run your code, the program crashes.


Yes, it segfaults. That was my point.


There's no mention of segfaulting in the standard. The behaviour is
undefined. That means that anything can happen, including the function
executing without any apparent problem.

DW


So what happens when you run it?

BTW, just as an aside, 'NULL' is the name of a #MACRO defined as '0'. A
pointer T* ptr; such that NULL == T; is an object of type 'pointer to T'.
'NULL' is, IMO, not appropriate for use as an adjective.

http://www.research.att.com/~bs/bs_faq2.html#null

--
If our hypothesis is about anything and not about some one or more
particular things, then our deductions constitute mathematics. Thus
mathematics may be defined as the subject in which we never know what we
are talking about, nor whether what we are saying is true.-Bertrand Russell
Jul 23 '05 #22
Sheesh.

The caller could just as easily do:

int i;
int* pI = &i + 1;
f(i);

or

int* i = new int;
delete i;
f(i);

C++ gives you all sorts of ways of doing something bad, and there are
lots of bad pointers other than 0.

I claim passing by reference helps keep the noise level down and make
it clear where the valid pointer test should be made. Passing by
pointer muddies the water: whose responsibility is it?

Stuart

Jul 23 '05 #23
Steven T. Hatton wrote:
David White wrote:
Steven T. Hatton wrote:
John Carson wrote:

Dereferencing a NULL pointer is undefined behaviour by section
8.3.2/4. When I run your code, the program crashes.

Yes, it segfaults. That was my point.


There's no mention of segfaulting in the standard. The behaviour is
undefined. That means that anything can happen, including the
function executing without any apparent problem.

DW


So what happens when you run it?


It doesn't matter what happens when I run it. All that matters is what the
standard allows to happen, and that is anything at all. You can't assume
that because it segfaults on a particular platform using a particular
compiler that it will do so for every platform and compiler, or even that
the program that segfaulted for you will do so the next time you run it.
Some computers (e.g., Intel 8085, 8086) are not even capable of segfaulting.
Instead, they are likely to just grab the value at address 0 (or whatever
address is used as the null pointer) and happily execute the function.

DW
Jul 23 '05 #24
Ian wrote:
Kristo wrote:
Mr A wrote:
I've been thinking about passing parameteras using references instead
of pointers in order to emphasize that the parameter must be an
object.

void func(Objec& object); //object must be an object
void func(Object* object); //object can be NULL


That looks like a very good idea to me, and IIRC, the FAQ agrees.
IMHO, it's more than a matter of style; it's also a matter of safety.
If a parameter should never be null, then don't give it the chance.

And passing by reference does this? I don't think so.


I do think so. Anyone else want to vote?

Jul 23 '05 #25
Stuart MacMartin wrote:
Well I suppose most of this comes down to local convention. I prefer to
work with references, and allow a semantic difference between a
parameter passed by reference rather than passed by pointer.
I suspect that could lead to problems if you apply that to code from outside
your local community. It would also seem wrong to criticize others who do
not share your conventional semantics.
Your comment:
Because code that does use pointers, and proper error checking won't
crash due to a null pointer, but code that uses references to pass
variables *can* crash because of a null pointer being passed to a
reference. So far as I know, there is no way to protect yourself from
that, other than checking for null before dereferencing.

And that's precisely the point.

I figure you should check for null before dereferencing (or otherwise
using what a pointer points to), and once you've safely dereferenced
continue to use that reference. You feel you should not trust your
caller, so insist on checking the pointer over and over again - and
instead of trusting your callers like I do, you trust the people who
write the routines you call.
Well, I have the advantage that I currently use only open source, so I can
verify, as well as trust. But, the fact of the matter is, I don't really
need to trust the function I call, other than to worry about subsequent
changes. If the function has a default null pointer parameter, I am pretty
safe passing a null pointer to it.
Since you'd check that the address of a
reference isn't 0, you see no semantic difference between passing a
pointer and a reference.
Actually, I can't check that the address of a reference is 0, but I can
assume it is non-zero in view of the fact that it makes no sense to have a
reference to '\0'.
Just out of curiosity: do you do dynamic_cast of all your variables to
make sure the types are the same as the parameters you specified? This
might sound snide, but I've had people downcast or force a cast
incorrectly, with problems only showing up when the data model changes
significantly or subtly on on rare data. If you can't believe they are
passing a reference to an object when you ask for a reference, how can
you believe that they are passing the correct kind of object?
Actually, there are some circumstances when the caller wouldn't really be
"wrong" to pass a parameter different than the one I handle.

virtual void operator()(osg: :Node *node, osg::NodeVisito r* nv){
if(_doRefresh){
if(PAT_T* pat = dynamic_cast<PA T_T*>(node)){
_refresh(pat);
_doRefresh=fals e;
}
}
traverse(node,n v);
}

Note that the code above is rather standard for 3D animation where
performance is critical. But this is called once per traversal. OTOH, if
the if() condition is satisfied, the call to _refresh() will result in
thousands of operations. It's
easy to fool the compiler into allowing this. The compiler can help
us, but only to a point. Eventually someone has to understand what a
routine is supposed to do before they call it.

Another thing that puzzles me in all of this: doesn't anyone use a
development environment? How can you not know the parameters and
whether they are passed by value or pointer or reference, const or
non-const?


Well, I use Emacs and KDevelop. Typically the technique I use to know what
the function declaration looks like is called RTFS. Don't get me wrong, I
believe IDEs are very valuable. It's one of the greatest weaknesses of C++
that creating good IDEs is extremely difficult. There are several nice
ones for Java, and monodevelop seems to be shaping up nicely. I'm hoping
to figure out how to provide some additional features to KDevelop's C++
part.

Sadly, it would seem Borland is not going to be able to keep pace with their
primary competitor. This is mostly due to the fact that Borland doesn't
control the platform that holds 90% or more of the desktop OS market share,
while their competitor does.

--
If our hypothesis is about anything and not about some one or more
particular things, then our deductions constitute mathematics. Thus
mathematics may be defined as the subject in which we never know what we
are talking about, nor whether what we are saying is true.-Bertrand Russell
Jul 23 '05 #26
Old Wolf wrote:
Ian wrote:
Kristo wrote:
Mr A wrote:

I've been thinking about passing parameteras using references instead
of pointers in order to emphasize that the parameter must be an
object.

void func(Objec& object); //object must be an object
void func(Object* object); //object can be NULL

That looks like a very good idea to me, and IIRC, the FAQ agrees.
IMHO, it's more than a matter of style; it's also a matter of safety.
If a parameter should never be null, then don't give it the chance.

And passing by reference does this? I don't think so.


I do think so. Anyone else want to vote?


Define parameter in this context.
--
If our hypothesis is about anything and not about some one or more
particular things, then our deductions constitute mathematics. Thus
mathematics may be defined as the subject in which we never know what we
are talking about, nor whether what we are saying is true.-Bertrand Russell
Jul 23 '05 #27
Steven T. Hatton wrote:
Larry I Smith wrote:
Steven T. Hatton wrote: [...]
AFAIK, you *can* pass a null to func() in your example, and the compiler
will accept it. Your code will segfault when you do so. If you pass a
pointer, you can check for null, before accessing it. I, therefore,
suggest using a pointer instead of a reference.

There is no such thing as a null reference. However, see below.
If a function takes a ref, then NULL can not be passed.
Attempting to pass NULL causes a compile error.

For example:
[snip] func(0); // causes a compile error
In your example, you are not trying to pass null, you are trying to pass a
literal. That results in the attempt to create a temporary object of type
int and assign it to the non-const reference.


True, 0 or NULL is the null pointer constant, which is not the same
thing as a null pointer.
This, however, will compile:

#include <string>
std::string func(std::strin g& s) { return s; }
int main() {
std::string* s(0);
func(*s);
}


True, this will compile, and will result in undefined behavior.
However, the unbehavior is not in func for trying to use a "null
reference" (there being, again, no such thing) but in main for
dereferencing a null pointer.

"But," you may say, "main doesn't dereference the pointer. The
seg fault [or whatever] occurs in func!"

First, it is not unusual for a run-time error (if one occurs
at all) to happen some time after the undefined behavior that
caused it.

Second, people tend to mistakenly think that "deferencin g" a
pointer means accessing the memory at the machine address stored
in the pointer. This is confusing a possible implementation of a
language concept with the concept itself. In C and C++, any time
you form an expression by applying the '*' operator to a pointer,
you have deferenced that pointer.

One might imagine it's a special case if the expression is used
to initialize a reference. After all, references and pointers are
both "just addresses" so "under the hood" you're not doing anything
at all. Again, this is confusing a possible implementation with the
language itself. In C++, if an expression dereferences a null
pointer, it is undefined behavior, regardless of how that
expression is then used.

In general, although it is nice to have some idea of what goes
on at the assembly code level, one cannot reductively understand
C++ in terms of machine code. To see how doing so might lead
one astray, consider an example which does not involve references:

int foo()
{
int n = 5;
int* p = &n;
return *p;
}

You don't have be a language lawyer to see that the expression *p
in this function dereferences the pointer p. Yet here, stripped
of comments, is the assembly listing for the above function
(from VC++ 7.1 with "optimize for size" specified):

?foo@@YAHXZ PROC NEAR
push 5
pop eax
ret 0
?foo@@YAHXZ ENDP

So what Steven's example shows is not that it is possible to
create a null reference, but rather that one must be careful
not to dereference a null pointer. That is certainly good
advice. :-)

Jul 23 '05 #28
Ian
Old Wolf wrote:
Ian wrote:
Kristo wrote:
Mr A wrote:
I've been thinking about passing parameteras using references instead
of pointers in order to emphasize that the parameter must be an
object.

void func(Objec& object); //object must be an object
void func(Object* object); //object can be NULL

That looks like a very good idea to me, and IIRC, the FAQ agrees.
IMHO, it's more than a matter of style; it's also a matter of safety.
If a parameter should never be null, then don't give it the chance.


And passing by reference does this? I don't think so.

I do think so. Anyone else want to vote?

As has been show elsewhere on this thread, references can be null.

Ian
Jul 23 '05 #29
ni*****@microso ft.com wrote:
Steven T. Hatton wrote:
Larry I Smith wrote:
> Steven T. Hatton wrote: [...] >> AFAIK, you *can* pass a null to func() in your example, and the
>> compiler
>> will accept it. Your code will segfault when you do so. If you pass
>> a
>> pointer, you can check for null, before accessing it. I, therefore,
>> suggest using a pointer instead of a reference.
There is no such thing as a null reference. However, see below.
> If a function takes a ref, then NULL can not be passed.
> Attempting to pass NULL causes a compile error.
>
> For example:
> [snip] > func(0); // causes a compile error
In your example, you are not trying to pass null, you are trying to pass
a
literal. That results in the attempt to create a temporary object of
type int and assign it to the non-const reference.


True, 0 or NULL is the null pointer constant, which is not the same
thing as a null pointer.


Actually it is an integer literal which when assigned to a pointer to type T
is converted to a null pointer constant to T. The distinction may seem
trivial, but the error is not due to the fact that 0 is being used. It is
due to the fact that a literal is being used to initialize a temporary of
type int.
This, however, will compile:

#include <string>
std::string func(std::strin g& s) { return s; }
int main() {
std::string* s(0);
func(*s);
}


True, this will compile, and will result in undefined behavior.
However, the unbehavior is not in func for trying to use a "null
reference" (there being, again, no such thing) but in main for
dereferencing a null pointer.


The act resulting in the undefined behavior is in main where the attempt to
dereference a null pointer is made. Where the actual /behavior/ occurs is
a different story. Basically, accessing the null pointer gives
'permission' to produce undefined behavior.
"But," you may say, "main doesn't dereference the pointer. The
seg fault [or whatever] occurs in func!"

First, it is not unusual for a run-time error (if one occurs
at all) to happen some time after the undefined behavior that
caused it.
The segfault *is* the undefined behavior that results from the attempt to
dereference a null pointer.
Second, people tend to mistakenly think that "deferencin g" a
pointer means accessing the memory at the machine address stored
in the pointer. This is confusing a possible implementation of a
language concept with the concept itself. In C and C++, any time
you form an expression by applying the '*' operator to a pointer,
you have deferenced that pointer.
The C++ Standard does not define what is meant by dereferencing a pointer.

§5.3.1/1
"The unary * operator performs /indirection/: the expression to which it is
applied shall be a pointer to an object type, or a pointer to a function
type and the result is an lvalue referring to the object or function to
which the expression points. If the type of the expression is ?pointer to
T,? the type of the result is ?T.?"

An lvalue is said to refer to specific storage, so I would say that pretty
much means returning an address.
One might imagine it's a special case if the expression is used
to initialize a reference. After all, references and pointers are
both "just addresses" so "under the hood" you're not doing anything
at all. Again, this is confusing a possible implementation with the
language itself. In C++, if an expression dereferences a null
pointer, it is undefined behavior, regardless of how that
expression is then used.
No, applying the indirection operator to a variable of type pointer to type
T is the definition of a behavior. The _result_ is undefined.
In general, although it is nice to have some idea of what goes
on at the assembly code level, one cannot reductively understand
C++ in terms of machine code. To see how doing so might lead
one astray, consider an example which does not involve references:

int foo()
{
int n = 5;
int* p = &n;
return *p;
}

You don't have be a language lawyer to see that the expression *p
in this function dereferences the pointer p. Yet here, stripped
of comments, is the assembly listing for the above function
(from VC++ 7.1 with "optimize for size" specified):

?foo@@YAHXZ PROC NEAR
push 5
pop eax
ret 0
?foo@@YAHXZ ENDP
Well, yes, you can invoke the "as if" clause.
So what Steven's example shows is not that it is possible to
create a null reference, but rather that one must be careful
not to dereference a null pointer. That is certainly good
advice. :-)


For the record, I don't believe I actually said the result was a null
reference.
--
If our hypothesis is about anything and not about some one or more
particular things, then our deductions constitute mathematics. Thus
mathematics may be defined as the subject in which we never know what we
are talking about, nor whether what we are saying is true.-Bertrand Russell
Jul 23 '05 #30

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

Similar topics

7
2086
by: winlinchu | last post by:
Hi! I use Python, and writing some extension modules I think which could be written an C compiler, useful only to compile extension modules (I not think an GCC!!!!), so that the user not have to use GCC, Microsoft Visual C++, or other. It must have an common API to all platforms, even if obviously the implementation is various. Could be write in 100% Python pure.
29
1974
by: Jim Hubbard | last post by:
Yet another hotfix alert (http://www.kbalertz.com/Feedback_823535.aspx) that states "To resolve this problem immediately, contact Microsoft Product Support Services to obtain the hotfix." Whatever happened to automatic updates? Doesn't Microsoft know that developers are busy people.....too busy to be stopped by this type of crap every time Microsoft is alerted to something they missed in the software by another developer (note:...
13
2592
by: Bryan Parkoff | last post by:
You may notice that switch (...) is much faster than function that can gain a big improved performance because it only use JMP instruction however function is required to use CALL, PUSH, and POP instruction that can be slower. I created three functions in an array. Array is called pArray_Func(). pArray_Func() contains three functions. "xx" can be used to point each function. Let say, I have over 1,000 functions. I would put "inline"...
25
10443
by: dixie | last post by:
I have some code that adds new records into a table for each ID in a list box when a button on a form is clicked. This works fine. My problem now is that I wish to be able to edit all the records for people whose ID is in the list box. I made minor changes to the code (mainly replacing rs.AddNew with rs.Edit)and it appears to be updating only the first record and then overwriting that record with the next, etc until it runs out of ID's...
19
3614
by: Raposa Velha | last post by:
Hello to all! Does any of you want to comment the approach I implement for instantiating a form? A description and an example follow. Cheers, RV jmclopesAThotmail.com replace the AT with the thing you know ;-) After discovering that access 2000 support form properties (I'm a
7
1765
by: Alan Silver | last post by:
Hello, I would like to create a new web site with VWD, but would like to run it under IIS, not the development server. One reason for this is that I want the web site to be at the domain root, not under a virtual directory. I have tried this, but don't seem to be able to get it to work. I created a new (empty) web site in IIS, and then started a new project in VWD and specified the type as HTTP. I browsed to http://test/ and
4
6729
by: Pedro Leite | last post by:
Good Afternoon. the code below is properly retreiving binary data from a database and saving it. but instead of saving at client machine is saving at the server machine. what is wrong with my code ?? thank you Pedro Leite From Portugal ------------------------------------
12
1824
by: Paul H | last post by:
A little off topic this one because the database may not be written in Access. I am just looking for some advice.. I have an idea to help prevent a particular type of crime, a database will be involved in storing and analysing information. The idea is quite a simple one and once disclosed would be easy, with reasonable technical know-how, to set-up. Police forces all over the country could use my system, but I want to approach the...
32
2728
by: andresj | last post by:
I was doing some programming in Python, and the idea came to my mind: using fractions instead of floats when doing 2/5. The problem arises when you try to represent some number, like 0.4 in a float. It will tell you that it's equal to 0.40000000000000002. "This is easy to fix", you may say. "You just use the decimal.Decimal class!". Well, firsly, there would be an excess of typing I would need to do to calculate 0.4+0.6: from decimal...
0
9957
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9799
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11173
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10770
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10875
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9593
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7141
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4632
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4238
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.