mardi 19 juillet 2016

Why does dereferencing an uninitialised pointer not crash here?

I try to run this code, it is built with only a warning ('a' maybe uninitialized in this function ....) but during run time my computer crash and only show "Hello world!".

#include <iostream>
using namespace std;
int main()
{
 cout << "Hello world!" << endl;
 int p=20;
 int *a;
 *a=p;
 cout << *a << endl;
 cout << &p << endl;
 return 0;
}

However when I omit this part:

cout << &p << endl;

Again I will have the same warning but the code work properly. Anyone can explain what's going on?

Thank's before.

Aucun commentaire:

Enregistrer un commentaire