Sizeof vs. Strlen

T

Thomas stegen

sugaray said:
hi, it just came up my mind that since we can get the length of any
given string literal S with 'sizeof S-1', so, what's the merit of
library function strlen()'s existence ? thanx in advance for your
instruction.

#include <stdio.h>
#include <string.h>

int main(void)
{
char s[20] = "hello";
char *ptr = "hello2";

printf("s: sizeof == %u, strlen == %u\n", sizeof s, strlen(s));
printf("ptr: sizeof == %u, strlen == %u\n", sizeof ptr, strlen(ptr));

return 0;
}
 
S

sugaray

hi, it just came up my mind that since we can get the length of any
given string literal S with 'sizeof S-1', so, what's the merit of
library function strlen()'s existence ? thanx in advance for your
instruction.
 
D

Dan Pop

In said:
hi, it just came up my mind that since we can get the length of any
given string literal S with 'sizeof S-1', so, what's the merit of
library function strlen()'s existence ? thanx in advance for your
instruction.

strlen is seldom used with a string literal as its argument...

Dan
 
M

Mark McIntyre

hi, it just came up my mind that since we can get the length of any
given string literal S with 'sizeof S-1',

you don't need the -1
so, what's the merit of
library function strlen()'s existence ? thanx in advance for your
instruction.

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

int main(void)
{
char* somestr = "hello world";
printf("length is %d or %d\n", sizeof somestr, strlen(somestr));
return EXIT_SUCCESS;
}
 
D

Dan Pop

In said:
^^^^^^^^^^^^^^
you don't need the -1

Of course you do, when using the sizeof operator in the OP's context,
and if you want to get the same result as from strlen(S). Then again,
Mark McIntyre is not supposed to understand this...

Dan
 
S

Stephen L.

sugaray said:
hi, it just came up my mind that since we can get the length of any
given string literal S with 'sizeof S-1', so, what's the merit of
library function strlen()'s existence ? thanx in advance for your
instruction.

sizeof () gives the total storage size of the string
(including the '\0' terminating character)
and is evaluated at compile time. It's argument
(and the resultant value) can never change at run-time.

strlen() gives the length, excluding the '\0'
terminating character, of the string, and is
evaluated at run-time. It's argument, a pointer
to char array, can change at run-time (as well
as the items in the array itself).

Stephen
 
G

Guest

sugaray said:
hi, it just came up my mind that since we can get the length of any
given string literal S with 'sizeof S-1', so, what's the merit of
library function strlen()'s existence ? thanx in advance for your
instruction.

Strlen is useful for non literal string.

#include <stdio.h>
#include <string.h>
#define STRING "Hello World!"
#define PRINT(S) printf("%s sizeof == %2u, strlen == %2u\n", \
S, sizeof(S), strlen(S));
int main(void)
{
char s[50] = STRING;
char *p = STRING;
PRINT(STRING);
PRINT(s);
PRINT(p);
return 0;
}

Compiled and executed on my machine will output:

Hello World! sizeof == 13, strlen == 12
Hello World! sizeof == 50, strlen == 12
Hello World! sizeof == 4, strlen == 12

- Dario
 
D

Default User

sugaray said:
hi, it just came up my mind that since we can get the length of any
given string literal S with 'sizeof S-1', so, what's the merit of
library function strlen()'s existence ? thanx in advance for your
instruction.

You answered your own question. It only works string literals or arrays
that are occupied by a string that exactly fills them (including
terminator). Arrays that contain strings shorter than their capacity,
dynamically allocated string buffers, or any string that has been
assigned to a pointer or passed in to a function won't work.




Brian Rodenborn
 
M

Martin Ambuhl

sugaray said:
hi, it just came up my mind that since we can get the length of any
given string literal S with 'sizeof S-1', so, what's the merit of
library function strlen()'s existence ? thanx in advance for your
instruction.

You are very confused. Consider the following program. Does 'sizeof'
help in any way in determining the length of the string?

#include <stdio.h>
#include <string.h>

int main(void)
{
char foo[BUFSIZ] = "crap";
char *bar = foo;
printf("When bar points to foo containing \"%s\",\n"
" sizeof foo = %lu, strlen(foo) = %lu\n"
" sizeof bar = %lu, strlen(bar) = %lu\n\n",
foo,
(unsigned long) sizeof foo, (unsigned long) strlen(foo),
(unsigned long) sizeof bar, (unsigned long) strlen(bar));
strcpy(foo, "bananas");
printf("When bar points to foo containing \"%s\",\n"
" sizeof foo = %lu, strlen(foo) = %lu\n"
" sizeof bar = %lu, strlen(bar) = %lu\n\n",
foo,
(unsigned long) sizeof foo, (unsigned long) strlen(foo),
(unsigned long) sizeof bar, (unsigned long) strlen(bar));
return 0;
}


When bar points to foo containing "crap",
sizeof foo = 16384, strlen(foo) = 4
sizeof bar = 4, strlen(bar) = 4

When bar points to foo containing "bananas",
sizeof foo = 16384, strlen(foo) = 7
sizeof bar = 4, strlen(bar) = 7
 
C

Christian Bau

Default User said:
You answered your own question. It only works string literals or arrays
that are occupied by a string that exactly fills them (including
terminator). Arrays that contain strings shorter than their capacity,
dynamically allocated string buffers, or any string that has been
assigned to a pointer or passed in to a function won't work.

On most implementations, sizeof ("Hello, world!") - 1 != 13. Usually the
result will be 3 or 7.
 
E

Eric Sosman

Christian said:
On most implementations, sizeof ("Hello, world!") - 1 != 13. Usually the
result will be 3 or 7.

You're saying "most implementations" are broken, then?
Every C implementation I have ever used would get this one
right -- but then, I've used only a small minority of all
C implementations.
 
M

Minti

hi, it just came up my mind that since we can get the length of any
given string literal S with 'sizeof S-1', so, what's the merit of
library function strlen()'s existence ? thanx in advance for your
instruction.

The difference is that sizeof is compile time operator, so you can get
length of only "string literlals" rather than "strings" in general.
While your strlen is a function that is called at runtime.

strlen is a general function capable of computing the length of any
string. It could be used against your "manufactured"/"malloced"
strings as effeciently as against string literals or char arrays,
while sizeof can only be used against your string literals.


I would go with sizeof() as it won't involve any runtime costs.
 
D

Default User

Christian said:
On most implementations, sizeof ("Hello, world!") - 1 != 13. Usually the
result will be 3 or 7.

I think you are wrong. Please present some evidence of this. You ARE
aware that sizeof is an operator, not a function?


Here's a test program:

#include <stdio.h>

int main(void)
{
printf ("%u\n", (unsigned int) (sizeof ("Hello, world!") - 1));
return 0;
}


Built and run with Visual Studio:

13
Press any key to continue


Built and run with gcc:

hercules:/home/m196957> gcc -o so so.c
hercules:/home/m196957> ./so
13


I can't imagine how anything else would be produced.



Brian Rodenborn
 
M

Martin Dickopp

Christian Bau said:
On most implementations, sizeof ("Hello, world!") - 1 != 13. Usually
the result will be 3 or 7.

Are you sure? A string literal has type array of char (6.4.5#5), and
when it is operand of the sizeof operator, it is /not/ converted to a
pointer type (6.3.2.1#3).

Martin
 
A

Arthur J. O'Dwyer

On most implementations, sizeof ("Hello, world!") - 1 != 13. Usually the
result will be 3 or 7.

I count three replies so far along the lines of "gee, I think you're
probably wrong." Just for the record: fourteen minus one is thirteen
on every C implementation in existence. It is *never* 3, and it is
*never* 7. Christian's statement was just plain wrong.

-Arthur
 
C

CBFalconer

Arthur J. O'Dwyer said:
I count three replies so far along the lines of "gee, I think
you're probably wrong." Just for the record: fourteen minus
one is thirteen on every C implementation in existence. It is
*never* 3, and it is *never* 7. Christian's statement was
just plain wrong.

I think he is suffering from a temporary delusion that the parens
cause the string to be represented by a pointer. We all have
these problems when a cow flies by.
 
V

Villy Kruse

sugaray said:
hi, it just came up my mind that since we can get the length of any
given string literal S with 'sizeof S-1', so, what's the merit of
library function strlen()'s existence ? thanx in advance for your
instruction.

#include <stdio.h>
#include <string.h>

int main(void)
{
char s[20] = "hello";
char *ptr = "hello2";

printf("s: sizeof == %u, strlen == %u\n", sizeof s, strlen(s));
printf("ptr: sizeof == %u, strlen == %u\n", sizeof ptr, strlen(ptr));

return 0;
}


Another example

#include <stdio.h>
#include <string.h>

void sub(char s[20])
{
printf("s: sizeof == %u, strlen == %u\n", sizeof s, strlen(s));
}


int main(void)
{
char s[20] = "hello, world";

sub(s);

return 0;
}



Villy
 
D

Default User

Arthur J. O'Dwyer said:
I count three replies so far along the lines of "gee, I think you're
probably wrong."



I generally try to avoid the Dan Pop "engage your brain" rhetoric. After
all, I'm not perfect and I appreciate when people are relatively gentle
when my imperfections surface.




Brian Rodenborn
 
J

Jack Klein

sugaray said:
hi, it just came up my mind that since we can get the length of any
given string literal S with 'sizeof S-1', so, what's the merit of
library function strlen()'s existence ? thanx in advance for your
instruction.

#include <stdio.h>
#include <string.h>

int main(void)
{
char s[20] = "hello";
char *ptr = "hello2";

printf("s: sizeof == %u, strlen == %u\n", sizeof s, strlen(s));
printf("ptr: sizeof == %u, strlen == %u\n", sizeof ptr, strlen(ptr));

Passing an uncast size_t to printf() to match a conversion specifier
of "%u" is undefined behavior, of course, and just plain doesn't work
on some implementations.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top