Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

From pointer to reference? 2

Status
Not open for further replies.

RbgoWeb

Programmer
Apr 10, 2002
91
NL
//I have a structure type, designed to describe something
typedef struct _DESCRIPTION{
//imagine 10 members
} DESCRIPTION;

//I have a bunch of functions to manage structures of that
//type, and they take a reference to DESCRIPTION
void desc_do_something_1(DESCRIPTION& desc);
void desc_do_something_2(DESCRIPTION& desc);
void desc_do_something_3(DESCRIPTION& desc);

//Some where else in the programm I have a function which
//has a DESCRIPTION pointer, e.g. from a dynamic array of
//descriptions that uses the managing functions
void do_dialog_description(DESCRIPTION* pDesc)
{
//Inside I call
desc_do_something_1(*pDesc);
}


I have not considered this while constructing the functions. I use references because they work nice and the above sample code works fine. But I am worried...

What exactly is the difference between reference and pointer?
What is being passed/copied to desc_do_something_1 in the call? (I hope not all structure values)
 
The difference is just semantic, but with some important effects.
You can use ref and ptr to show/control ownage of an object, a GoodThing(tm). References are just "borrowed" (someone else own them and must ensure they are valid for the duration of your usage) while the one recieveing a pointer can also destruct the object, which should be considered if that is really what you want to allow.

With references you can use overloaded operators on the class.

But if you look at it in technical terms, references and pointers are pretty much the same thing: They are really just holding the address of something.



/Per
Nerdy signatures are as lame as the inconsistent stardates of STTNG.
 
Thanks a lot PerFnurt.
But what happens with the statement from the code sample...

desc_do_something_1(*pDesc);

I need to dereference the pointer to get it to work.
Does this mean copying all structure values?
 
Dude, PerFnurt explained it exactly. Did you read his post?

references and pointers are pretty much the same thing: They are really just holding the address of something.

Does that say that the copy all structure values?

-pete
 
That desc_do_something_1(*pDesc) can change the values at pDesc, does say that the values are not copied. You're right.

But if I dereference the DESCRIPTION* pDesc, I can assign
it to a DESCRIPTION desc structure like...
desc = *pDesc;
where all the values of the structure at pDesc are
copied into desc, because pDesc was dereferenced.

What does exactly happen here?
desc_do_something_1(*pDesc);

If a pointer and reference both hold an address, why can I not make the call...
desc_do_something_1(pDesc);


I once read that

printf("some number is %d\n", number);

is slower than

char g_szFmNumber[] = "some number is %d\n";
printf(szFmNumber, number);

because the first example copies the entire format string to
printf, while the second only passes a pointer.

I did it the slow way for a long time; from now on a long directory list among things, comes to screen a lot faster in my code.
It also encouraged me to learn more about what technically goes on behind the scenes in the area of pointers, references and function calls. So please, forgive me my silly questions, dude Palbano.
 
hey.... dont' capitalize the Palbano if your going to flame me! [lol]
[blue]
But if I dereference the DESCRIPTION* pDesc, I can assign
it to a DESCRIPTION desc structure like...
desc = *pDesc;
where all the values of the structure at pDesc are
copied into desc, because pDesc was dereferenced.
[/blue]

That makes your question clear to me now, i think.

It is the assignment to a non reference type that causes the memberwise copy to occur and hence the extra overhead not the fact that you dereference the pointer.

If you did this there would be no copy:
Code:
DESCRIPTION& desc = *pDesc

There is no copy there. That is equivalent to copying a pointer only. That does not perform a memberwise copy of the structure into another memory block. This is what is effectively happening with a function reference parameter type of DESCRIPTION&.

In technical terminology it is called, pass by reference
Code:
void somefunc(SOMETYPE& val);
vs.
pass by value
Code:
void somefunc(SOMETYPE val);

Does that help?


-pete
 
That was exactly what I needed to know. Thank you Palbano =). Sorry bout the flames.
 
I was just joking about the "flame"… hence the little laughing animation guy --> [lol]

Glad we helped you.


-pete
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top