[Solved] Learning C++ pointer runs into core dump with following code, I really don’t why?


Change the following statements

int ** dp = new int *;

*dp = array;

Or even you could write one statement instead of the two above

int ** dp = new int *( array );

Also do not forget to free the allocated memory

delete dp;
delete []array;

As for your code then you are trying to dereference a null pointer.

Take into account that C header <stdlib.h> in C++ is named like <cstdlib>. And this header is not used in your program. So you may remove it.

The program could look like

#include <iostream>

int main() 
{
    int *array = new int[2] { 1, 2 };
    int ** dp = new int *( array );

    std::cout << ( *dp )[0] << '\t' << ( *dp )[1] << std::endl;

    delete dp;
    delete []array;

    return 0;
}

The output is

1   2

0

solved Learning C++ pointer runs into core dump with following code, I really don’t why?