pointers anbd references as parameters

J

Jim Langston

E.T. Grey said:
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.
 
E

E.T. Grey

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 ...
 
A

Alf P. Steinbach

* 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.
 
F

Frederick Gotham

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.
 
F

Frederick Gotham

Alf P. Steinbach posted:
* E.T. Grey:

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);
}
 
A

Alf P. Steinbach

* Frederick Gotham:
Alf P. Steinbach posted:



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<MyClass> const& v )
{
for( ... )
{
...
}
}
 
F

Frederick Gotham

Alf P. Steinbach posted:
Use

void func( std::vector<MyClass> const& 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.
 
T

Thomas J. Gritzan

Frederick said:
Alf P. Steinbach posted:



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.
 
D

Diego Martins

Frederick said:
E.T. Grey posted:




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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,166
Latest member
DollyBff32
Top