Thread: strlen

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    84

    strlen

    Hi all

    strlen() defined in string.h return size_t. What is it?

    Thank you

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    What is what? size_t? It's an unsigned integer type capable of holding any result from sizeof.
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    84
    oops sorry,
    Thank you, I didn't know what is size_t.
    If size_t is always unsigned why my compiler don't give me a warning with the following code:

    Code:
    int len;
    len=strlen(string);
    Thanks again

  4. #4
    Madly in anger with you
    Join Date
    Nov 2005
    Posts
    211
    int is signed by default.

    try:

    Code:
    unsigned len;
    
    len = strlen(string);

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    84
    I know I can use unsigned type, but why when using SIGNED type my compiler don't give me an error saying:
    Possible lost of data converting unsigned to signed.

    Thanks again

  6. #6
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Are you using the highest warning level?

  7. #7
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    The only time a C compiler ever gave me a warning concerning the signedness of a integer was if I compared a signed and unsigned variable.

    As far as I can tell, C compilers won't stop you from assigning an unsigned number to a signed variable like len, but that doesn't mean there is no problem in doing that. The integer could overflow if the string were long enough (say 2^15 in length). If this is a problem, use unsigned longs or size_ts.

  8. #8
    Registered User
    Join Date
    Dec 2007
    Posts
    84
    Thank you guys!

  9. #9
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by citizen View Post
    As far as I can tell, C compilers won't stop you from assigning an unsigned number to a signed variable like len, but that doesn't mean there is no problem in doing that. The integer could overflow if the string were long enough (say 2^15 in length). If this is a problem, use unsigned longs or size_ts.
    The integer might "overflow" in the sense that a positive number might become a negative representation, but the actual bit pattern is guaranteed to be intact. In other words, if you do:

    Code:
    unsigned int x;
    int y;
    unsigned int z;
    
    y = x;
    z = y;
    z will always be equal to x. The bits themselves are preserved. The meaning of those bits is not.

  10. #10
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    > z will always be equal to x. The bits themselves are preserved. The meaning of those bits is not.

    If the bits aren't presented properly, the way they should be, than that is indeed an issue. I'd simply contend that trying to express a number bigger than a container can hold is the very definition of overflow. Whether or not z is equal to x is immaterial. You've merely repeated the solution to the problem.

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I think you'll find that different compilers are more or less picky on unsigned to signed conversions.

    Being overly picky may just irritate programmers who are used to writing code using int for strlen() - very few strings are so long that size_t is actually required - you could only have ONE of those strings in most cases, since size_t is usually equivalent to (or greater than) the addressable range of the OS - so a 32-bit unsigned requires the string to be 2GB+ in length, you can't have two of those strings.

    Of course, the compiler don't know if this is the case or now - if you read in the entire Shakespeare's works into a single string, you may end up with a string that is long enough, perhaps...

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > if you read in the entire Shakespeare's works into a single string
    All the words, and how many times each is used, are listed here - http://www.opensourceshakespeare.com/concordance/
    Factoid of the day
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  13. #13
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by citizen View Post
    If the bits aren't presented properly, the way they should be, than that is indeed an issue. I'd simply contend that trying to express a number bigger than a container can hold is the very definition of overflow.
    Nothing is being expressed. It's just bits. When you stick the bit pattern 11111111 into an unsigned char you get 255. In a signed char, you get -128. The computer doesn't care. The problem exists only in your mind.

  14. #14
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    >The problem exists only in your mind.

    Your assertion that human perception doesn't matter when you use a tool is ludicrous. If 1111... means two different things depending on how the computer presents it, and I'm the end-user of some application, then wouldn't I notice getting an incorrect answer? It's suddenly not a bug because you neglected to make the computer print something properly, yet the underlying bit pattern happens to be the same? This whole debacle is semantic.
    Last edited by whiteflags; 01-11-2008 at 10:19 AM.

  15. #15
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by citizen View Post
    >The problem exists only in your mind.

    Your assertion that human perception doesn't matter when you use a tool is ludicrous. If 1111... means two different things depending on how the computer presents it, and I'm the end-user of some application, then wouldn't I notice getting an incorrect answer? It's suddenly not a bug because you neglected to make the computer print something properly, yet the underlying bit pattern happens to be the same? This whole debacle is semantic.
    The point I'm trying to make is that overflow is a operational problem, not a representational one. If you have an 8 bit quantity 255 and add 1 to it, it rolls around to zero. That's overflow.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Playing around strlen :)
    By audinue in forum C Programming
    Replies: 6
    Last Post: 06-13-2008, 03:22 PM
  2. strlen help
    By stewie1986 in forum C Programming
    Replies: 10
    Last Post: 12-04-2007, 12:15 PM
  3. strlen in expressions
    By justforthis1 in forum C++ Programming
    Replies: 4
    Last Post: 10-24-2006, 10:28 AM
  4. strlen()
    By exoeight in forum C Programming
    Replies: 9
    Last Post: 04-01-2005, 10:18 AM
  5. Just say NO to strlen.
    By anonytmouse in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 02-11-2005, 01:34 PM