+ 2
Are pointers reliable?
we use pointers in c++, but pointers are very dangerous because they can manipulate memory.
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.
+ 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;
}
+ 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
+ 2
We could but only if not used properly, and it applies on pointers too. @Rei