Hi all
strlen() defined in string.h return size_t. What is it?
Thank you
Hi all
strlen() defined in string.h return size_t. What is it?
Thank you
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.
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:
Thanks againCode:int len; len=strlen(string);
int is signed by default.
try:
Code:unsigned len; len = strlen(string);
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
Are you using the highest warning level?
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.
Thank you guys!
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:
z will always be equal to x. The bits themselves are preserved. The meaning of those bits is not.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.
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.
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.
> 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.
>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.