Relative Content

Tag Archive for pointers

C++ Pointers: Number of levels of Indirection

In a C++ program that doesn’t contain legacy C code, is there a guideline regarding the maximum number of levels of indirection that should be used in the source code? I know that in C (as opposed to C++), some programmers have used pointers to pointers for a multiple dimension array, but for the case of arrays, there are data structures in C++ that can be used to avoid the pointers to pointers.

What’s a good way to explain the need for pointing to a pointer?

Understanding what a pointer (an address) is, is fairly easy and an eleven-year old can understand it. But how do we express why we have a need for a pointer to a pointer? what is a very pedagogical example? Does it extend to pointing to a pointer to a pointer (and/or onward)?

What happens when using address before it’s allocated?

The very simple piece of C++ code below is incorrect, it’s easy to see why and tools like Valgrind will tell you. In running several C++ codes containing this kind of error, I noticed that each time, it ended up with a Segmentation fault at the line which tries to use the address.

C++ Typecasting VS performance

Let’s say we’re designing a video game. We have some sprites on the map and we want to call some method of the particular sprite at some particular position.