The reason is, of course, you are dereferencing some uninitialized pointer. It is impossible to tell from your current code because for one thing it is dependent on user input. It seems to run just fine for me with t = 2
; Even though I am not sure if it is working as it is supposed to.
However why does it run on Visual Studio?
Because VC++
doesn't initialize pointers to NULL. It is working becasue what ever is pointed by the uninitialized pointer is somehow working as the initialized one would. So basically Undefined behavior.
Why does it fail with a segmentation fault when compiled GNU C++ compiler?
Because g++
does initialize pointers to NULL