473,903 Members | 4,818 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 10015
Steven T. Hatton wrote:
I have never found a need for a constant pointer, as opposed to a pointer to
constant.


unsigned char volatile *const Pointer_To_Memo ry_Mapped_Regis ter;
unsigned char volatile long *const Pointer_To_MM_R ead_Only_Regist er;
Jul 23 '05 #101
red floyd wrote:
I have never found a need for a constant pointer, as opposed to a pointer to constant.


unsigned char volatile *const Pointer_To_Memo ry_Mapped_Regis ter;
unsigned char volatile long *const Pointer_To_MM_R ead_Only_Regist er;


Ah, but those should be references, because you will never use the pointer
datum itself as a value. You should seat them once and then use them
(volatiley).

;-)

--
Phlip
http://www.c2.com/cgi/wiki?ZeekLand

Jul 23 '05 #102
Steven T. Hatton wrote:
I agree. After this discussion started I began looking at places where
pointers are used, and, indeed, they can 'vanish' from clear sight. I
typically name things that are pointers _something_ptr, (intrusive)
reference counted pointers become _something_rptr and smart pointers become
_something_sptr .


If you're going to rely on naming conventions to convey "may be
modified", then whether you use references or pointers is almost
irrelevant. This makes it possible to decide on reference or pointer
arguments based on what makes more sense for the function in question.

Nitpick: avoid names that begin with "_".

Bob

Jul 23 '05 #103
Phlip wrote:
Shezan Baig wrote:
By using the '#define' macro, you're basically messing with the C++
type system and asking for trouble (in other places as well). Using
this instead:
Did you see the place where I wrote "If anyone finds an an issue with the
example, they'l be qualified to think of a similar one that generates the
same problem." ?

Steven T. Hatton wrote:
That's because MACROS SUCK!


Yet sometimes we pay our vendors to provide them.


I'm considering a funddrive to persuade (bribe) one of the providers of a
library I'm working with to remove his _______ #MACROS.
My example is not the Alpha and Omega of 'const' placement. It merely
shows how const placement can't be called "_only_ a style question".
// t_rptr_ reminds me I plan to keep a handle on the argument.


I hate that kind of comment. There's no way for smart pointers to enforce
'new' (or have the Boosties found one?).


These aren't Boost pointers. They are form OSG. You cannot do a new with
one, but you can do referencing counting on all the objects derived from
osg::Referenced . So as long as you get that first handle, and make sure
you don't get any cycles, you can be sure of no memory leaks (assuming the
platform you're standing on isn't already adrift.)

--
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 #104
be****@pacbell. net wrote:
Nitpick: avoid names that begin with "_".


BAH! The implementation ain't got no business snoopin' around in my
classes! _foo works great!
--
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 #105
Steven T. Hatton wrote:
I'm considering a funddrive to persuade (bribe) one of the providers of a
library I'm working with to remove his _______ #MACROS.


Are they actually causing you trouble?

Or are they just easy to spot?

Some chronic design problems are not...
I hate that kind of comment. There's no way for smart pointers to enforce 'new' (or have the Boosties found one?).


These aren't Boost pointers.


I didn't say that. If there were a way to solve the problem "only pass the
output of 'new' into a constructor", then I would expect the Boosties to
find it.

--
Phlip
http://www.c2.com/cgi/wiki?ZeekLand
Jul 23 '05 #106
Steven T. Hatton wrote:
Clark S. Cox III wrote:
Steven T. Hatton said:
E. Robert Tisdale wrote:

Isn't it "obvious" that, in

std::cout << "i = " << 42 << std::endl;

ostream std::cout is used *thrice*?

Would that be written like this?

using namespace std;
cout.operato r<<(cout.operat or<<(cout.opera tor<<(cout,i),4 2),endl);
No, like this:
cout.operator <<("i = ").operator<<(4 2).operator<<(e ndl);


That's what I started to write, and I suspect it will also work.


Sigh!
cat main.cc #include <iostream>

int main(int argc, char* argv[]) {
//std::cout << "i = " << 42 << std::endl;
operator<<(std: :cout, "i = ").operator<<(4 2)
.operator<<(std ::endl);
return 0;
}
g++ -Wall -ansi -pedantic -o main main.cc
./main

i = 42
Jul 23 '05 #107
Phlip wrote:
red floyd wrote:

I have never found a need for a constant pointer, as opposed to a
pointer to
constant.


unsigned char volatile *const Pointer_To_Memo ry_Mapped_Regis ter;
unsigned char volatile long *const Pointer_To_MM_R ead_Only_Regist er;

Ah, but those should be references, because you will never use the pointer
datum itself as a value. You should seat them once and then use them
(volatiley).

;-)

And how do you seat it, short of using a pointer? How do you make a
reference to it? Remember, the address of memory mapped register is
"magic", the compiler doesn't know about it.
Jul 23 '05 #108
red floyd wrote:
And how do you seat it, short of using a pointer? How do you make a
reference to it? Remember, the address of memory mapped register is
"magic", the compiler doesn't know about it.


In general, '* const' is always a hint you might need a reference instead.

In this case, Pointer_To_MM_R ead_Only_Regist er might indeed need to be born
NULL, wait a while, then be assigned a memory address.

If not, this works fine, right?

unsigned char volatile long &
Pointer_To_MM_R ead_Only_Regist er =
*reinterpret_ca st<unsigned char volatile long *> (0xDEADBEEF);

Fear this!

unsigned char volatile long *const volatile
Pointer_To_MM_R ead_Only_Asynch ronously_Moving _Register;

--
Phlip
http://www.c2.com/cgi/wiki?ZeekLand

Jul 23 '05 #109
Phlip wrote:
Steven T. Hatton wrote:
I'm considering a funddrive to persuade (bribe) one of the providers of a
library I'm working with to remove his _______ #MACROS.
Are they actually causing you trouble?


Trouble in the sense that they are offensive. They incorporate C-style
protramming in places where C++ constructs and concepts would be more
desirable. For example, they have many error code retrun value calls where
exceptions would be superior (which is basically everywhere there is an
error code retrun value.) They could use templates with some kind of
meta-programming gizmo to make them vanish when the optimization switches
are thrown. They use C-style output fromatting rather than C++
std::ostream. They hold the place of what could be more expressive and
elegant C++ function calls, or objects. The fail to use RAII where it is
appropriate. And they are all around just plain ugly!
Or are they just easy to spot?


The actual application code gets lost in the midst of them.
These aren't Boost pointers.


I didn't say that. If there were a way to solve the problem "only pass the
output of 'new' into a constructor", then I would expect the Boosties to
find it.


That won't always work for these. If I call new in a function where the
osg::ref_ptr<T> t_rptr(new T()); is defined I bump the reference count to
1. The transition from 1 to 0 forces the destructor to be called, so I may
have to fiddle with the reference count if I don't want it destroyed when
the block is exited. If you mean to call new in the constructor of the
object holding the osg::ref_ptr<T> , that has it's own set of problems.
It's probably pretty safe if there's only one parameter, but if there's
more than one there's a potential memory leak. It's just bad style to call
the constructor in a function call argument list. The brief existence of
the raw, un-handled pointer is safer and cleaner than any of the
alternatives.

--
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 #110

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

Similar topics

7
2088
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
1977
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
2596
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
10447
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
1766
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
2730
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
9999
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
10875
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
10986
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
10501
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
8049
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7206
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();...
0
5894
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
4308
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3324
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.