0

Okay, after ignoring and skipping pointers at any chance I got (They looked really complicated when I first started learning C++ >.<), I finally decided to learn what they are and how to use them.

So now I know how to use pointers, the only thing I can't work out is exactly why you would want to use them. I've been searching all over the internet for almost 7 hours now and I still can't find a suitable explanation.

So please, could someone explain to me what the point of using pointers is? (show me an example if you can, preferably an example of a function that does something relatively useful so I can understand).

Toon Krijthe
  • 52,876
  • 38
  • 145
  • 202

3 Answers3

3

A few choice examples, there are many, many more:

Michael Wild
  • 24,977
  • 3
  • 43
  • 43
2

A pointer is just a number storing a numerical index for a memory location. Every non-trivial program in every language needs such numbers to keep track of where interesting functions and data are. In some languages the use of pointers is hidden - you just see some identifier like "employee_name" and internally the compiler or interpreter has arranged for storage and passes the pointer thereto around as it needs to to make the program behave as stipulated by the language, but they're always there underneath.

If you can't see the pointer use, you can't always reason about which operations in the language are cheap and which incur costs.

In some dumbed-down languages like VB, it used to be that more advanced data types like associative containers (std::map, std::unordered_map), linked lists, graphs etc. weren't available; clumsy functions would have to be used that internally used C or C++ to track the relationships between data elements. Working directly in C++, you can create such data types yourself to model arbitrary relationships with the precise behavioural, performance and memory use compromises that suit you.

C++ exposes pointers so that you have explicit control over whether the memory area used by an existing object is passed around different parts of the program, potentially granting them access to change the value or even deallocate the memory. This is more efficient than other languages like Java, Ruby and C# where it's less obvious which code has access to some data versus a copy of that data, when the copying can happen, when the data is no longer needed. Things like garbage collection exist to try to track use of data, but typically introduce performance issues, inefficiencies and unpredictable timing of destruction - whether than matters depends on the application.

Tony Delroy
  • 102,968
  • 15
  • 177
  • 252
0

The very basic reason is the effective usage of stack and heap.

Stack has a very limited size and it is a container that holds variables.

The heap (or "free store") is a large pool of memory used for dynamic allocation. It's size limited to the available space in your dynamic memory (RAM).

If you try to store everything in the stack without dynamic allocation and pointers you will get into stack overflow.

You can also imagine heap as a country to travel and stack is your guide which you can get info about the location of restaurants, hotels, etc..

fatihk
  • 7,789
  • 1
  • 26
  • 48