Are pointers reliable? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Are pointers reliable?

we use pointers in c++, but pointers are very dangerous because they can manipulate memory.

27th Aug 2017, 6:59 AM
Subhan Mangi
Subhan Mangi - avatar
4 Answers
+ 12
Are knives reliable? We use them to cut things apart, but knives are very dangerous because we can also stab ourselves in the leg.
27th Aug 2017, 12:51 PM
Hatsy Rei
Hatsy Rei - avatar
+ 9
Pointers pros and cons summary: [https://www.quora.com/What-are-the-pros-and-cons-of-using-pointers-in-programming] ●● Pros: 1. Efficiency: Pointers as a indirect/direct reference to avoid copying and multiple copy synchronization e.g ... int number = 7; foo(&number); ... void foo(int *n) { ... cout << bar(n); ... } void bar(int *m) { ... *m += 2; ... } //////////////////// 2. Flexibility: Pointers can be used in many flexible data structures. Some features such as function pointer can be even treat as high-order structure to more abstract. [ http://www.cprogramming.com/tutorial/function-pointers.html] e.g void myFunc(int n) { cout << n; } int main() { void (*foo) (int); foo = &myFunc; // & is optional foo(7); // or (*foo) (7); } ////////////// ■■ Cons: 1. Unsafety: In C/C++, pointers are weak-typed and there's no routine to check NULL pointer/ dangling [Unless you wanted to use Smart pointers] [https://stackoverflow.com/questions/106508/what-is-a-smart-pointer-and-when-should-i-use-one] 2. Complexity: Resource(memory) management comes along with pointers, May cause other serious problem: Resource leak. e.g void leaker() { int *n = new int; .... // NO nullification. int *n = 0; // NO delete. delete n; }
27th Aug 2017, 8:13 AM
Babak
Babak - avatar
+ 2
pointers are one of the best things that you can meet out there! they speed up the process of everything, they are useful in countless of situations and if you know how tobuse pointers, you are cool! but mosly because they speed thing up. Of course they can be dangerous but only when you can't use them properly tho
27th Aug 2017, 7:14 AM
Paul
+ 2
We could but only if not used properly, and it applies on pointers too. @Rei
27th Aug 2017, 1:29 PM
Kartikey Sahu
Kartikey Sahu - avatar