What would produce the code below ?
int* ptr_j {};
int j = *ptr_j;
cout << j << endl;
The line
int* ptr_j {};
declares ptr_j using zero-initialization,
which means that the program will initialize ptr_j
using the default value for all pointers, that is nullptr.
The segmentation fault occurs then when trying to dereference it using *ptr_j,
which cannot be done when ptr_j == nullptr.
🔔 Note that using
int* ptr_j;instead (default-initialization) will not initializeptr_jwithnullptr, but rather use a random memory address not linked to any concrete value. In that case, printingjin the console will show a random value, that may change for different executions of the program.