The default value for int * NULL?

I wrote the following source

#include <iostream> using namespace std; template <class T> class AAA { public: void f() { cout << T() << " "; } }; int main ( void ) { AAA<int*> a; AAA<int> b; af(); /// in this case, T() == NULL?? bf(); return 0; } 

and console printing - 00000000 0. (in visual studio 2010)

if T is int *, T () == NULL? and is it always so?

+6
source share
3 answers

This is called value initialization , you are guaranteed 0 .

In addition, you do not need such a complex example to demonstrate:

 typedef int* T; int main() { T x = T(); std::cout << x; } 
+9
source

Yes. A pointer initialized to a value is always zero.

+8
source

Yes. This is a null pointer. And why it prints:

 00000000 

because it uses 4 bytes to represent a null address (in hexadecimal format).

On a 64-bit machine, it can print this instead (in hexadecimal format):

 0000000000000000 
0
source

Source: https://habr.com/ru/post/922125/


All Articles