Question:
Why does C++ have pointers?
?
2010-12-25 11:48:41 UTC
Why does C++ have pointers and why should I use it when I can declare variables in dynamic way as in Java/PHP/VB etc.
Five answers:
The Phlebob
2010-12-25 12:09:49 UTC
Java and VB also have pointers. They just hide them from programmers to make things simpler.



Programs use pointers for several reasons:



1. It's faster to pass a pointer to a large data structure (an array, an object) to a function than it is to copy and pass the structure itself.

2. If a function has to modify a passed parameter, it can't access just a copy of it. It must access the actual parameter.

3. Pointers can be shared among entities and can so save the space multiple copies would require.

4. Some sophisticated data structures (lists, queues, stacks) just would not work without pointers.



Hope that helps.
jplatt39
2010-12-25 13:13:56 UTC
C++ has pointers because C had pointers. And to declare pointers dynamically in C -- or to pass variables by reference rather than value -- you need to declare a pointer to the type and either allocate memory or pass it the address of the parameter. The unary amperstand (&) of course is the address of operator. C provides a broader variety of tools for this sort of thing, as it does for abstraction and type security, however C is a midlevel language rather than a high level language, and while not intended specifically for any one chipset or hardware really shines at modelling hardware logically -- so it's been ported to most of them.



Actually, you will find that if you read the specs, C++ implements a lot of this dynamic stuff under the hood by using pointers. And you should read the specs and try to understand both them and pointers (I'm talking about Bjarne Stroustrup's book). Don't just be a consumer of compilers -- be an educated one.
husoski
2010-12-25 13:00:00 UTC
C++ has pointers specifically because it inherits C's memory and array model. Dynamic memory allocated with malloc()/calloc() must be explicitly released with free(); dynamic objects created with new must be explicitly released with delete or delete[].



You can't use Java-style dynamic syntax in C++. The new operator returns a pointer, not an object.



string s = new string("invalid indirection error");



...fails because the string s is not compatible with the (string*) returned by new. You could hack this to look almost like Java:



string s = *new string("memory leak");



This compiles, and will:

1. create a new string object named "s" in the runtime stack

2. create a new unnamed string object on the heap and initialized it to "memory leak"

3. call the string assignment operator on s with the unnamed string as an argument, which will copy the unnamed string's data, without changing or destroying it

4. forget about the unnamed string...there's no way to delete it because it has no name.



Wrap that in an infinite loop and watch your system's memory utilization climb until the program finally throws an out-of-memory exception. This is the sort of thing that C++, like C with malloced memory that doesn't get freed, will silently let you do; and it's called a "memory leak" because, while your program is running it looks like available memory is leaking out of a hole somewhere.



Proper handling of pointer variables is essential to effective C++ programming. References work great for function/method arguments, but reference variables are nearly useless.



Edit: Fixed a couple of typos.
modulo_function
2010-12-25 11:55:51 UTC
Why shouldn't C++ have pointers?



+add

Let me add something to what The Phelbob said. Java is pass by value. Note that this means that when you pass an object, the address of that object is passed. This address is a pointer! When you change field of the object you are changing fields of the original object and so you do get the same effect as pass by reference. This leads to the somewhat odd effect of being able to change fields of an object by not the values of primatives, like int, char, etc.
?
2010-12-25 12:01:46 UTC
The main use of pointers is the virtual functions and polymorphism which is what makes C++ and C the best languages out there


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...