#include <stdlib.h>
int main(){
int *array = new int[2];
array[0] = 1;
array[1] = 2;
int ** dp = NULL;
*dp = array;
return 0;
}
when I run it ,Segmentation fault (core dumped). g++ version is
Reading specs from /usr/lib/gcc/x86_64-redhat-linux/3.4.5/specs Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --enable-shared --enable-threads=posix --disable-checking --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-java-awt=gtk --host=x86_64-redhat-linux Thread model: posix gcc version 3.4.5 20051201 (Red Hat 3.4.5-2)
Change the following statements
Or even you could write one statement instead of the two above
Also do not forget to free the allocated memory
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
The output is