Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I am a little confused on both pointers and reference. From my understanding pointers are addresses in memory. If I pass a variable using pointers and reference to a function, any manipulations of the variable in the function will change the original variable.

I read online that passing by reference is not encouraged because it can get messy code easily. I also read here (http://stackoverflow.com/questions/7058339/c-when-to-use-references-vs-pointers), that we should avoid pointers if we can.

My question is when should pointers and reference be used? I know pointers are used in Xcode for strong references and reference counting. Since, higher level languages like c# have garbage collector, does that mean we shouldn't ever use pointers and reference in them?

share|improve this question
    
Your question would be easier to address if you pick a particular language (obj-C, C#, C++, Java, etc...) as the details can vary based upon the language. However, your question is still answerable even at the general language level with the understanding it may not cover your preferred language as well. –  GlenH7 Jul 31 '13 at 3:33

3 Answers 3

up vote 4 down vote accepted

I am a little confused on both pointers and reference. From my understanding pointers are addresses in memory.

In general, they are both addresses in memory. Usually one speaks of pointers if arithmetic can be done on them and of references if not, but there are some language-specific variations.

If I pass a variable using pointers and reference to a function, any manipulations of the variable in the function will change the original variable.

Yes.

But there are three reasons for passing something by reference:

  1. To avoid copying large amount of data to the stack.
  2. To modify the original variable.
  3. To support polymorphism, as the generated code does not know in advance how large the object will be and how to properly copy it.

Code using the second option is always more difficult to read than code using return values and if it is not used consistently, it is a sure way to create real mess. To distinguish this case from the other two C++ has the const modifier. Unfortunately Java and C# don't (and their authors don't seem to understand why it is useful; might have something to do with the fact that many people sell const in C/C++ as optimization tool which it certainly isn't).

I read online that passing by reference is not encouraged because it can get messy code easily.

High level languages hold everything by reference, because of the reason 3 above. They may pass the reference by value (just like passing pointer in C; the function can't change where the pointer points, but it can change content of the pointed object) or by reference (that is pointer-to-pointer; the function can make the variable point to different object), but it is always a reference to the actual object.

Java does that except for primitive numeric types and C# does it for everything that is class.

But passing by reference for the reason 2 above is indeed discouraged. One simply does not normally expect function to modify it's arguments, so better not do it.

I also read here (http://stackoverflow.com/questions/7058339/c-when-to-use-references-vs-pointers), that we should avoid pointers if we can.

That is C++-specific. References can't be set to nullptr (0 in older C++) so you don't have to check and references can't be deleted, so you don't have to think whether you should.

My question is when should pointers and reference be used?

Wherever they have to be.

I know pointers are used in Xcode for strong references and reference counting. Since, higher level languages like c# have garbage collector, does that mean we shouldn't ever use pointers and reference in them?

Managed languages (C#, Java, ...) don't have pointers at all. Pointers allow access to raw memory and that would interfere with the managed runtime. So they don't have them (C# does, in the special, "unsave", extension, to allow interfacing with plain old C libraries).

But references can't be not used in them. Everything is a reference. Because allocated memory can only be handled by pointer/reference. And because of polymorphism. So all variables of object types are references in Java and C# and all variables are references in anything dynamically typed like smalltalk, perl, python, ruby, javascript etc.

share|improve this answer
    
"High level languages pass everything by reference" – For many high-level languages this is not true. Java is pass-by-value, always. C is pass-by-value, always. C++ is pass-by-value by default, you have to explicitly ask for pass-by-reference. C# is pass-by-value by default, you have to explicitly ask for pass-by-reference. Smalltalk, Python, Ruby, ECMAScript, most Lisps etc. are pass-by-value, always. def is_ruby_pass_by_value?(foo) foo = 'Ruby is pass-by-reference.' end; bar = 'Ruby *is* pass-by-value!'; is_ruby_pass_by_value?(bar); p bar # 'Ruby *is* pass-by-value!' –  Jörg W Mittag Jul 31 '13 at 8:46
    
@JörgWMittag: In Java, everything is a reference. The reference is passed by value, yes. But it is a reference. (I'll reword it to make that clearer). –  Jan Hudec Jul 31 '13 at 8:51

I think of a pointer as what is returned from "alloc" or equivalent. In real systems, it's typically a reference to a chunk of memory, but not necessarily. Passing pointers around completely kosher, although undesciplined manipulation of the objects pointed to can be messy.

I think of a reference as a pointer I created rather than alloc, and for which "free" would not be appropriate. Use them extremely cautiously, for example to return multiple values from a function. It's extremely dangerous to treat a reference like a proper pointer, because if mistreated your pointer becomes a time bomb - for example taking a reference to a structure allocated on the stack, and allowing it to be retained when the stack frame it refers to is reclaimed.

share|improve this answer

There are 3 main reasons to use a pointer

  1. You do actually want to modify the things that you're passed. For example, in a swap function, you'd really like to actual swap the variables your given and not just copies of them. A more common example here is the this pointer/reference in C++, C#, Java, etc.

  2. For whatever reason, copying your argument is not feasible. For example, you don't want to copy a whole new 100000 item list, it's just too slow. Or if you have something that shouldn't be copied, like a mutex or some other valuable resource.

  3. This is much less common but sometimes you want to be able to go reach out and actually touch some random piece of memory or a register. This is very rare but quite possible in low enough code.

In these cases, pass a reference where possible and a pointer otherwise. Generally prefer references to pointers where possible since a pointer can be null.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.