In your sample code you made the same mistake that I pointed our earlier: You are writing a zero outside of the memory block you allocated with malloc. (By the way you should really get a book on C a read it because this is really basic stuff).
Assume the following code
#define BLOCKSIZE 1
send = malloc ( BLOCKSIZE * sizeof ( char ) );
This will allocated 1 byte of memory, agreed? Now we can write to that memory by doing
send[0] = ‘x’.
But what happends if I do
send[1] = ‘x’
Basicaly you go undefined behavior, it may crash, it may corrompt other variable, etc. Basicaly send[1] is the same as send[BLOCKSIZE]. You should be using send[BLOCKSIZE-1] = 0;
Back to your question:
send is a pointer to char, send is NOT an array it’s a pointer, hence when you asked for sizeof ( send ) you get the size of the pointer, NOT to size of what it’s pointing to. On x86/32 all pointers are 32 bits (which is why you get a sizeof 4 ). sizeof is an operating that is done at compile time, not at run time. The compiler isn’t smart enough to figure out that you did a malloc and get the size of what send is pointing to.
That is why sizeof receive yield the result you expect because receive is not a pointer but rather an array.
sizeof gives the size of an object, strlen calculate the size of a string but scanning the string an looking for a terminator (the 0 character). In fact if the memory region does NOT contain a 0, strlen will keep on going until it finds one, which means it may read past the buffer…
In the case of receive strlen and sizeof gives the same result for two reasons, one because receive is an array and sizeof len return the proper size. Second because you code is buggy, strlen returns the string lenght NOT including the 0. So even if strlen report string is 100 bytes, it’s actually using 101 because of the 0 at the end of the string. With receive you wrote the 0 OUTSIDE the variable, normaly strlen should return BLOCKSIZE-1.
The fact that array are indexed starting at 0 can create confusion.