Jump to

v01-l4-findOutput

(random out of 3)

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 initialize ptr_j with nullptr, but rather use a random memory address not linked to any concrete value. In that case, printing j in the console will show a random value, that may change for different executions of the program.