Uninitialized pointers in code

后端 未结 8 2296
旧巷少年郎
旧巷少年郎 2020-11-30 03:15

I am learning C++ and I came to know that pointers if left uninitialized could point to random locations in memory and create problems that memory might be used by some othe

8条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-30 03:48

    int a,*ptr;
    

    now

    print(ptr,*ptr)
    

    In the above code two cases can be possible:

    1. It'll execute if default value in ptr is not the address of some used memory of program.

      Output:

             ptr             *ptr
      eg.  0x400730       -1992206795
      
    2. It'll give error (segmental fault) if default address in the ptr is the address of some used memory of program. E.g. if the address of variable a in the memory is also 0x400730.

提交回复
热议问题