In which case we generally use pointers? Can we predict there output? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

In which case we generally use pointers? Can we predict there output?

14th Feb 2017, 12:52 PM
Swapnil Mali
Swapnil Mali - avatar
2 Answers
+ 1
Suppose i have 10,000,000 names stored. I want to make a method to sort all the names in alpha order. I don't want to copy ALL 10,000,000 and send to my program to sort...... my computer will DIE.... So i send my program the address where my 10,000,000 names are stored. i save a lot of time. Can we predict the output ? Usually NO. Suppose data is stored in RAM, the data will not ALWAYS be in the same location, so the address will not be the same always...
14th Feb 2017, 2:06 PM
Michael W
Michael W - avatar
0
Summarilly pointers are the only actual abstraction showing access to underlying memory and you need them for any kind of controled memory allocation. If you remove them from the language, you are losing something that can't be replaced. Going to details, most pointer uses can be replaced by references, but there is still a visible few cases where references can't replace them. void pointers is such a case. There is some functions expecting the address of something where something is untyped (like memcpy). Accessing hardware from addresses is another such case. Heap memory allocation (through malloc or new) implies pointers (even if you can dereference return of new to assign to reference). To remove pointers we would have to change new to something else. References provides no tool for memory allocation. And there is even some cases where underlying implementation is horrible. Also a reference is unique henceforth you can't really change it and this ability is needed for many dynamic data structures. To change that you would have to change both new and delete to something else (for instance the Java way), but doing so you are losing control on the low level. What could probably be removed is equivalence between pointers and arrays. This is an ongoing process as there is more and more restrictions on the subject, like C++ forbidding to compute some intermediate address out of the array (except the next to last) even if you never dereference it. To my eyes a C++ without pointers should look very much like Java (if we ignore other differences like templates). If this is a good evolution or not is another question. I would say if it evolves this way, C++ won't fit the "portable assembly" historic ecological niche of C language any more. Henceforth we would need another low-level object oriented language to replace it for that purpose. Taken from Christophe Grosjean -Agile Developer( quora.com/Why-are-pointers-used-in-C-C++ )
14th Feb 2017, 1:30 PM
Roman Santa
Roman Santa - avatar