473,499 Members | 1,568 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Pointers *&

Hello,
I'm a c++'s newbie. In a BinarySearchTree data structure I found this
definition:
void insert( const Comparable & x, BinaryNode<Comparable> * & t ) const;
What does " *& " mean?
It' s a pointer to a reference? or what?
Thanks
Yuri

Jul 19 '05 #1
9 4059
"Yuri" <bo********@libero.it> wrote in message
news:Nq**********************@twister1.libero.it.. .
Hello,
I'm a c++'s newbie. In a BinarySearchTree data structure I found this
definition:
void insert( const Comparable & x, BinaryNode<Comparable> * & t ) const;
What does " *& " mean?
It' s a pointer to a reference? or what?

The other way round. It is a reference to a pointer, meaning that the
function 'insert' can directlt change the value of the pointer you are
passing to it.

hth
--
jb

(replace y with x if you want to reply by e-mail)
Jul 19 '05 #2

"Yuri" <bo********@libero.it> wrote in message
news:Nq**********************@twister1.libero.it.. .
Hello,
I'm a c++'s newbie. In a BinarySearchTree data structure I found this
definition:
void insert( const Comparable & x, BinaryNode<Comparable> * & t ) const;
What does " *& " mean?
It' s a pointer to a reference? or what?
Thanks
Yuri


It's a very important feature that I've used a lot. You probably know what a
pointer is, it's the address of an object. By using the pointer, the data
inside the BinaryNode could be modified and passed back. Unfortunately, if
the function were to modify the address contained in t, that address
wouldn't be passed back. Thus, the reference (&) is needed. This allows the
address to be passed back if it is changed. Try this to demonstrate it on
yourself:

/* This results in an access violation: data space never allocated */
void Allocate(int* var)
{
var = new int;
}

int main()
{
int* a;
Allocate(a);
*a = 5; // access violation
return (0);
}

This is because the address to the new int is never passed back, so main()
has no knowledge of that new address. On the other hand:

/* This works */
void Allocate(int* &var)
{
var = new int;
}

int main()
{
int* a;
Allocate(a);
*a = 5; // works
return (0);
}

This works because of the reference. Because of the reference, main() has
the knowledge of the new address. Note this does cause a memory leak, but I
didn't worry about deleting my pointers in this simple example, as this
example is not to teach you about pointers but rather references.

--
MiniDisc_2k2
To reply, replace nospam.com with cox dot net.
Jul 19 '05 #3
"MiniDisc_2k2" <Ma******@nospam.com> wrote in message
news:Nk*****************@news2.east.cox.net...

"Yuri" <bo********@libero.it> wrote in message
news:Nq**********************@twister1.libero.it.. .
Hello,
I'm a c++'s newbie. In a BinarySearchTree data structure I found this
definition:
void insert( const Comparable & x, BinaryNode<Comparable> * & t ) const;
What does " *& " mean?
It' s a pointer to a reference? or what?
Thanks
Yuri

It's a very important feature that I've used a lot. You probably know what

a pointer is, it's the address of an object. By using the pointer, the data
inside the BinaryNode could be modified and passed back. Unfortunately, if
the function were to modify the address contained in t, that address
wouldn't be passed back. Thus, the reference (&) is needed. This allows the address to be passed back if it is changed. Try this to demonstrate it on
yourself:

/* This results in an access violation: data space never allocated */
Not here it doesn't result in an access violation, and memory for an int is
quite definitely allocated. The point is that you lose the pointer to the
memory when the function returns, so you've ended up with a memory leak.
Furthermore, the pointer passed in will be unchanged after the call, which
causes the problem you've highlighted below.
void Allocate(int* var)
{
var = new int;
}

int main()
{
int* a;
Allocate(a);
*a = 5; // access violation
*Now* there is undefined behaviour, which might or might not result in an
"access violation" (hopefully it will, but anything could happen). The
reason is that the value of a at this point is indeterminate. In other
words, you are dereferencing a pointer that points to a random place in
memory -> undefined behaviour.
return (0);
No brackets necessary around the 0:

return 0;
}

This is because the address to the new int is never passed back, so main()
has no knowledge of that new address. On the other hand:
The point in this instance is that you're modifying a local copy of a rather
than the one you passed to the function.
/* This works */
void Allocate(int* &var)
{
var = new int;
}

int main()
{
int* a;
Allocate(a);
*a = 5; // works
return (0);
}

This works because of the reference. Because of the reference, main() has
the knowledge of the new address.
To put it more precisely: Allocate now modifies the pointer you pass in
rather than a local copy of it, so the pointer a, which is local to main(),
is modified by the call (assuming, of course, that new succeeds). Thus
(assuming all went well), a points to a valid int when Allocate returns.

I know that didn't add a great deal, but I'm not sure I particularly like
the terminology "main() has the knowledge of the new address" (or even
"main() knows about the new address", I'm not into quibbling about grammar
particularly). The thing is that main(), as a C++ function, doesn't "know"
anything (it's not a living entity, right? :-)). I understand what you mean,
it's just a bit of a woolly way of putting it... :-) No offence intended,
incidentally, just trying to make it a little clearer to the OP.

HTH,

Stuart.
Note this does cause a memory leak, but I
didn't worry about deleting my pointers in this simple example, as this
example is not to teach you about pointers but rather references.

--
MiniDisc_2k2
To reply, replace nospam.com with cox dot net.

Jul 19 '05 #4
A little mistake, it means that function 'insert' can directly change "the
pointer" itself.
"Jakob Bieling" <ne*****@gmy.net>
???????:be*************@news.t-online.com...
"Yuri" <bo********@libero.it> wrote in message
news:Nq**********************@twister1.libero.it.. .
Hello,
I'm a c++'s newbie. In a BinarySearchTree data structure I found this
definition:
void insert( const Comparable & x, BinaryNode<Comparable> * & t ) const;
What does " *& " mean?
It' s a pointer to a reference? or what?

The other way round. It is a reference to a pointer, meaning that the
function 'insert' can directlt change the value of the pointer you are
passing to it.

hth
--
jb

(replace y with x if you want to reply by e-mail)

Jul 19 '05 #5
Alex Leung <al**@hitogo.com> wrote in message news:3f085b86$2@shknews01...
A little mistake, it means that function 'insert' can directly change "the
pointer" itself.


Isn't that what Jakob said?

"the function 'insert' can directlt change the value
of the pointer you are passing to it."

-Mike

Jul 19 '05 #6
In article <be**********@slb6.atl.mindspring.net>, mk******@mkwahler.net
says...

Yuri <bo********@libero.it> wrote in message
news:Nq**********************@twister1.libero.it.. .
Hello,
I'm a c++'s newbie. In a BinarySearchTree data structure I found this
definition:
void insert( const Comparable & x, BinaryNode<Comparable> * & t ) const;
What does " *& " mean?
It' s a pointer to a reference? or what?


Other way around. It's a reference to a pointer.


....and, in fact, a pointer to a reference isn't allowed.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 19 '05 #7
"MiniDisc_2k2" <Ma******@nospam.com> wrote in message
/* This works */
void Allocate(int* &var)
{
var = new int;
}

int main()
{
int* a;
Allocate(a);
*a = 5; // works
return (0);
}


I had to do this today for the first time, the same program also has
me working with an array of **, as well for the first time, a very fun
program :) Anyways I have a question about *&.

lets say instead of Allocate(int* &var) it's Allocate(char* &var) and
var = new char[64]. now if a is still int* when I pass it to Allocate
I would think I would only have to pass it as Allocate((char*)a)...
however my compiler (sun's optimized compiler) is complaning and it
wants me to pass it as Allocate((char*&)a) to work. is this normal?
Jul 19 '05 #8
"Chris Ritchey" <re*****@yahoo.com> wrote in message
news:48**************************@posting.google.c om...
"MiniDisc_2k2" <Ma******@nospam.com> wrote in message
/* This works */
void Allocate(int* &var)
{
var = new int;
}

int main()
{
int* a;
Allocate(a);
*a = 5; // works
return (0);
}


I had to do this today for the first time, the same program also has
me working with an array of **, as well for the first time, a very fun
program :) Anyways I have a question about *&.

lets say instead of Allocate(int* &var) it's Allocate(char* &var) and
var = new char[64]. now if a is still int* when I pass it to Allocate
I would think I would only have to pass it as Allocate((char*)a)...
however my compiler (sun's optimized compiler) is complaning and it
wants me to pass it as Allocate((char*&)a) to work. is this normal?


Yes, it's normal. Perhaps the error message Comeau C++ gives is a little
clearer?

"ComeauTest.c", line 9: error: initial value of reference to non-const must
be an
lvalue
Allocate((char*)a);

I think this is the relevant bit from the Standard, but it really needs
someone else to clarify it, because my ability to explain lvalues and
rvalues is minimal... :-) As I understand it, an l-value is anything you can
take the address of. Beyond that, I'll leave it to someone else, because
I'll as like as not get it wrong.

"3.10-6
An expression which holds a temporary object resulting from a cast to a
nonreference type is an rvalue (this includes the explicit creation of an
object using functional notation (5.2.3))."

What I think is happening is that (char*)a [which is clearly a cast to a
non-reference type] creates a temporary char pointer [which is an rvalue]
pointing to the same address as does a. Now since you can't bind a temporary
to a non-const reference, you can't pass (char*)a as the parameter to the
function. If the function took a char pointer by value, or by
const-reference, you'd be fine.

In any case, casting like this is inherently dodgy, and you really shouldn't
be doing it. 'Nuff said.

HTH,

Stuart.
Jul 19 '05 #9
> In any case, casting like this is inherently dodgy, and you really shouldn't
be doing it. 'Nuff said.


I agree, but I'm trying to build a char* from a linked link of linked
lists...
So what I'm doing is building the char*'s first for each of the inside
linked linsts, then allocating enough memory from the sum of all the
allocated char*. Thought i'd post some code just for kicks:
ComNode* current = members;
char** objGroups = new char*[numMembers];

cout << endl;
// The following loop is to generate strings for each ObjectGroup
created
// by each member, and to calculate the total size of the new
data chunk
for(int i = 0; i < numMembers, current != NULL; i++, current =
current->next)
{
objGroups[i] = (char*)current->member->StrToken();
cout << "|-------------------|" << endl;
cout << " MessageSize[" << i << "]: " << *((int*)objGroups[i])
<< endl;
cout << " NumberItems[" << i << "]: " <<
*(((int*)objGroups[i]) + 1) << endl;
messageSize += *((int*)objGroups[i]);
}
cout << "|-------------------|" << endl << endl;
// Begin Building the char*
toReturn = (int*)new char[(messageSize += 2 * SOF_INT +
SOF_GROUPTYPE + SOF_GROUPID)];
// Build the header for this char*
*toReturn = messageSize;
*(toReturn + 1) = numMembers;
data = toReturn + 2;
Jul 19 '05 #10

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

Similar topics

11
8069
by: Josh Lessard | last post by:
Hi all. I'm maintaining a C++ program and I've come across a nasty piece of code that works, but I just don't understand why. I'm not actually this part of the program, but I really want to know...
19
6804
by: Thomas Matthews | last post by:
Hi, Given a structure of pointers: struct Example_Struct { unsigned char * ptr_buffer; unsigned int * ptr_numbers; }; And a function that will accept the structure:
4
1287
by: DaKoadMunky | last post by:
My question relates to storing the addresses of functions generated by templates in pointers to functions. <CODE> template<class T> void Global() {} namespace ANamespace {
3
2530
by: Peter Goddard | last post by:
Hello, Is it possible, using casting to promote... void *Ptr; To behave like... void (*FnPtr)(void); ?
1
1698
by: ballpointpenthief | last post by:
I am trying to build a generic type for data structures in order to be able to build complex data structures based on other, simpler data structures. So, say we have linked lists, doubly linked...
0
1279
by: David Thompson | last post by:
On Wed, 09 Apr 2008 12:34:43 +0500, arnuld <arnVuld@ippiVmail.com> wrote: I think you've got the idea, but: - I would be careful about using 'equal'. Pointers and arrays are different things,...
3
2788
by: Ramesh | last post by:
Hi, I am trying to create an array of pointers to member functions inside my class. When I created a global array of type pfn & initialized with member functions and copy it back to the member...
0
7132
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
7223
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
7390
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...
0
5475
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,...
0
4602
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...
0
3103
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...
0
3094
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
665
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
302
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...

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.