Monday, February 28, 2011

what these functions will do in socket programming [ htons(), htonl(), ntohs() and ntohl() ]

By Order of the Realm! There shall be two byte orderings, hereafter to be known as Lame and Magnificent!

I joke, but one really is better than the other. :-)

There really is no easy way to say this, so I'll just blurt it out: your computer might have been storing bytes in reverse order behind your back. I know! No one wanted to have to tell you.

The thing is, everyone in the Internet world has generally agreed that if you want to represent the two-byte hex number, say b34f, you'll store it in two sequential bytes b3 followed by 4f. Makes sense, and, as Wilford Brimley would tell you, it's the Right Thing To Do. This number, stored with the big end first, is called Big-Endian.

Unfortunately, a few computers scattered here and there throughout the world, namely anything with an Intel or Intel-compatible processor, store the bytes reversed, so b34f would be stored in memory as the sequential bytes 4f followed by b3. This storage method is called Little-Endian.

But wait, I'm not done with terminology yet! The more-sane Big-Endian is also called Network Byte Order because that's the order us network types like.

Your computer stores numbers in Host Byte Order. If it's an Intel 80x86, Host Byte Order is Little-Endian. If it's a Motorola 68k, Host Byte Order is Big-Endian. If it's a PowerPC, Host Byte Order is... well, it depends!

A lot of times when you're building packets or filling out data structures you'll need to make sure your two- and four-byte numbers are in Network Byte Order. But how can you do this if you don't know the native Host Byte Order?

Good news! You just get to assume the Host Byte Order isn't right, and you always run the value through a function to set it to Network Byte Order. The function will do the magic conversion if it has to, and this way your code is portable to machines of differing endianness.

All righty. There are two types of numbers that you can convert: short (two bytes) and long (four bytes). These functions work for the unsigned variations as well. Say you want to convert a short from Host Byte Order to Network Byte Order. Start with "h" for "host", follow it with "to", then "n" for "network", and "s" for "short": h-to-n-s, or htons() (read: "Host to Network Short").

It's almost too easy...

You can use every combination of "n", "h", "s", and "l" you want, not counting the really stupid ones. For example, there is NOT a stolh() ("Short to Long Host") function—not at this party, anyway. But there are:

htons()

host to network short

htonl()

host to network long

ntohs()

network to host short

ntohl()

network to host long

Basically, you'll want to convert the numbers to Network Byte Order before they go out on the wire, and convert them to Host Byte Order as they come in off the wire.

How to detect memory leaks in C/C++ program ?

Answer:
Let us first see, How malloc and free works.

int * p = (int*)malloc(sizeof(int));
//malloc allocates memory and returns address to p. Let us say memory address 1000 is returned by malloc.

while freeing the memory, we will make use of the same address.

free(p); //free the memory address pointed by p [ in our case the memory address is 1000]

To detect memory leaks in a multiple file:


The concept is we have to use a Linked List to store the every malloc information.

Let us say malloc() is called in 100th line in 1.C.

At the time of malloc, We need to store the following informations in a linked list.
1.address returned by the malloc() fn
2.CPP filename [To get the c/cpp filename, we can use _FILE_ macro which will gives the cpp filename]
3.Line number at which malloc is called [_LINE_ macro gives us the line number]

Whenever we are freeing the memory, we need to check the linked list whether memory to be freed is available in where we stored the malloc() information.
if it is available in a linked list, then we need to delete that particular node and then free the memory.

Ex:
1.cpp

Line No:50: int * p = malloc(sizeof(int));
Line No:70: int* q = malloc(sizeof(int));
End of the program:
free(q);
//we forget to free the p;

For the above program, we will create the linked list with two nodes to represent two malloc info in 1.cpp.
while freeing the memory, the address to be freed will be checked with the linked list. if any node in a linked list matches, then we will remove that entry.
At the end of the program,Linked list will contains information about whichever pointer is not freed from memory.

Reference:http://www.codeproject.com/KB/cpp/MemoryLeakDetectionIn_CPP.aspx

Wednesday, February 23, 2011

awesome usage of function pointers in android C/C++

I have observed some awesome usage of function pointers in android C/C++. My sample application similar to that code:

#include <conio.h>

void fn( void (*seekDoneCb)() )
{
    void (*mSeekDoneCb)() = seekDoneCb;   

    (*mSeekDoneCb)();    
}

void Seek()
{
    printf("Seek()fn Called");
}

int main(int argc, _TCHAR* argv[])
{
    fn(Seek);
    getch();
    return 0;
}

Output:Seek()fn Called

Tuesday, February 22, 2011

How Negative values are stored in system ????

How Negative values are stored in system ????

int a = -1;
printf("Value of a:%x",a );

what will be the result ??? Can you tell me the value of it ???

Is it possible to predict this value ??


Result is:

 Value of a:0xffff ffff

Yes it is possible to predict the value of it.

Reasoning:
 
   Every bit is on or off / binary coded system. In binary coded system,
the negative values can be represented in two ways:

   1.One's complement [convert the zeroes to ones and ones to zeroes]
   2.Two's complement [ Two complement = one's complement value + 1]   


                      value of  1: 0000 0001
 One's complement to represent -1: 1111 1110 [changing the zeroes and to 1s and ones to zeroes in 0000 0001]
 Two's complement value          : 1111 1111 [1111 1110 +1]   

 To represent -1, the values will be stored as 1111 1111  in memory.



In my system, size(int) is 4 bytes.
   
To represent value 1 in Hex: 0x00000001 [ single value represents 4 bits].
         Value 1 in binary  : 00000000 00000000 00000000 00000001
One's complement for -1 in binary: 11111111 11111111 11111111 11111110
       
Two's complement for -1 in binary: 11111111 11111111 11111111 11111111
Two's complement for -1 in Hex   : 0x ffff ffff [single digit represents 4 binary digits]

In the same way, we can try for different negative values too.


int a = -1;
printf("Value of a:%x",a );

what will be the result ??? Can you tell me the value of it ???

Is it possible to predict this value ??


Result is:

 Value of a:0xffff ffff

Yes it is possible to predict the value of it.

Reasoning:
 
   Every bit is on or off / binary coded system. In binary coded system,
the negative values can be represented in two ways:

   1.One's complement [convert the zeroes to ones and ones to zeroes]
   2.Two's complement [ Two complement = one's complement value + 1]   


                      value of  1: 0000 0001
 One's complement to represent -1: 1111 1110 [changing the zeroes and to 1s and ones to zeroes in 0000 0001]
 Two's complement value          : 1111 1111 [1111 1110 +1]   

 To represent -1, the values will be stored as 1111 1111  in memory.



In my system, size(int) is 4 bytes.
   
To represent value 1 in Hex: 0x00000001 [ single value represents 4 bits].
         Value 1 in binary  : 00000000 00000000 00000000 00000001
One's complement for -1 in binary: 11111111 11111111 11111111 11111110
       
Two's complement for -1 in binary: 11111111 11111111 11111111 11111111
Two's complement for -1 in Hex   : 0x ffff ffff [single digit represents 4 binary digits]

In the same way, we can try for different negative values too.