Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Well, I basically understand how to use pointers, but not how best to use them in order to do better programming.

What are good projects or problems to resolve involving the use of pointers so I can understand them better?

share|improve this question
33  
Programming problems are created using pointers, not solved. :) –  xpda Sep 11 '11 at 22:16
4  
Actually, I wonder which problems can be solved without pointer. Very few for sure. –  deadalnix Sep 11 '11 at 23:43
3  
@deadalnix, it depends if you mean explicit pointers or implicit pointers. If you mean implicit pointers (ie, there are pointers used somewhere), then it would be impossible to write any program that uses the stack or the heap. If you mean explicit pointers then you are unrestrained, you can do any size of allocation by creating new stack frames (basically using a function call) and deallocation using tail calls, assuming they are optimized away by the compiler. –  dan_waterworth Sep 12 '11 at 6:19
2  
ugh, some of these answers are awful. –  acidzombie24 Sep 12 '11 at 6:55
show 10 more comments

17 Answers

up vote 65 down vote accepted

Manipulating large amounts of data in memory is where pointers really shine.

Passing a large object by reference is equivalent to just passing along a plain old number. You can manipulate the needed parts directly as opposed to copying an object, altering it, then passing back the copy to be put in place of the original.

share|improve this answer
10  
This is a great answer and I would add that you don't need large objects to be manipulating large amounts of data; occasionally manipulating large objects will do it, but an even more common problem is manipulating a bunch of small objects really really frequently. Which, if you think about it, is just about all computer programs. –  jhocking Sep 11 '11 at 20:20
add comment

Write any non-trivial project in C and you WILL have to figure how/when to use pointers. In C++ you will mostly use RAII-enabled objects which manage pointers internally, but in C raw pointers have a much more prevalent role. As for what kind of project you should do, it can be anything non-trivial:

  • A tiny and simple web server
  • Unix command line tools (less, cat, sort etc.)
  • Some actual project you want to do for its own sake and not just for learning

I recommend the last one.

share|improve this answer
3  
In my experience, doing real projects that are slightly out of your reach skill-wise is the best way to learn. Concepts can be learned by reading and writing small "toy" programs. However, to really learn how you can use those concepts to write better programs, you simply have to write non-toy programs. –  Viktor Dahl Sep 11 '11 at 20:08
show 1 more comment

Pixel level image manipulation is almost always easier and faster using pointers. Sometimes it is only possible using pointers.

share|improve this answer
1  
I don't think the statement "sometimes it is only possible using pointers" is true. There are languages that don't expose pointers to the developer, yet they can be used to solve problems in the same space. –  Thomas Owens Sep 11 '11 at 20:08
1  
On the contrary, image processing often involves "coordinate arithmetic" - say, taking the sine and cosine of an angle and multiplying it with the x- and y-coordinates. In other cases wrap-around or out-of-border pixel replication is needed. In this sense, pointer arithmetic is rather inadequate. –  rwong Sep 11 '11 at 22:41
show 6 more comments

One simple way is in Polymorphism. Polymorphism only works with pointers.

Also, you use pointers any time you need dynamic memory allocation. In C, this usually happens when you need to store data into an array but you do not know the size at compile time. You would then call malloc to allocate the memory and a pointer to access it. Also, whether you know it or not, when you use an array you are using pointers.

for(int i = 0; i < size; i++)
   std::cout << array[i];

is the equivalent of

for(int i = 0; i < size; i++)
   std::cout << *(array + i);

This knowledge allows you to do really cool things like copy an entire array in one line:

while( (*array1++ = *array2++) != '\0')

In c++, you use new to allocate memory for an object and store it into a pointer. You do this anytime you need to create an object during run-time instead of during compile time (i.e. a method creates a new object and stores it into a list).

To understand pointers better:

  1. Find some projects that play around with traditional c-strings and arrays.
  2. Find some projects that use inheritance.

  3. Here is the project that I cut my teeth on:

Read in two n x n matrices from a file and perform the basic vector space operations on them and print their result to the screen.

To do this, you have to use dynamic arrays and refer to your arrays by pointers since you will have two arrays of arrays (multi-dimensional dynamic arrays). After you finish that project, you will have a pretty good idea how to use pointers.

share|improve this answer
2  
"Polymorphism only works with pointers." Not accurate: polymorphism also works with references. Which are, ultimately, pointers, but I think the distinction is important, especially to the newbies. –  Laurent Pireyn Sep 11 '11 at 21:58
show 1 more comment

The pointer concept allows you to refer to data by address without duplicating the storage of data. This approach allows for writing efficient algorithms such as:

  1. Sorting
    When moving data in a sort algorithm you may move the pointer instead of the data itself—think of sorting millions of rows on a string of 100 chars; you save a lot of unnecessary data movements.

  2. Linked lists
    You can store the next and/or previous item location and not the entire data associated with the record.

  3. Passing parameters
    In this case, you pass the address of the data instead of the data itself. Again, think of a name compression algorithm that runs on millions of rows.

The concept can be extended to data structures such as relational databases where a pointer is akin to a foreign key. Some language don't encourage the use of pointers such as C# and COBOL.

Examples can be found in many places such as:

The following post may be relevant in some way:

share|improve this answer
14  
I wouldn't say C# doesn't encourage the use of pointers; instead, it doesn't encourage the use of unmanaged pointers - there's plenty of stuff in C# that gets passed around by reference instead of value. –  Blakomen Sep 12 '11 at 3:02
4  
Had to downvote for the C# comment. C# "references" are equivilent to pointers, it's just that C# abstracts you away from knowing that really you're just dealing with a pointer to an object in the managed heap. –  MattDavey Sep 12 '11 at 10:56
1  
@MattDavey: C# references are not pointers. You cannot add from them, use them to index an array, use multiple indirection, etc. You can't have references to references, for instance. –  Billy ONeal Sep 13 '11 at 21:24
4  
@Billy ONeal the notion that "it's only a pointer if you can do arithmetic on it" is frankly ludicrous, but I don't have the patience to argue with you. –  MattDavey Sep 14 '11 at 21:58
show 4 more comments

I'm surprised no other answer has mentioned this: pointers allow you to create non-contiguous and non-linear data structures, wherein an element may be related to multiple others in complex ways.

Linked lists (singly, doubly, and circularly linked), trees (red-black, AVL, trie, binary, space partitioning…), and graphs are all examples of structures that can be constructed most naturally in terms of references rather than just values.

share|improve this answer
show 2 more comments

To truly understand why pointers are important you need to understand the difference between heap allocation and stack allocation.

The following is an example of a stack allocation:

struct Foo {
  int bar, baz
};

void foo(void) {
  struct Foo f;
}

Objects allocated on the stack only exist for the duration of the current function execution. When the call to foo goes out of scope so does the variable f.

One case where this becomes an issue is when you need to return something other than an integral type from a function (for example the Foo structure from the example above).

For example, the following function would result in so-called "undefined behavior."

struct Foo {
  int bar, baz
};

struct Foo *foo(void) {
  struct Foo f;
  return &f;
}

If you want to return something like struct Foo * from a function what you really need is a heap allocation:

struct Foo {
  int bar, baz
};

struct Foo *foo(void) {
  return malloc(sizeof(struct Foo));
}

The function malloc allocates an object on the heap and returns a pointer to that object. Note that the term "object" is used loosely here, meaning "something" rather than object in the sense of object-oriented programming.

The lifetime of heap-allocated objects is controlled by the programmer. The memory for this object will be reserved until the programmer frees it, i.e. by calling free() or until the program exits.

Edit: I failed to notice this question is tagged as a C++ question. The C++ operators new and new[] perform the same function as malloc. The operators delete and delete[] are analogous to free. While new and delete should be used exclusively to allocate and free C++ objects, the use of malloc and free is perfectly legal in C++ code.

share|improve this answer
show 1 more comment

Almost all programming problems that can be solved with pointers can be solved with other safer types of references (not referring to C++ references, but the general CS concept of having a variable refer to the value of data stored elsewhere).

Pointers by being a specific low-level implementation of references, where you can directly manipulate memory addresses are very powerful, but can be slightly dangerous to use (e.g., point to memory locations outside the program).

The benefit of using pointers directly is that they'll be slightly faster by not having to do any safety checks. Languages like Java that don't directly implement C-style pointers will suffer a slight performance hit, but will reduce many types of difficult to debug situations.

As for why you need indirection, the list is fairly long, but essentially the two key idea are:

  1. Copying values of big objects is slow and will make the object be stored in RAM twice (potentially very costly), but copying by reference is near instantaneous using only a few bytes of RAM (for the address). E.g., say you have ~1000 big objects (each being about say 1MB of RAM) in memory, and your user needs to be able to select the current object (which will be acted upon by the user). Having a variable selected_object that is a reference to one of the objects is much more efficient than copying the value of the current object into a new variable.
  2. Having complicated data structures that refer to other objects like linked lists or trees, where each item in the data structure refers to other items in the data structure. The benefit of referring to other items in the data structure means that you don't have to move every item in the list around in memory just because you inserted a new item to the middle of the list (can have constant time inserts).
share|improve this answer
add comment

Pointers are used in so many programming languages below the surface without bugging the user about it. C/C++ just gives you access to them.

When to use them: As often as possible, because copying data is inefficient. When to not use them: When you want two copies that can be changed individually. (What basically will end up copying the content of object_1 to another place in memory and returning a pointer - this time pointing to object_2)

share|improve this answer
add comment

As a real life example, building a limit order book.

the ITCH 4.1 feed, for example, has a concept of "replace" orders, where prices (and therefore, priority) can change. You want to be able to take out orders and move them elsewhere. Implementing a double-ended queue with pointers makes the operation very easy.

share|improve this answer
add comment

Perusing the various StackExchange sites, I realize that it's rather in vogue to ask a question like this. At risk of criticism and downvotes, I'll be honest. This is not meant to troll or to flame, I am only meaning to help, by giving an honest assessment of the question.

And that assessment is as follows: This is a very strange question to ask of a C programmer. Pretty much all it says is "I don't know C." If I analyze a bit further and more cynically, there is an undertone of a follow-up to this "question": "Is there some shortcut I can take to quickly and suddenly acquire the knowledge of an experienced C programmer, without devoting any time for independent study?" Any "answer" that someone can give is no substitute for going and doing the legwork of understanding the underlying concept and its use.

I feel like it's more constructive to go learn C well, first hand, than to go on the web and ask people this. When you know C well you won't bother asking questions like this, it will be like asking "what problems are best solved by using a toothbrush?"

share|improve this answer
add comment

Pointers are an essential part to any data structure implementation in C and data structures are an essential part of any non-trivial program.

If you'd like to learn why it is that pointers are so vital, then I'd suggest learning what a linked list is and trying to write one without using pointers. I haven't set you an impossible challenge, (HINT: pointers are used to reference locations in memory, how do you reference things in arrays?).

share|improve this answer
show 1 more comment

Even though pointers really shine while working with large memory objects, there is still a way to do the same without them.

The pointer are absolutely essential for so-called dynamic programming, it is when you don't know how much memory you'll need before your program executes. In dynamic programming you can request memory chunks during runtime and place your own data into those -- so you need pointer or references (the difference is not important here) to be able to work with those data chunks.

As long as you can claim certain memory during runtime and place your data into newly acquired memory you can then do the following:

  1. You can have self-extending data structures. Those are structures that can extend themselves by claiming additional memory as long as their capacity runs out. The key property of every self-extending structure is that it consists of small memory blocks (termed nodes, list items etc. depending upon the structure) and every block contains references to other block(s). This "linked" structures build the majority of modern data types: graphs, trees, lists etc.

  2. You can program using OOP (Object-Oriented Programming) paradigm. The whole OOP is based upon using not direct variables, but references to class instances (termed objects) and manipulating them. No single instance can exist without pointers (even though it is possible to use static-only classes even without pointers, that is rather an exception).

share|improve this answer
add comment

Funny, i just answered a question about C++ and talked about pointers.

Short version is you NEVER need pointers unless 1) the library you are using forces you 2) You need a nullable reference.

If you need an array, list, string etc just have it on the stack and use a stl object. Returning or passing stl objects are fast(unchecked fact) because they have internal code that copies a pointer instead of an object and will only copy the data if you write to it. This is regular C++ not even the new C++11 which will make it easier on library writers.

Your question might be answered at this part

If you do use a pointer make sure its in one of these two conditions. 1) You are passing input that may be nullable. An example is an optional filename. 2) If you want to give ownership away. As in if you pass or return the pointer you do not have ANY copies of it remaining nor use the pointer you give away

ptr=blah; func(ptr); //never use ptr again for here on out.

But i havent used pointers or smart pointers for a very long time and i have profiled my application. It runs very fast.

ADDITIONAL NOTE: I notice that i do write my own structs and i do pass them around. So how do i do this without using pointers? Its not an STL container so passing by ref is slow. I always load my data list/deques/maps and such. I dont remember returning any objects unless it was some sort of list/map. Not even a string. I looked at code for single objects and i notice i do something like this { MyStruct v; func(v, someinput); ... } void func(MyStruct&v, const D&someinput) { fillV; } so i pretty much return objects (multiple) or preallocate/pass in a reference to fill (single).

Now if you were writing you're own deque, map, etc you'll need to use pointers. But you don't need to. Let STL and possibly boost worry about that. You just need to write data and the solutions. Not containers to hold them ;)

I hope you now never use pointers :D. Good luck dealing with libs that force you to

share|improve this answer
add comment

As your question is tagged C++, I will answer your question for that language.

In C++ there is a distinction between pointers and references, therefore there are two scenarios where pointers (or smart pointers) are necessary to facilitate certain behaviours. They can be used in other circumstances, however you ask how "best to use them", and in all other circumstances there are better alternatives.

1. Polymorphism

A base class pointer allows you to call a virtual method that is dependent on the type of object that pointer points to.

2. Creating persistent objects

Pointers are necessary when createing an object dynamically (on the heap instead of the stack). This is necessary when you want that objects lifetime to be longer than the scope in which it is created.

In terms of "good projects or problems to resolve", as others have said here already, any non-trivial project will make use of pointers.

share|improve this answer
show 1 more comment

Pointers are very useful for working with memory-mapped devices. You can define a structure that reflects (say) a control register, then assign it to the address of the actual control register in memory and manipulate it directly. You can also point directly to a transfer buffer on a card or chip if the MMU has mapped it into the system memory space.

share|improve this answer
add comment

I see pointers as my index finger, we use to do a couple of things:

  1. when someone asks you for something you can't carry, like where is street so and so, I would "point" at this street, and that's what we do in case of arguments by reference
  2. when counting or traversing through something, we would use the same finger, and that's what we do in arrays

please forgive this poor answer

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.