Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++: What are scenarios where using pointers is a "Good Idea"(TM)? [duplicate]

Tags:

c++

pointers

Possible Duplicate:
Common Uses For Pointers?

I am still learning the basics of C++ but I already know enough to do useful little programs.

I understand the concept of pointers and the examples I see in tutorials make sense to me. However, on the practical level, and being a (former) PHP developer, I am not yet confident to actually use them in my programs.

In fact, so far I have not felt the need to use any pointer. I have my classes and functions and I seem to be doing perfectly fine without using any pointer (let alone pointers to pointers). And I can't help feeling a bit proud of my little programs.

Still, I am aware that I am missing on one of C++'s most important feature, a double edged one: pointers and memory management can create havoc, seemingly random crashes, hard to find bugs and security holes... but at the same time, properly used, they must allow for clever and efficient programming.

So: do tell me what I am missing by not using pointers.

What are good scenarios where using pointers is a must?
What do they allow you to do that you couldn't do otherwise?
In which way to they make your programs more efficient?

And what about pointers to pointers???

[Edit: All the various answers are useful. One problem at SO is that we cannot "accept" more than one answer. I often wish I could. Actually, it's all the answers combined that help to understand better the whole picture. Thanks.]

like image 766
augustin Avatar asked Oct 27 '10 04:10

augustin


People also ask

What are the pointers and its advantages?

Pointers allow C to support dynamic memory management. Pointers reduce length and complexity of programs. Pointers increase the execution speed and thus reduce the program execution time. Pointers provide an efficient tool for manipulating dynamic data structures such as structure, union, linked list etc.


3 Answers

I use pointers when I want to give a class access to an object, without giving it ownership of that object. Even then, I can use a reference, unless I need to be able to change which object I am accessing and/or I need the option of no object, in which case the pointer would be NULL.

like image 187
Benjamin Lindley Avatar answered Oct 10 '22 04:10

Benjamin Lindley


This question has been asked on SO before. My answer from there:

I use pointers about once every six lines in the C++ code that I write. Off the top of my head, these are the most common uses:

  • When I need to dynamically create an object whose lifetime exceeds the scope in which it was created.
  • When I need to allocate an object whose size is unknown at compile time.
  • When I need to transfer ownership of an object from one thing to another without actually copying it (like in a linked list/heap/whatever of really big, expensive structs)
  • When I need to refer to the same object from two different places.
  • When I need to slice an array without copying it.
  • When I need to use compiler intrinsics to generate CPU-specific instructions, or work around situations where the compiler emits suboptimal or naive code.
  • When I need to write directly to a specific region of memory (because it has memory-mapped IO).
like image 44
Crashworks Avatar answered Oct 10 '22 02:10

Crashworks


Pointers are commonly used in C++. Becoming comfortable with them, will help you understand a broader range of code. That said if you can avoid them that is great, however, in time as your programs become more complex, you will likely need them even if only to interface with other libraries.

  • Primarily pointers are used to refer to dynamically allocated memory (returned by new).

  • They allow functions to take arguments that cannot be copied onto the stack either because they are too big or cannot be copied, such as an object returned by a system call. (I think also stack alignment, can be an issue, but too hazy to be confident.)

  • In embedded programing they are used to refer to things like hardware registers, which require that the code write to a very specific address in memory.

  • Pointers are also used to access objects through their base class interfaces. That is if I have a class B that is derived from class A class B : public A {}. That is an instance of the object B could be accessed as if it where class A by providing its address to a pointer to class A, ie: A *a = &b_obj;

  • It is a C idiom to use pointers as iterators on arrays. This may still be common in older C++ code, but is probably considered a poor cousin to the STL iterator objects.

  • If you need to interface with C code, you will invariable need to handle pointers which are used to refer to dynamically allocated objects, as there are no references. C strings are just pointers to an array of characters terminated by the nul '\0' character.

Once you feel comfortable with pointers, pointers to pointers won't seem so awful. The most obvious example is the argument list to main(). This is typically declared as char *argv[], but I have seen it declared (legally I believe) as char **argv.

The declaration is C style, but it says that I have array of pointers to pointers to char. Which is interpreted as a arbitrary sized array (the size is carried by argc) of C style strings (character arrays terminated by the nul '\0' character).

like image 31
Marc Butler Avatar answered Oct 10 '22 04:10

Marc Butler