   Pointer default value in C++
  Can anybody explain to me why this programtypedef int* ptr;struct A{    A() {}    ptr p;};struct B{    B() : p() {}    ptr p;};void main(){    A a;    B b;    printf( "a=%d, b=%d\n", a, b );}  outputs thata=1, b=0under VC++2003 ?Thanks[edited by - cnstrnd on February 14, 2004 10:23:08 PM]
  Chance. Uninitialized variables have random values.Edit: A has a trivial constructor, therefore it's still a POD-type. POD-types are uninitialized, so p, member of A is uninitialized. B explicitely sets it to zero.“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.”    — Brian W. Kernighan (C programming language co-inventor) [edited by - Fruny on February 14, 2004 10:30:19 PM];  Ok, I was suspecting something like that but i didn''t manage to get informations.Thanks. ;  A() {}<--what does this lines do specially the {} part?B() : p() {}<--and this? ;quote:Original post by Makoy  A() {}<--what does this lines do specially the {} part?B() : p() {}<--and this?   They are the definitions for the class constructors.For A, you have a constructor that does nothing.For B, you have a constructor that default-constructs (zeroes) p then does nothing.   “Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.”    — Brian W. Kernighan (C programming language co-inventor);  To be safer you should initialise pointers at the point of creation with 0 or a valid address so you can check it for NULL later:typedef int* ptr;struct A{   A() : p(0) {}   ptr p;};