Archived

This topic is now archived and is closed to further replies.

Trashcann

C++... Reference Parameters

Recommended Posts

Either one will screw up if you pass in NULL. No two ways about that.

The general rule for references and pointers, which most good C++ programmers ascribe to, is to use references "whenever possible" and use pointers "wherever else". That means that almost all function arguments will be references; the only real place where you use pointers are where you have to reseat them.


How appropriate. You fight like a cow.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
The first one is definetly the way to go (use the reference and not the pointer).

References in C++ cannot be NULL. For more information, see "More Effective C++" by Scott Meyers, Item number 1 (page 10 specifically).

Many features of the C++ language can be defeated by underhandedness (like changing the private members of a class for example), but that shouldn''t prevent you or anyone else from using the language properly.

Share this post


Link to post
Share on other sites
To nitpick, there''s no risk of NULL reference or NULL pointer in the specific example, because that new expression can''t return NULL.

In general I''d say any function accepting a pointer argument should check for NULL while there''s no need to check for NULL reference arguments. Based on that the first one is better.

Passing a pointer to something makes it more obvious (in the calling code) that the argument will be modified; some people prefer that and avoid using non-const reference arguments, but that''s a style preference rather than a technical reason.

Share this post


Link to post
Share on other sites
quote:
Original post by spock
To nitpick, there''s no risk of NULL reference or NULL pointer in the specific example, because that new expression can''t return NULL.

In general I''d say any function accepting a pointer argument should check for NULL while there''s no need to check for NULL reference arguments. Based on that the first one is better.

Passing a pointer to something makes it more obvious (in the calling code) that the argument will be modified; some people prefer that and avoid using non-const reference arguments, but that''s a style preference rather than a technical reason.


NULL is returned on failure. Here''s the proof:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccelng/htm/express_25.asp

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccelng/htm/conve_12.asp

Trashcan,

On looking at the code with ''fresh eyes'' what Sneftel says is perfectly correct.

You will need to check for NULL before using add2() to prevent undefined behaviour.

Share this post


Link to post
Share on other sites
quote:
Original post by Mathematix
NULL is returned on failure.

For a standard-conforming compiler, the required behavior is to return a non-null pointer or throw a bad_alloc exception. If you want a null pointer as an error indication, you need to use the nothrow version of new.

AFAIK Visual C++ .NET conforms to the standard in this respect while earlier versions did not.

[edited by - spock on October 7, 2003 6:28:45 AM]

Share this post


Link to post
Share on other sites
Why do people try to make wrong what is right?

I am also aware that in some implementations NULL is (void *) 0 which is still zero!

You can alter the default behaviour if you wish, but the facts are there, plain and simple. Read it, it says 'If unsuccessful, by default new returns zero.' How much more plain do you want?!

[edited by - mathematix on October 7, 2003 6:40:59 AM]

Share this post


Link to post
Share on other sites
Mathematix: That''s only true for MSVC. (And probably only MSVC 6, seems like they fixed it in .NET)
There ARE other compilers, and some of them actually follow the spec. (which says that new either returns a non-null pointer, or throws bad_alloc, like spock said)

Share this post


Link to post
Share on other sites
That''s the reason why it p***** me off posting here! Everybody is a bloody know-all until somebody dares to post something sensible, then try to crush it. It makes me sick.

I am aware of other compilers. Just one question, what on earth is the pointer set to once a bad_alloc exception is raised?

Share this post


Link to post
Share on other sites
quote:
Original post by Mathematix
That''s the reason why it p***** me off posting here! Everybody is a bloody know-all until somebody dares to post something sensible, then try to crush it. It makes me sick.

I am aware of other compilers. Just one question, what on earth is the pointer set to once a bad_alloc exception is raised?


It depends on how the code is structured. If the exception is caught outside of the pointer''s scope then the pointer doesn''t even exist. If it''s caught inside the pointer''s scope then its value is probably undefined, though I don''t have my standard to hand to check.

Share this post


Link to post
Share on other sites
quote:
Original post by DrPizza
quote:
Original post by Mathematix
That''s the reason why it p***** me off posting here! Everybody is a bloody know-all until somebody dares to post something sensible, then try to crush it. It makes me sick.

I am aware of other compilers. Just one question, what on earth is the pointer set to once a bad_alloc exception is raised?


It depends on how the code is structured. If the exception is caught outside of the pointer''s scope then the pointer doesn''t even exist. If it''s caught inside the pointer''s scope then its value is probably undefined, though I don''t have my standard to hand to check.


Thanks DrPizza! You are someone who at last has a damn good idea of what they are talking about, rather than trying to impress all others with their textbook knowledge.

I notice that those who felt that they knew the answer before failed to answer. Let me guess, they didn''t see my last post! Surprise, surprise.

Share this post


Link to post
Share on other sites