Does strlen() not return it's size_t if the data is uninitialized ?
strlen() always returns a size_t value, whether argument points at an
initialized area or not.
strlen() accepts a pointer to a character array which contains a string in C
format (terminated with a '\0' (0) character), and returns the length of
that string. strlen() does *not* return the length of an arbitrary
character buffer.
If the pointer you give as the argument to strlen() points to character
array, initialized with a string, strlen() will return a count of the
number of significant characters in the string. Technically, it returns a
count of characters between the start of the string (the pointer's value)
and the first '\0' (0) character.
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.
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.
And
sizeof does it work with initialized and uninitialized data?
sizeof evaluates to the size (in characters) of it's argument. It works
whether the argument is initialized or not.
strlen() and sizeof are two different facilities, with two different
purposes. Please don't confuse them.
For example,
ssize_t recv(int s, void *buf, size_t len, int flags);
char buf[1024];
recv(sock,buf,strlen(buf),0);
^
notice this parameter of the function returns size_t.
So?
Would it help if I
used char buf[1024]={0}; ?
No, it wouldn't.
Bill, you still have a fundamental misunderstanding about strlen() and
sizeof, and about function arguments for specific functions.
As I pointed out above, strlen() and sizeof are two different facilities,
with two different purposes.
sizeof is evaluated once, when you compile your program.
sizeof evaluates to the size of it's argument, in characters
sizeof is handy when you want to substitute, at compile time, a value that
represents the fixed length of an object.
strlen() is evaluated, possibly many times, when you run your program.
strlen() counts the number of char elements between the start of an array
and the first '\0' (0) byte in the array. This count does not have to
relate /at all/ to the size of the array.
strlen() is handy when you want to know how big a C string is.
To illustrate the differences, I'll use a code fragment.....
First, let's allocate a character array object and initialize it
with a string
char buf[1024] = "My buffer";
Notice that the character array object (<buf>) is 1024 characters long,
and that it contains a string ("My buffer") which is 10 characters long,
including the terminating '\0'.
Let's also allocate a couple of size_t objects:
size_t bufSizeof,
bufStrlen;
Now, let's put some values into thes two size_t objects:
bufSizeof = sizeof buf;
bufStrlen = strlen(buf);
If we look at these two values, we see that
- bufSizeof is set to 1024, representing the total number of characters
allocated to buf, and
- bufStrlen is set to 9, representing the 9 significant characters of the
string "My buffer", stored in buf
Follow me so far?
So we have strlen() to find out how big a *string* is, and sizeof to find
out how big an *object* is.
Now, on to your recv() function call.....
Using your prototype for recv()
ssize_t recv(int s, void *buf, size_t len, int flags);
and the relevant documentation (recv() is outside of ISO C, and it's
documentation is found elsewhere), we find that the argument <len>
specifies the length (in characters) of the buffer <buf>. Further, we find
that recv() will, if successful, place no more than <len> characters of new
data into <buf>.
You have an object
char buf[1024];
and want recv() to put data into that object. You need to tell recv() /how
much/ data to put into <buf>. The *most* you can ask recv() to put into
<buf> is (sizeof buf). You can ask for less, of course, but strlen() is not
an appropriate function (in this example) to determine /how much/ data
recv() should put in <buf>, because your argument to strlen() *doesn't
contain a string*.
To make it simple, until you become more familiar with C, POSIX, sockets,
and all those other areas of programming that you are exploring, you ALWAYS
want
char buf[1024];
recv(sock,buf,sizeof buf,0);
HTH