sizeof and strlen()

  • Thread starter Bill Cunningham
  • Start date
B

Ben Bacarisse

I was trying to illuminate the difference between a function and a
keyword since the OP seems to be confused about what to use and when.
At his current state of knowledge I don't think he necessarily
benefits from these examples.

As the examples show, the function/keyword distinction has little to do
with when they are evaluated. The simplest way to show the distinction
would be to try to take their addresses: you can do this with a function
but not with an operator like sizeof. This is in C of course; some
languages recognise the operator/function relationship.
 
L

Lew Pitcher

Strlen does it's work at run-time.
Sizeof does it's work at compile time.

Not true. Consider:
[snip]
Also consider
[snip]
Ok, OK, I give. :)

I was trying to illuminate the difference between a function and a
keyword since the OP seems to be confused about what to use and when.
At his current state of knowledge I don't think he necessarily
benefits from these examples.

And /I/ was trying to give Bill the simplest explanation I could, without
edge and corner cases to worry about.

Yes, /I/ know about VLAs and compiler-based code optimization techniques,
and they don't confuse me. But, then again, I've worked with all this for
decades.

But, Bill often needs simpler explanations (which I don't always manage to
give him) that disregard corner cases and esoteric features. For someone
who can't easily decide which of strlen() or sizeof to use, a simple rubric
(like the one I tried to provide) is often a good way to proceed.
 
N

Nick Keighley

On May 7, 2010 14:22, in comp.lang.c, (e-mail address removed) wrote:

strlen() always returns a size_t value, whether argument points at an
initialized area or not.

a call to strlen() might not return anything at all- that is it may
not even return- if you cause it to invoke undefined bahaviour. I
don't really understand Bill's question. Bill, you understand that
size_t is a type?

If the pointer you give as the argument to strlen() points to character
array that is *not* initialized with a string, the results of strlen() are
unpredictable and likely incorrect. This is because there is no guarantee
that the character array pointed to contains the sentinal '\0' (0)
character in /any/ position.

the program may crash or do some other weird behaviour

If the pointer you give as the argument to strlen() points to an
uninitialized array, the results of strlen() are unpredictable and likely
incorrect. This is because there is no guarantee that the character array
pointed to contains the sentinal '\0' (0) character in /any/ position.

again, it may even crash (or worse!)

<snip>
 
N

Nick Keighley

    That would be easy. They both return doubles. So if you came across a
function like this,

double math(double operator);

Then sqrt() or sin() would work. Now if you came across this,

double math(int operator);

I don't think you can do this,

math(sqrt(5));

you can actually.
because math takes a double as parameter.

um, no it doesn't
It wants something that is going
to return a double not just an int. The int isn't big enough.

you didn't say what you meant to say (and you'd have been wrong
anyway)
 
N

Nick

Tom St Denis said:
Strlen does it's work at run-time.
Sizeof does it's work at compile time.

Not true. Consider:

#include <stdio.h>

int blah(int a) { int buf[a]; return sizeof buf; }
int main(void) { printf("%d\n", blah(10)); return 0; }

It's valid C99 code and should print out 40 on [most] 32-bit hosts.

Although if the compiler replaces "sizeof buf" with "a", most of the
work has been done at compile time.
 
B

Bill Cunningham

[snip]
I was trying to illuminate the difference between a function and a
keyword since the OP seems to be confused about what to use and when.
At his current state of knowledge I don't think he necessarily
benefits from these examples.

It's sometimes hard for me to grasp. Amen to that. Some of these
examples confuse me. I was coping code that I really didn't fully
understand.

Bill
 
K

Keith Thompson

Geoff said:
Many of the str___() family of functions are problematic when dealing
with non-string or uninitialized data so don't be surprised when you
make a mistake with them.

And "problematic", in this case, means that the str*() functions
will usually misbehave in arbitrarily bad ways when confronted
with non-string or uninitialized data. The only solution is this:
*never* pass (a pointer to) non-string or uninitialized data to a
str*() function.

Some of them can deal with non-string data in some cases,
particularly functions that can be told to look only at a subset
of a buffer, but that's a more advanced topic. Think of the above
as a decent rule of thumb with a few exceptions.
 
T

Tom St Denis

But, Bill often needs simpler explanations (which I don't always manage to
give him) that disregard corner cases and esoteric features. For someone
who can't easily decide which of strlen() or sizeof to use, a simple rubric
(like the one I tried to provide) is often a good way to proceed.

Bill needs to stop trolling usenet is what Bill needs. Either that or
recognize that he's far far far too stupid to learn how to write C
programs.

But I suspect based on his repeated questions with the same fallacies
over and over that he's really just a troll.

Tom
 
B

Bill Cunningham

Geoff said:
Understandable. And considering that send() and recv() take similar
arguments in similar orders it's tempting to regard them as
exchangeable which is what appears to have started some of the
confusion.

Notice that in Beej's examples send() is dealing with a buffer that
contains known string data so strlen() is appropriate in that case but
recv() must be prepared for unknown data so the length argument must
be the size of the buffer.

You should also be aware that his client rather ungracefully exits if
the received data is longer than the buffer so you should not regard
this as an example of good error handling. :) His client and server
examples are meant to work with each other but aren't designed to be
robust.

Many of the str___() family of functions are problematic when dealing
with non-string or uninitialized data so don't be surprised when you
make a mistake with them.

One of the reasons I take on this tough code is that it teaches me by
doing. I have learned a little more about pointers in working with this
code. I confused strlen and recv and send. But I noticed that size_t was a
parameter to recv. So I thought I should use strlen when strlen was being
used with send.

Bill
 
D

David Thompson

"Bill Cunningham" <[email protected]> writes:
<snipped>

I concede the difficulty of writing responses to Bill -- (you might
notice) I don't even try -- but nevertheless:
In any case, both int and size_t are integer types. If you pass
a value of one type to a function expecting an argument of the
other type, it will be implicitly converted. It's likely to be

If a (correct) prototype declaration is visible, aka in scope.
Otherwise arg passing works in a way that is less intuitive to many
people (likely including Bill) and not always safe. So Don't Do That.
 

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

Forum statistics

Threads
474,093
Messages
2,570,610
Members
47,230
Latest member
RenaldoDut

Latest Threads

Top