S
S
sabn1k2016-02-24 20:21:52
Programming
sabn1k, 2016-02-24 20:21:52

Pointer to pointer?

How often and where are pointers to pointers used? And is it necessary at all?

Answer the question

In order to leave comments, you need to log in

6 answer(s)
M
MiiNiPaa, 2016-02-24
@MiiNiPaa

In modern C++, pointers are generally avoided. But here are some examples:
Pseudo2D array - array of arrays
An array of pointers. For example, a vector stores a pointer to a buffer inside. .data() returns this pointer. If pointers are stored there, the result is a pointer to a pointer.
When a pointer needs to be changed and API C is used: A pointer to an object is used when the object needs to be changed so that it is visible outside of the modifying function. In this case, the object is another pointer.

X
xozzslip, 2016-02-25
@xozzslip

Pointers are a very low-level thing, but modern programming cannot be imagined without them.
Think there is a variable? After all, memory is, in fact, just a tape on which you can write 0 and 1. So a variable is just a small section of this memory. If we allocate 4 bytes for a variable, then it will occupy 32 cells, each of which will contain 0 or 1. Well, we wrote it down, but how can we count it now? This is where pointers are needed! The pointer points to the first bit of the section in which the variable is stored. Further, acting in this way, you can get arrays, and then all modern programming.
Understanding this abstraction is essential. Learn to write in C. I advise D. Ritchie "C programming language". The language is very simple, the content of the book is only 100 pages. But it will take a couple of weeks.

A
abcd0x00, 2016-02-25
@abcd0x00

Here in this line argv is a pointer to a pointer to a string.
The program arguments are represented as an array of pointers to strings, in which the last pointer is zero (null pointer).
It happens that this line is written down and so
there is no error. They just write brackets to remind you that there is an array of pointers, and not just some kind of one pointer.
What `s next? That's right, you can do argv++ by traversing the array of pointers to the right.
But how to make a function that would rearrange argv itself?

void func(char ***p) { (*p)++; }
...
    func(&argv);

Example
#include <iostream>

using namespace std;

void func(char ***p)
{
    (*p)++;
}

int main(int argc, char *argv[])
{
    cout << argv << " " << *argv << endl;
    func(&argv);
    cout << argv << " " << ((*argv) ? *argv : "no") << endl;
    return 0;
}

Вывод
[[email protected] cpp]$ .iso++ t.cpp -o t
[[email protected] cpp]$ ./t
0xbffc3114 ./t
0xbffc3118 no
[[email protected] cpp]$ ./t a
0xbf999594 ./t
0xbf999598 a
[[email protected] cpp]$

A
Alexander Titov, 2016-02-25
@alex-t

A pointer to a pointer is often found in "old" C++ interfaces and in "just C" when it is necessary to allocate memory in a function and put something there. Something like

int foo(int **bar)
{
  *bar = new int;
  **bar = 123;
  return 0; // код ошибки
}

In modern C++, it is better to write a function of the form
a and implement possible errors on exceptions, so the memory will not leak, and the error code does not need to be checked with each call. In addition, the return of the autopointer will not cause the "calling" code to ask questions whether it should then release the memory received by the pointer or not.

A
asd111, 2016-02-24
@asd111

Usually do nested structures or nested classes.
Those. we make a structure in which the pointer lies, then we make a pointer to this structure, etc.
A pointer to a pointer is rarely used.
Now, instead of ordinary pointers in C ++, it is customary to use smart pointers.
std::unique_ptr
std::shared_ptr
std::weak_ptr

M
maxwolf, 2016-02-26
@maxwolf

From a theoretical point of view, "pointer to pointer" is simply a reflection of the data representation hierarchy. Those. as the complexity of mutual relations in the data being processed increases, a pointer to a pointer to a pointer, etc. may also be required. The simplest example is a path in a graph. A list (itself built on pointers) of pointers to graph vertices (which are also pointers to other vertices). From a practical point of view, you need to clearly understand all the advantages and disadvantages of each way of working with pointers (for example, shared_ptr is not some kind of abstract "magic", but a special object that adds convenience, but charges for it in the form of an additional memory and additional actions hidden in primitive operations).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question