What are C++ pointers really used for?

Asked

Viewed 701 times

3

Where pointers are actually used?

I’ve searched so many places and the example is always theoretical.

I would like to know a real situation, an example within a real project in which pointers are used.

Other doubts are:

If I’m not using pointers, this is wrong?

I can do everything in C++ using only variables?

  • 1

    Ué, has the same used as in C. Serves to reference a memory space, a simple variable or an object.

  • But why reference a space in memory? where does this apply? as I said I already understood this part but do not know where it applies.

  • 2

    When you want to use a more complex data structure, or something with a different size than the native types. You will have to allocate the memory in the arm and save a pointer to that memory space. However, pointers are for many things. It goes from your need or creativity. Remembering that C++ is based on C. If you want to understand pointer, look for tutorials that show how to use and what it serves for in C.

  • Objects can be placed in three places: the stack, the heap and the global variables section. Objects in the heap can only be accessed via pointers, because they may or may not be there, the heap is a kind of "no man’s land". Pointer is essentially an annulable reference (or a reference is a pointer that can never be null), the language syntax ensures that global or stack variables always exist, but not in heap.

1 answer

3


Maybe you are looking for information in the wrong order. Maybe you still lack a handful of more basic information before you get to the pointers. Perhaps it is studying the subject without a structure. When we study step by step, in the right order the need of the pointer appears naturally.

In general we should not seek a use for a mechanism. We seek the mechanism when we have a need.

Variable

Let’s start with the basics: a variable is a data storage site with a name. It is an abstract concept that we use to better understand the code. Computers do not have and do not understand variables.

In these storage locations we have values that will be useful in the processing we are performing.

If these values do not need to be referenced nominally we do not need variables, we can only work with values. The variable is a useful instrument to access the same value more than once easily. It is also used when a complex intermediate step is required. In some cases it can be used only for documentation of what you are doing.

The data that is stored in memory can be by value, that is, the useful data is in the storage location, possibly with a name. Or the data can be by reference, so there are two storage locations, one of them the reference telling where the useful data is, and another is that useful data location.

Memoriam

This reference is like a postal address, and the data is the house where the family lives. The reference is implemented as a pointer. There are several forms of pointer in C++.

It is common that the pointers point to a location in the heap. But it is possible to point to other areas of memory, such as the stack and the static area of the code. See more about this in What are and where are the "stack" and "heap"?.

In general, access to heap is done through pointers.

It is possible to do everything without pointers, only with direct values?

Yes, in theory it’s possible.

But you want to know in practice, and then the answer is no. First that the performance would be tragic having to keep copying large data all the time, would have to always have memory reserved for everything up to the maximum that is allowed, which would be a huge waste, could not make use of repeated data, a series of techniques would be impossible, such as the composition of data and the dynamic exchange of data.

Pointer should be one of the 5 or 6 most important things to learn in C++ coding, even though it is not so often used.

And if you’re going to learn C++, learn C++, don’t learn C and think you’re learning C++. They’re very compatible languages, but totally different. In C pointers are more directly used. Do not fall into the error of using C++ to program C, as many do. You can learn both languages, but the first thing is to understand that they are very different and should be used in different ways.

If I’m not using pointers this is wrong?

I don’t know, it depends on the case. There is no single answer. There is no cake recipe, there are no universal "good practices". There is the need and the mechanism that solves it, has to learn everything about the subject, experiment (to gain experience) to understand when to use and when not to use.

The idea is to avoid using until necessary. If you have no gain using a pointer, do not use.

I’ve searched so many places and the example is always theoretical

In general this is what you will find. Unless you go digging through existing codes, they are all very practical examples. It is a good way to complement studies. Today there is no shortage of open source projects.

If you ask a theoretical question, you will receive a theoretical answer. If you look for theory, you will find theory. Good, without it it is not possible to understand what is happening. And no one really programs without understanding the theory.

Of course I delete anyone who copies what has been done before and fills in gaps. This is not programming.

One of the biggest problems to answer that is that I can answer and you stay the same because I don’t know what you already know. When there is a structured study, where no steps are skipped, each new knowledge is given knowing what you already know.

In example without contact probably won’t be for much.

Pointer use

The question is not even clear whether you are talking about raw pointer or any form of pointer. In thesis on an entire project it is relatively easy to go without using raw pointers, as long as you don’t use external libraries that result in a raw pointer.

Every time you need to store data dynamically (heap), will use pointer. If you will do this with raw pointer, with a reference, with a smart Pointer, or if the object you will use makes the pointer treatment for you transparently, it is a matter of choice according to need. What I can say is that raw pointers are avoided in modern C++ . If you can’t use reference, the option is to unique_ptr, shared_ptr or other managed form of pointer.

A pointer is a indirect. Accessing data indirectly gives you a lot of flexibility because you happen to have an intermediary that can be variable. So we can have a variable of something variable.

All problems in computer Science can be Solved by Another level of indirection

-- David Wheeler

The pointer can be a abstraction for concrete value. Abstractions are very useful to facilitate understanding and even solve certain problems. It is common to have other levels of abstraction that hide the pointer, but it is still there.

A array or other forms of data collections (lists, stacks, trees, graphs, queues) are usually accessed through pointers, after all the array is a subset memory. If a pointer is a given variable and porting points to different data, it fits like a glove on array whose value is composed of a series of variables. Allows programming without array? It does, but it’s insane. Unless you’re talking about problems and very simple, self-contained exercises.

Has more details on When to choose whether to use a pointer when creating an object?.

Has several questions on the subject here, many of them are very practical. Most are C, but if it is raw pointer it is equal.

As you progress you will know where it can be used naturally. Ask more specific questions here with more specific problems.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.