V
V
Vladislav2015-11-29 19:30:23
C++ / C#
Vladislav, 2015-11-29 19:30:23

How to clear a pointer - C++?

Pointer
double **p = 0;
Complete the following tasks (decisions can be made inside the main function).
Create the structure shown in the figure.
dd0c0b5cff3848ad883168f4d1682b9d.jpg
Print the number in the square on the screen. After that delete all dynamic objects.

double **p = new double*; // динамическое выделение памяти под указатель  double
    double value = 2;
    double *index = new double; // динамическое выделение памяти под объект типа double
    index = &value; // Передаем указателю адрес переменной value; 
    p = &index; // передаем указателю адрес указателя index;
    cout << "Адрес value:                       " << &value << "\n";
    cout << "В index хранится адрес value:      " << index << "\n";
    cout << "Адрес index:                       " << &index << "\n";
    cout << "В p хранится адрес index:          " << p << "\n";
    cout << "В *p хранится значение index:      " << *p << "\n";
    cout << "В **p хранится значение value:     " << **p << "\n";
    return 0;

Answer the question

In order to leave comments, you need to log in

3 answer(s)
O
Oleg Tsilyurik, 2015-11-30
@OnlyBooid

int main( void ) {
   double **p = 0;
   *( *( p = new double* ) = new double ) = 2;
   cout << **p << endl;
   delete *p;
   delete p;
}

T
Therapyx, 2015-11-29
@Therapyx

stackoverflow.com/questions/13223399/deleting-a-po... is this?
Described the solution:
myPointer = new int;
delete myPointer;
myPointer = NULL;

V
Vladislav, 2015-11-29
@OnlyBooid

Therapyx Thank you very much! In the course of my experiments, I came to this solution.

void oneCell {
    double **p = new double*; // динамическое выделение памяти под указатель  double
    double value = 2;
    double *index = new double; // динамическое выделение памяти под объект типа double
    index = &value; // Передаем указателю адрес переменной value; 
    p = &index; // передаем указателю адрес указателя index;
    cout << "Адрес value:                       " << &value << "\n";
    cout << "В index хранится адрес value:      " << index << "\n";
    cout << "Адрес index:                       " << &index << "\n";
    cout << "В p хранится адрес index:          " << p << "\n";
    cout << "В *p хранится значение index:      " << *p << "\n";
    cout << "В **p хранится значение value:     " << **p << "\n";
    p = new double*;
    index = new double;
    delete p;
    delete index; 
    return 0;
}

It remains a little to understand why this is so.
Ps I will learn English, I searched for answers on stackoverflow using "index" as a pointer( Pointer - pointer.
when using:
delete p;
delete index;
or
delete *p;
delete index;
076bc1089e904402a55d4650642a884b.jpg

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question