sizeof(string) versus strlen()

Hi Community

Why sizeof(send) is 4Byte and strlen(send) are 8Byte ???
I think the only difference should be ‘\0’


#include <stdlib.h>
#define BLOCKSIZE 8

int main( void )
{
int i=0;
char* send=NULL;

send = malloc(BLOCKSIZE * sizeof(char)); 

for ( i = 0; i < BLOCKSIZE; i++ )
{
*(send+i)=‘x’;
}

send[BLOCKSIZE] = ‘\0’;
receive[BLOCKSIZE] = ‘\0’;

printf(“String send is: %s Byte \n”,send);
printf(“Size of send is: %d Byte \n”,sizeof(send));
printf(“Size of send is: %d Byte \n”,strlen(send));

return (EXIT_SUCCESS);
}


result:

String send is: xxxxxxxx Byte
Size of send is: 4 Byte
Size of send is: 8 Byte

Thank you !!!

sizeof(send) is a char * which is 4 bytes.

Thank you cburgess

can you be a little bit more specific?

I dont understand, if I use “unsigned char receive[BLOCKSIZE];” insted of
char* send=NULL; send = malloc(BLOCKSIZE * sizeof(char)); all is perfect and sizeof(send) is
the same like strlen(send);

why ???

Thank you

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.

Hi Mario!!

I can see what you mean, thank you for your accurate illustration :smiley: