0

I read this similar question but after a few weeks of studying C++ one thing I am curious about is: Why use pointers or references in C++?

Not even why use one over the other, but just why not use the original value?

By default things are passed by value in C++ wouldn't a lot of the confusion and accidental side effects would be removed if the original value were always passed?

Is it simply so that a smaller value can be passed on the stack?

Startec
  • 12,496
  • 23
  • 93
  • 160
  • This question is waayyy too broad. References and pointers are used frequently to support polymorphism, pass by reference is used to avoid copying of large objects, pointers are sometimes used as a way for a function to return failure, these are just some of the reasons why pointers and references are used. – yizzlez Aug 02 '15 at 03:28
  • @awesomeyi Yes I was worried about that. I didn't see a simple solution, I guess because one doesn't exist. However, the answers here, including your comments are helpful. I have no issue with this being close. – Startec Aug 02 '15 at 03:32
  • possible duplicate of [Why use pointers?](http://stackoverflow.com/questions/162941/why-use-pointers) – Maximillian Laumeister Aug 02 '15 at 03:32
  • It's a good question for learning, but far to broad and encouraging of opinions for SO, so I'm **voting to close**. But just to point in a good direction of an answer: this is extremely difficult to answer in the context of C++, but one can get some idea by considering original Jensen & Wirth Pascal, and Kernighan's critique of it (Brian Kernighan was one of the developers of original C). In particular how in Pascal one could not write a function that operated on any size array. – Cheers and hth. - Alf Aug 02 '15 at 03:33
  • 1
    I think the votes to close are from people reading the question they expect to see, not the question that was asked. –  Aug 02 '15 at 03:33
  • 1
    Kernighan's "Why Pascal is not my favorite language", referred above: (http://www.lysator.liu.se/c/bwk-on-pascal.html) – Cheers and hth. - Alf Aug 02 '15 at 03:34
  • 1
    @Startec I know this is closed, but I wanted to add something that is almost always overlooked. You need to use pointers in order to use memory that isn't on the stack. malloc() and new return a pointer to a chunk of memory that has been allocated for your use. The stack simply isn't large enough to hold everything you need in a program. If you try to hold everything on the stack you will end up with a "stack overflow". Languages like Java don't use pointers because all the memory handling is done by the language itself, so the pointers are hidden. In C and C++ it's done manually. – SeanRamey Oct 04 '17 at 01:27

2 Answers2

2

Many reasons. In general, its used because of performance.

Copying objects consumes memory, CPU and time. If you copy every object you pass as parameter to the functions you use them, even if this object doesn't have to be copied for any particular reason, you are making an application with poor performance.

It makes sense to pass every parameter that is not a primitive type, read structs and classes, as reference or pointer, unless you have a really good reason not to do so.

You may also want to pass an object or a variable to a function that will then modify it on it's original place. If you pass a copy of this variable, the function won't be modifying it, it will be modifying just a temporary, volatile copy of it. You have to pass this variable or object as reference or pointer so that the function have means to modify it.

Havenard
  • 27,022
  • 5
  • 36
  • 62
1

If you want to use the original structure or value, pretty much the only option is to use a reference or a pointer; e.g.

void pass_by_value(int x)
{
    // x is a *copy* of whatever gets passed in, not the original
}

void pass_by_reference(int &x)
{
    // x refers what got passed in, not a copy of what got passed in
}