answersLogoWhite

0


Best Answer

When to use pointers and references?

Pointers and references look different enough (pointers use the "*" and "->" operators, references use "."), but they seem to do similar things. Both pointers and references let you refer to other objects indirectly. How, then, do you decide when to use one and not the other?

First, recognize that there is no such thing as a null reference. A reference must always refer to some object. As a result, if you have a variable whose purpose is to refer to another object, but it is possible that there might not be an object to refer to, you should make the variable a pointer, because then you can set it to null. On the other hand, if the variable must always refer to an object, i.e., if your design does not allow for the possibility that the variable is null, you should probably make the variable a reference.

"But wait," you wonder, "what about underhandedness like this?"

char *PC = 0; // set pointer to null char& RC = *PC; // make reference refer to // dereferenced null pointer

Well, this is evil, pure and simple. The results are undefined (compilers can generate output to do anything they like), and people who write this kind of code should be shunned until they agree to cease and desist. If you have to worry about things like this in your software, you're probably best off avoiding references entirely. Either that or finding a better class of programmers to work with. We'll henceforth ignore the possibility that a reference can be "null."

Because a reference must refer to an object, C++ requires that references be initialized:

string& rs; // error! References must // be initialized string s("xyzzy"); string& rs = s; // okay, rs refers to s

Pointers are subject to no such restriction:

string *PS; // uninitialized pointer: // valid but risky

The fact that there is no such thing as a null reference implies that it can be more efficient to use references than to use pointers. That's because there's no need to test the validity of a reference before using it:

void printDouble(const double& rd) { cout << rd; // no need to test rd; it } // must refer to a double

Pointers, on the other hand, should generally be tested against null:

void printDouble(const double *pd) { if (pd) { cout << *pd; } }

Another important difference between pointers and references is that pointers may be reassigned to refer to different objects. A reference, however, always refers to the object with which it is initialized:

string s1("Nancy"); string s2("Clancy"); string& rs = s1; // rs refers to s1 string *PS = &s1; // PS points to s1 rs = s2; // rs still refers to s1, // but s1's value is now // "Clancy" PS = &s2; // PS now points to s2; // s1 is unchanged

In general, you should use a pointer whenever you need to take into account the possibility that there's nothing to refer to (in which case you can set the pointer to null) or whenever you need to be able to refer to different things at different times (in which case you can change where the pointer points). You should use a reference whenever you know there will always be an object to refer to and you also know that once you're referring to that object, you'll never want to refer to anything else.

There is one other situation in which you should use a reference, and that's when you're implementing certain operators. The most common example is operator[]. This operator typically needs to return something that can be used as the target of an assignment:

vector v(10); // create an int vector of size 10; // vector is a template in the // standard C++ library

v[5] = 10; // the target of this assignment is // the return value of operator[]

If operator[] returned a pointer, this last statement would have to be written this way:

*v[5] = 10;

But this makes it look like v is a vector of pointers, which it's not. For this reason, you'll almost always want operator[] to return a reference.

References, then, are the feature of choice when you knowyou have something to refer to, when you'll never want to refer to anything else, and when implementing operators whose syntactic requirements make the use of pointers undesirable. In all other cases, stick with pointers.

User Avatar

Wiki User

12y ago
This answer is:
User Avatar
More answers
User Avatar

AnswerBot

1mo ago

Pointers are used when we need to directly access and manipulate memory addresses of variables. References are used when passing variables to functions by reference and when we want to have alias for a variable without the need for memory management. Pointer arithmetic and dynamic memory allocation are common use cases for pointers.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: When to use pointers and references?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

How do you write a reverse function in C plus plus without pointers?

You can either use references or you can simply return the result by value. Note that in C++, unlike C, references are not the same as pointers. C++ references are aliases, alternate names for existing objects, whereas pointers are just variables that may contain a memory address that can be dereferenced.


How pointers are used in java?

They aren't. Java uses the (safer) idea of references instead of pointers.


Can you use data structure in java?

Yes. Its just (sort of) like C and C++. The difference is that Java does not have pointers, it has references, but, philosophically, its the same thing.


In C you use the concept of pointers whereas there are no pointers used in JAVA why?

Pointers in C are generally the thing that gives learners the most trouble. When C code is not written correctly with respect to pointer use, the resulting bugs can often be very difficult to find and correct. On the other hand, pointers are absolutely necessary in some cases.The designers of Java wanted to make programming easier and hence avoided adding pointers to the language. Java does have object references which accomplish much of what pointers accomplish albeit in a safer way.


Can you use pointers in java?

Java does not support Pointers and hence you cannot use it in Java.


What is dangling pointer reference in c plus plus?

A dangling pointer (we also use the terms stray pointer and wild pointer) is created whenever we call delete on a pointer and then try to use the pointer without reassigning it.We can also create dangling pointers inadvertently by calling a rogue function that returns a pointer to an object that is local to the function we are calling. The object will fall from scope when the function returns so the pointer is left dangling.Note that there is no such thing as a dangling pointer reference. Pointers and references are not the same. A reference is merely an alias to an object -- it consumes no memory beyond the object it refers to. Whereas a pointer is a variable that may contain the address of an object, but it requires additional memory to do so (4 bytes on 32-bit architecture). Pointers may be NULL, references can never be NULL. Pointers to valid objects require indirection, references do not. References are the preferred method of accessing an object's members, not least because they are easier to work with.


Do you use pointers in java?

no


Why to use pointers in programs?

To use memory effectively. . .


What are five safety pointers for use in electronic shop work?

safety pointers in electric shop


Pointers are include in csharp or not?

Yes, you can use pointers in the C#, but to some extent. Links are added with more details.


What kind of drills do you use for three pointers two pointers and foul shots?

just keep shooting its not that hard


What is the difference between handles and pointers?

They exist in different contexts. Handles are like... well handles (or keys, tickets, references etc), to access objects (like files, sockets, resources) you have opened (created, allocated, etc); in the program they can be integers or pointers.