Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > sizeof and strlen()

Reply
Thread Tools

sizeof and strlen()

 
 
Keith Thompson
Guest
Posts: n/a
 
      05-07-2010
"Bill Cunningham" <(E-Mail Removed)> writes:
> Keith Thompson wrote:
>> A string is a '\0'-terminated sequence of characters. Are you
>> working with strings here? No. Then why would you even consider
>> using strlen()?

>
> Then I've been reading some really bad code lately. This was not my idea
> to use strlen but the code I was reading used it. Should one use numbers
> such as simply 1024 as int not char* and I put +1 to my code too for the \0.
> I don't know why the author used strlen unless it was because of the size_t.


No, you've just been failing to understand the code you're reading.

The author used strlen because he wanted to compute the length *of a
string*. He didn't use it "because of the size_t"; he used it because
it was the correct tool for the job.

Choose which function or operator to use on the basis of what it does
and whether that meets your requirement, not just on the basis of what
type it returns.

Don't guess.

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
 
 
 
Bill Cunningham
Guest
Posts: n/a
 
      05-07-2010
Lew Pitcher wrote:

> Are you certain that you haven't confused the examples of the send()
> call with the examples of the recv() call?


Right where you cut and pasted I noticed something I don't quite
understand.

Beej says:
int send(int sockfd, const void *msg, int len, int flags);
sockfd is the socket descriptor you want to send data to (whether it's the
one returned by socket() or the one you got with accept().) msg is a pointer
to the data you want to send, and len is the length of that data in bytes.
Just set flags to 0. (See the send() man page for more information
concerning flags.)

Some sample code might be:

char *msg = "Beej was here!";
int len, bytes_sent;
..
..
..
len = strlen(msg);
bytes_sent = send(sockfd, msg, len, 0);
..
..
end quote.len has been declared an int and Beej has strlen passing value to
it.But strlen returns a size_t. That's the thing I don't understand about
send().Bill


 
Reply With Quote
 
 
 
 
Bill Cunningham
Guest
Posts: n/a
 
      05-07-2010
Keith Thompson wrote:

> No, you've just been failing to understand the code you're reading.
>
> The author used strlen because he wanted to compute the length *of a
> string*. He didn't use it "because of the size_t"; he used it because
> it was the correct tool for the job.


I see.

> Choose which function or operator to use on the basis of what it does
> and whether that meets your requirement, not just on the basis of what
> type it returns.
>
> Don't guess.


This turns upside down some of my beliefs about functions and return
types. BTW I am still using quote-fix and I'm hoping the quoting is better.

Bill


 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      05-07-2010
"Bill Cunningham" <(E-Mail Removed)> writes:
> Keith Thompson wrote:
>> No, you've just been failing to understand the code you're reading.
>>
>> The author used strlen because he wanted to compute the length *of a
>> string*. He didn't use it "because of the size_t"; he used it because
>> it was the correct tool for the job.

>
> I see.
>
>> Choose which function or operator to use on the basis of what it does
>> and whether that meets your requirement, not just on the basis of what
>> type it returns.
>>
>> Don't guess.

>
> This turns upside down some of my beliefs about functions and return
> types. BTW I am still using quote-fix and I'm hoping the quoting is better.


I'm having trouble figuring out what those beliefs might have been. Did
you think that you could look *only* at what type a function returns and
decide that it's the right thing? If you were writing mathematical
software, how would you decide whether to use sin() or sqrt()?

Please note that I wrote "not *JUST* on the basis of what type it
reuturns".

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      05-07-2010
"Bill Cunningham" <(E-Mail Removed)> writes:
[...]
> Beej says:
> int send(int sockfd, const void *msg, int len, int flags);
> sockfd is the socket descriptor you want to send data to (whether it's the
> one returned by socket() or the one you got with accept().) msg is a pointer
> to the data you want to send, and len is the length of that data in bytes.
> Just set flags to 0. (See the send() man page for more information
> concerning flags.)
>
> Some sample code might be:
>
> char *msg = "Beej was here!";
> int len, bytes_sent;
> .
> .
> .
> len = strlen(msg);
> bytes_sent = send(sockfd, msg, len, 0);
> .
> .
> end quote.
>
> len has been declared an int and Beej has strlen passing value to
> it.But strlen returns a size_t. That's the thing I don't understand about
> send().Bill


Look at your system's documentation for send(). What is the type
of its third parameter?

Are you writing calls to functions without reading their
documentation first? For the sake of everybody's time and sanity,
stop doing that.

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
poor style, but as long as it's within the range of both, there's
(probably) no problem.

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Bill Cunningham
Guest
Posts: n/a
 
      05-07-2010
Keith Thompson wrote:
> "Bill Cunningham" <(E-Mail Removed)> writes:
>> Keith Thompson wrote:
>>> No, you've just been failing to understand the code you're reading.
>>>
>>> The author used strlen because he wanted to compute the length *of a
>>> string*. He didn't use it "because of the size_t"; he used it
>>> because it was the correct tool for the job.

>>
>> I see.
>>
>>> Choose which function or operator to use on the basis of what it
>>> does and whether that meets your requirement, not just on the basis
>>> of what type it returns.
>>>
>>> Don't guess.

>>
>> This turns upside down some of my beliefs about functions and
>> return types. BTW I am still using quote-fix and I'm hoping the
>> quoting is better.

>
> I'm having trouble figuring out what those beliefs might have been.
> Did you think that you could look *only* at what type a function
> returns and decide that it's the right thing? If you were writing
> mathematical software, how would you decide whether to use sin() or
> sqrt()?


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));

because math takes a double as parameter. It wants something that is going
to return a double not just an int. The int isn't big enough.

Bill


> Please note that I wrote "not *JUST* on the basis of what type it
> reuturns".



 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      05-07-2010
"Bill Cunningham" <(E-Mail Removed)> writes:
> Keith Thompson wrote:

[...]
>> I'm having trouble figuring out what those beliefs might have been.
>> Did you think that you could look *only* at what type a function
>> returns and decide that it's the right thing? If you were writing
>> mathematical software, how would you decide whether to use sin() or
>> sqrt()?

>
> 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.


Either you're trolling, or you've completely missed my point.

Suppose you're writing code that needs to perform some mathematical
operation.

Question: How do you decide, at a certain point in the code,
whether to call sqrt() or sin()?

Answer: Call sqrt() if you need to compute a square root; call sin() if
you need to compute a trigonometric sine.

[snip]

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Bill Cunningham
Guest
Posts: n/a
 
      05-07-2010
Keith Thompson wrote:

[...]

> Question: How do you decide, at a certain point in the code,
> whether to call sqrt() or sin()?
>
> Answer: Call sqrt() if you need to compute a square root; call sin()
> if you need to compute a trigonometric sine.
>
> [snip]


Oh I thought we were talking about return values.

Bill


 
Reply With Quote
 
Tom St Denis
Guest
Posts: n/a
 
      05-08-2010
On May 7, 9:01*pm, Geoff <(E-Mail Removed)> wrote:
> 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.

Tom
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      05-08-2010
On 05/ 8/10 01:10 PM, Tom St Denis wrote:
> On May 7, 9:01 pm, Geoff<(E-Mail Removed)> wrote:
>> 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.


Also consider

#include <string.h>

void f( size_t );

int main()
{
const char* p = "hello world";

f( strlen( p ) );

return 0;
}

A decent compiler will optimise "strlen( p )" to 11.

--
Ian Collins
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
sizeof (size_t) and sizeof (pointer) Alex Vinokur C++ 19 11-30-2007 11:11 PM
sizeof(EmptyStruct) in C and C++ (was: Base {}; sizeof(Base) == 1?) Alex Vinokur C Programming 7 08-14-2006 04:57 PM
#define ARR_SIZE sizeof(arr)/sizeof(arr[0]) Vinu C Programming 13 05-12-2005 06:00 PM
sizeof(enum) == sizeof(int) ??? Derek C++ 7 10-14-2004 05:11 PM
sizeof(str) or sizeof(str) - 1 ? Trevor C Programming 9 04-10-2004 05:07 PM



Advertisments