473,407 Members | 2,326 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,407 software developers and data experts.

pointers anbd references as parameters

Can I pass a pointer instead of a reference (and vice versa)? - I know a
reference and a pointer are not the same - but i can't help feeling I
can "get away with this".

Bad practise, ok practise, or must be avoided at all times?

If must be avoided, can I hust take the address of a reference and use
that as a pointer? (actually, its mre useful if I can pass a pointer as
reference

PS: Alarm bells ringing though - a ptr can be null, whereas a reference
can't be .. Hmmmm, looks like "terra infestus" (dangerous ground) to me ...

Sep 2 '06 #1
8 1502
* E.T. Grey:
>
PS: Alarm bells ringing though - a ptr can be null, whereas a reference
can't be .. Hmmmm, looks like "terra infestus" (dangerous ground) to me ...
Yes, that's why you preferentially should use references for arguments,
and pointers only where null is a possibility you actively support. One
common exception is to pass pointers for dynamically allocated objects.
A probably much better practice is to use smart pointers for those.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Sep 2 '06 #2
"E.T. Grey" <ne****@alpha-centauri.comwrote in message
news:YN********************@bt.com...
Can I pass a pointer instead of a reference (and vice versa)? - I know a
reference and a pointer are not the same - but i can't help feeling I can
"get away with this".
It depends on where you want to pass it. If a function or method expects a
reference then you couldn't pass a pointer to it, but you could modify the
function/method to accept a pointer instead.
Bad practise, ok practise, or must be avoided at all times?
Useful practice in some instances.
If must be avoided, can I hust take the address of a reference and use
that as a pointer? (actually, its mre useful if I can pass a pointer as
reference
Hopefully this code shows you what you are asking about.

#include <string>
#include <iostream>

void MyRefF( int& IntR )
{
// How to use IntR as a pointer? This works for me.
int* IntP = &IntR;
*IntP = 20;
std::cout << *IntP << std::endl;
}

int main()
{
int* MyIntP = new int(10);
// So how do we pass MyIntP to MyRefF? Dereference it.
MyRefF( *MyIntP );
std::cout << *MyIntP << std::endl;

delete MyIntP;
MyIntP = NULL;
// Following is undefined behavior, and gives me illegal memory access
// when the variable is used inside of MyRefF
MyRefF( *MyIntP );

std::string wait;
std::getline( std::cin, wait );
}
PS: Alarm bells ringing though - a ptr can be null, whereas a reference
can't be .. Hmmmm, looks like "terra infestus" (dangerous ground) to me
...
And you are right to think that, as the code shows.
Sep 2 '06 #3
E.T. Grey posted:
Can I pass a pointer instead of a reference (and vice versa)?


Depends on the context. In general: Yes.

- I know a reference and a pointer are not the same - but i can't help
feeling I can "get away with this".

Pointers are different in that they are an actual object.

A reference is simply a idea, a mystical concept.

Bad practise, ok practise, or must be avoided at all times?

A lot of people like references; they're handier to use.

If must be avoided, can I hust take the address of a reference and use
that as a pointer?

A reference isn't an object (nor a function) -- it doesn't have an address.
Once a reference has been declared, any usage of it is actually usage of
the object which the reference refers to. For example:

int i = 5;

int &r = i;

Func(&r); /* This passes the address of i */

(actually, its mre useful if I can pass a pointer as reference

Depends on context.

--

Frederick Gotham
Sep 3 '06 #4
Alf P. Steinbach posted:
* E.T. Grey:
>>
PS: Alarm bells ringing though - a ptr can be null, whereas a reference
can't be .. Hmmmm, looks like "terra infestus" (dangerous ground) to me
...

Yes, that's why you preferentially should use references for arguments,
and pointers only where null is a possibility you actively support.

Another common usage of passing pointers by value is where you are dealing
with an array, e.g.:

void Func(MyClass *p,size_t len)
{
assert(p); assert(len);

do
{
/* Manipulate the array */
} while(--len);
}

--

Frederick Gotham
Sep 3 '06 #5
* Frederick Gotham:
Alf P. Steinbach posted:
>* E.T. Grey:
>>PS: Alarm bells ringing though - a ptr can be null, whereas a reference
can't be .. Hmmmm, looks like "terra infestus" (dangerous ground) to me
...
Yes, that's why you preferentially should use references for arguments,
and pointers only where null is a possibility you actively support.


Another common usage of passing pointers by value is where you are dealing
with an array, e.g.:

void Func(MyClass *p,size_t len)
{
assert(p); assert(len);

do
{
/* Manipulate the array */
} while(--len);
}
Use

void func( std::vector<MyClassconst& v )
{
for( ... )
{
...
}
}

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Sep 3 '06 #6
Alf P. Steinbach posted:
Use

void func( std::vector<MyClassconst& v )
{
for( ... )
{
...
}
}

I prefer to use arrays for simple tasks. I would find a vector more
appropriate if the code were a little more complicated.

--

Frederick Gotham
Sep 3 '06 #7
Frederick Gotham schrieb:
Alf P. Steinbach posted:
>Use

void func( std::vector<MyClassconst& v )
{
for( ... )
{
...
}
}


I prefer to use arrays for simple tasks. I would find a vector more
appropriate if the code were a little more complicated.
I started to use std::vector everytime I would use new[].

Why? Because it's easier and it makes short & clear code. The class holds
the actual size, and deletes the memory in case of exception and normal
cleanup.

The opposite is in my (windows) driver code, where I write more "C-like".
The code looks like this:

<bad code>
Obj* o1 = CreateSomeObject();
if (!o1)
{
// handle error
return;
}

Obj* o2 = CreateSomeOtherObject();
if (!o2)
{
// handle error
free(o1); // don't forget it!
return;
}

Obj* o3 = CreateSomeOtherObject();
if (!o3)
{
// handle error
free(o2);
free(o1); // don't forget one of it!
return;
}

free(o3);
free(o2);
free(o1);
</bad code>

Perhaps I should start using scope guards in kernel code also.

--
Thomas
http://www.netmeister.org/news/learn2quote.html
Sep 3 '06 #8

Frederick Gotham wrote:
E.T. Grey posted:
Can I pass a pointer instead of a reference (and vice versa)?

Depends on the context. In general: Yes.
when you need a SENTINEL, you can't use references
I use const pointers, in this case

Diego
HP

Sep 4 '06 #9

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

Similar topics

94
by: Gabriel Zachmann | last post by:
Is it correct to say that strong/weak typing does not make a difference if one does not use any pointers (or adress-taking operator)? More concretely, I am thinking particularly of Python vs C++....
26
by: Desmond Liu | last post by:
I've read articles like Scott Meyer's EC++ (Item 22) that advocate the use of references when passing parameters. I understand the reasoning behind using references--you avoid the cost of creating...
12
by: Lance | last post by:
VB.NET (v2003) does not support pointers, right? Assuming that this is true, are there any plans to support pointers in the future? Forgive my ignorance, but if C# supports pointers and C# and...
20
by: Joe Van Dyk | last post by:
Is there some rule of thumb about when to use pointers to an object and when to use a reference* to an object when a class needs to have objects as data members? Example: class A { B* b_ptr;...
5
by: Dinesh Kumar | last post by:
Hi all I am using VB.NET for a Connector dll in Delphi client and some webservice . can you tell me how to handle pointers in Vb.net which are passed by delphi client as parameters in function...
7
by: Erdal Mutlu | last post by:
Hi, I am trying to design a base class (interface) with two or more subclasses as follows: class A { .... virtual static A* getByName(const string x)=0 const; }
19
by: MQ | last post by:
Can someone tell me where I should use pointers and where I should use references? In his book, Stroustrup says that you should use pointers for passing arguments that are to be modified, not...
66
by: Simon Saize | last post by:
Hi - What's the point of having references (&)? Why don't we just use pointers (*)? Thanks.
8
by: darren | last post by:
Hi everybody I am a bit confused about functions that take pointers / references as parameters. Are these two functions the same? void myFunc(int& anInt); void myFunct(int* anInt(0; Would...
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: 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
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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,...
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...
0
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...
0
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,...

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.