Tuesday, September 23, 2008

Usage of C++ NULL vs 0

There's a very common confusion between C++ developer as which one we should use for pointer assignment, NULL or 0???

 

NULL is a macro and defined to 0. But for readibility purpose one should use NULL for poiner and 0 for integer. 

Also since system provide NULL to be defined to some value(today it is 0) and if tomorrow the defined value changes, then still the code will be portable if NULL is used. So I think instead of using hard coaded value 0, we 
should use NULL for pointer. It increases both for readbility and portability.

 

But many C++ developers prefer 0 because it avoids the need to #include whichever header NULL was defined in. 

  The only situation where using 0 as null pointer might cause ambiguity problems is in overloading situations like this: 

void foo(int i); 
void foo(SomeType*); 

int main() 

    foo(0); // ambiguous 

  In practice the int version of the function will be called (although I'm not 100% sure what the standard says about this), which might not be what the programmer wanted. 

  A properly-implemented NULL would avoid the ambiguity. Rather oddly, though, the NULL macro in many compilers still cause ambiguity, and a "foo(NULL);" will call the int version regardless (gcc at least issues a 
warning, though). 

Moreover, there may be some confusion about what the latest standard says, but it at least used to be true that in C NULL was ((void *)0)  At least on some systems.  This is illegal in C++. 

 

 

Why does a host need a gateway address?

The actual communication between networked devices takes place on layer 2. However, Layer 3 is required to route this traffic between different networks. The LAN network and the Internet are diffferent IP networks. This is layer 3. Thet IP happens in Layer 3. IP addressing is required when someone need to move between two separate IP networks. The gateway address is an interface on the router that "knows" about both networks and must be on the same network as the host making the request.


 If the router doesn't know about the Internet address a user give it then it will send it to it's gateway which is another router. Then it will send it to it's gateway and so forth until the destination network is reached.

 Each step along the way requires a new set of ARPing. The ARP tables on most Cisco routers are retained for 4 hours by default so the ARP requests don't happen all the time, the MAC address are remembered. Also, the ARP is a broadcast protocol. Broadcasts don't cross routers by default. Routers can't ARP for Internet addresses because the Internet is a routed network. LANs are typically switched networks and broadcasts do traverse network switches. The Internet routers have tables of routes so they use the logic in their programming to decide which interface would be the best route to send traffic. Once the route is established between you and the destinations, the routers along the way remember the route and keep it in their routing tables. These are dynamic routes and they are retained for a preset time to live period. Of course some of the top level routers have all static routes but they are known by routers on each end.