Naija Ryders

Go Back   Naija Ryders > Main > Techie Forum
Connect with Facebook
Register Blogs FAQ Members List Guitar Hero Calendar Mark Forums Read

Reply
 
LinkBack Thread Tools Display Modes
Old 01-15-2007, 08:15 PM   #1 (permalink)
Registered User
 
djmightymike's Avatar
 
Join Date: Dec 2006
Location: Festac Town
Posts: 8,638
Points: 1,278,100,513.38
Bank: 0.02
Total Points: 1,278,100,513.40
Donate
Rep Power: 21474865 djmightymike is a legenddjmightymike is a legenddjmightymike is a legenddjmightymike is a legenddjmightymike is a legenddjmightymike is a legenddjmightymike is a legenddjmightymike is a legenddjmightymike is a legenddjmightymike is a legenddjmightymike is a legend



Default C++ DeMystefied - References Vs. Pointers

In C++, references provide many of the same capabilities as pointers. Although most C++ programmers seem to develop some intuition about when to use references instead of pointers, and vice versa, they still encounter situations where the choice isn't so clear. If you'd like to develop a reasonably consistent philosophy about using references, it really helps to know exactly how references differ from pointers. Those differences are my subject this month.

More than Skin Deep
A reference, like a pointer, is an object that you can use to refer indirectly to another object. A reference declaration has essentially the same syntactic structure as a pointer declaration. The difference is that while a pointer declaration uses the * operator, a reference declaration uses the & operator. For example, given:

int i = 3;

then:

int *pi = &i;

declares pi as an object of type "pointer to int" whose initial value is the memory address of object i. On the other hand:

int &ri = i;

declares ri as an object of type "reference to int" referring to i. The difference between pointer and reference declarations is noteworthy, but it's not the basis for deciding to use one over the other. The real basis for the choice is the difference in appearance between references and pointers when you use them in expressions.

The big difference between pointers and references is that you must use an explicit operator-the * operator-to dereference a pointer, but you don't use an operator to dereference a reference. For example, once the previous declarations are in place, the indirection expression *pi derefences pi to refer to i. In contrast, the expression ri-without any operators at all-dereferences ri to refer to i. Thus, an assignment such as: *p = 4;

changes the value of i to 4, as does the assignment:

ri = 4;

This difference in appearance is significant when you're choosing between pointers and references for function parameter types and return types. This is especially true in functions that declare overloaded operators.

References vs. const pointers

C++ doesn't let you declare a "const reference" because a reference is inherently const. In other words, once you bind a reference to refer to an object, you cannot rebind it to refer to a different object. There's no notation for rebinding a reference after you've declared the reference. For example:

int &ri = i;

binds ri to refer to i. Then an assignment such as:

ri = j;

doesn't bind ri to j. It assigns the value in j to the object referenced by ri, namely, i.

In short, whereas a pointer can point to many different objects during its lifetime, a reference can refer to only one object during its lifetime. Some people claim that's a significant difference between references and pointers. I'm not sold on the idea. Maybe this is a difference between references and pointers, but it's not a difference between references and const pointers. Again, once you bind a reference to an object, you can't change it to refer to something else. Since you can't change the reference after you bind it, you must bind the reference at the beginning of its lifetime. Otherwise, the reference will never be bound to anything and will be useless, if not downright dangerous.

All of the statements in the previous paragraph apply to const pointers just as much as they do to references. (I'm talking about "const pointers" here, not about "pointers to const.") For example, a reference declaration at block scope must have an initializer, as in:

void f()
{
int &r = i;
...
}

Omitting the initializer produces a compile-time error:

void f()
{
int &r; // error
...
}

A const pointer declaration at block scope must also have an initializer, as in:

void f()
{
int *const p = &i;
...
}

Omitting the initializer is just as much an error:

void f()
{
int *const p; // error
...
}

In my opinion, the fact that you can't rebind a reference is no more a difference between references and pointers than that which exists between const pointers and non-const pointers.

Null references

Appearances aside, const pointers differ from references in one subtle but significant way. A valid reference must refer to an object; a pointer need not. A pointer, even a const pointer, can have a null value. A null pointer doesn't point to anything.

This difference suggests that you should use a reference as a parameter type when you want to insist that the parameter refer to an object. For example, consider a swap function, which accepts two int arguments and swaps the value of its first argument with the value of its second argument. For example:

int i, j;
...
swap(i, j);

leaves the value that was in i in j, and the value that was in j in i. You could write this function as:

void swap(int *v1, int *v2)
{
int temp = *v1;
*v1 = *v2;
*v2 = temp;
}

so that a call looks like: swap(&i, &j);

This interface suggests that either or both arguments might be a null pointer. The suggestion is misleading. For example, the consequences of calling: swap(&i, NULL);

are likely to be unpleasant. Defining the function with reference parameters, as in:

void swap(int &v1, int &v2)
{
int temp = v1;
v1 = v2;
v2 = temp;
}

clearly suggests that a call to swap should provide two objects whose values will be swapped. As an added bonus, calls to this function look nicer without the &s cluttering things up:

swap(i, j);

Greater safety?

Some people take the fact that a reference can't be null to mean that references are somehow safer than pointers. They may be a little safer, but I don't think they're a lot safer. Although a valid reference can't be null, an invalid one can be null. In fact, there's a ton of ways programs can produce invalid references, not just null references. For example, you can define a reference so that it refers to the object addressed by a pointer, as in:

int *p;
...
int &r = *p;

If the pointer happens to be null at the time of the reference definition, the reference is a null reference. Technically, the error is not in binding the reference, but in dereferencing the null pointer. Dereferencing a null pointer produces undefined behavior, which means lots of things could happen, and most of them aren't good. It's likely that, when the program binds reference r to *p (the object that p points to), it won't actually dereference p. Rather, it will just copy the value of p to the pointer that implements r. The program will keep running only to have the error manifest itself more overtly sometime later during program execution. Oh joy.

The following function shows another way to produce an invalid reference:

int &f()
{
int i;
...
return i;
}

This function returns a reference to local variable i. However, the storage for i vanishes as the function returns. Thus, the function returns a reference to deallocated storage. The behavior is the same as returning a pointer to a local variable. Some compilers do detect this particular error at compile time. However, you can disguise the sin so it will go undetected.

I like references. There are good reasons to use them instead of pointers. But, if you expect that using references will make your program significantly more robust, you're likely to be disappointed.

Last edited by djmightymike; 01-15-2007 at 08:22 PM..
djmightymike is offline   Reply With Quote
Old 01-17-2007, 03:17 AM   #2 (permalink)
Registered User
 
aba made's Avatar
 
Join Date: Jun 2003
Location: waterside
Posts: 5,897
Points: 1,246,715.09
Bank: 1.15
Total Points: 1,246,716.24
Donate
Rep Power: 21474866 aba made is a legendaba made is a legendaba made is a legendaba made is a legendaba made is a legendaba made is a legendaba made is a legendaba made is a legendaba made is a legendaba made is a legendaba made is a legend
Default

ol' boy, u get time mehn. anyway hook me up with a free license of ws_ftp. i dey try keep my work notebook warez free

btw there are ways to declare a const reference.
__________________
It is the mark of an educated mind to be able to entertain a thought without accepting it. - Aristotle

Last edited by aba made; 01-17-2007 at 03:20 AM..
aba made is offline   Reply With Quote
Old 01-18-2007, 06:34 AM   #3 (permalink)
Registered User
 
djmightymike's Avatar
 
Join Date: Dec 2006
Location: Festac Town
Posts: 8,638
Points: 1,278,100,513.38
Bank: 0.02
Total Points: 1,278,100,513.40
Donate
Rep Power: 21474865 djmightymike is a legenddjmightymike is a legenddjmightymike is a legenddjmightymike is a legenddjmightymike is a legenddjmightymike is a legenddjmightymike is a legenddjmightymike is a legenddjmightymike is a legenddjmightymike is a legenddjmightymike is a legend



Default

Quote:
Originally Posted by aba made
ol' boy, u get time mehn. anyway hook me up with a free license of ws_ftp. i dey try keep my work notebook warez free

btw there are ways to declare a const reference.
I dont work on the ftp project, but i'll talk to my ftp guys and see if i could get a freebie.

I'm interested in seeing how u could declare a const reference in c++
djmightymike is offline   Reply With Quote
Old 01-18-2007, 03:44 PM   #4 (permalink)
Registered User
 
aba made's Avatar
 
Join Date: Jun 2003
Location: waterside
Posts: 5,897
Points: 1,246,715.09
Bank: 1.15
Total Points: 1,246,716.24
Donate
Rep Power: 21474866 aba made is a legendaba made is a legendaba made is a legendaba made is a legendaba made is a legendaba made is a legendaba made is a legendaba made is a legendaba made is a legendaba made is a legendaba made is a legend
Default

Quote:
Originally Posted by djmightymike
I dont work on the ftp project, but i'll talk to my ftp guys and see if i could get a freebie.

I'm interested in seeing how u could declare a const reference in c++
havent coded in a while but i think something like this should work. hopefully....

typedef int &val;
val const x = y;


as per the license...if u fit i go appreciate am.
__________________
It is the mark of an educated mind to be able to entertain a thought without accepting it. - Aristotle
aba made is offline   Reply With Quote
Old 01-18-2007, 09:28 PM   #5 (permalink)
Registered User
 
Join Date: Jan 2007
Posts: 10
Points: 54.00
Bank: 0.00
Total Points: 54.00
Donate
Rep Power: 0 vinmoore is on a distinguished road
Default

Quote:
Originally Posted by aba made
ol' boy, u get time mehn. anyway hook me up with a free license of ws_ftp. i dey try keep my work notebook warez free

btw there are ways to declare a const reference.
Djmightymike, I think the note is just too long for one post, plz try flow in beats. Please wats ur root subjt.
vinmoore is offline   Reply With Quote
Old 01-18-2007, 09:48 PM   #6 (permalink)
Registered User
 
djmightymike's Avatar
 
Join Date: Dec 2006
Location: Festac Town
Posts: 8,638
Points: 1,278,100,513.38
Bank: 0.02
Total Points: 1,278,100,513.40
Donate
Rep Power: 21474865 djmightymike is a legenddjmightymike is a legenddjmightymike is a legenddjmightymike is a legenddjmightymike is a legenddjmightymike is a legenddjmightymike is a legenddjmightymike is a legenddjmightymike is a legenddjmightymike is a legenddjmightymike is a legend



Default

Quote:
Originally Posted by vinmoore
Djmightymike, I think the note is just too long for one post, plz try flow in beats. Please wats ur root subjt.
i dint get the last part. My root subject u meant?

@abamade - Check your pm for link to ws_ftp2k6
djmightymike is offline   Reply With Quote
Old 03-03-2007, 06:30 PM   #7 (permalink)
Elder
 
Sir Cristao's Avatar
 
Join Date: Feb 2004
Location: White Hart Lane
Posts: 10,065
Points: 6,000.16
Bank: 1,487,655.04
Total Points: 1,493,655.20
Donate
Rep Power: 5163588 Sir Cristao is a legendSir Cristao is a legendSir Cristao is a legendSir Cristao is a legendSir Cristao is a legendSir Cristao is a legendSir Cristao is a legendSir Cristao is a legendSir Cristao is a legendSir Cristao is a legendSir Cristao is a legend
Default

headaches!!
Sir Cristao is offline   Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are Off
Pingbacks are Off
Refbacks are On

Points Per Thread View: 0
Points Per Thread: 0
Points Per Reply: 0
Forum Jump


All times are GMT +2. The time now is 09:30 PM.


Powered by vBulletin® Version 3.8.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.2.0
NaijaRyders