- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
Try that with a wchar_t "string": OOPS!
Admin
I think the point was that using sizeof() to get the size of a string is in some sense somewhat dangerous, because while there are circumstances in which you can certainly use it, you have to be aware of the context in which you are. So for instance, if you have a declaration
you could use sizeof(), but this is somewhat brittle because if someone changes it to the size calculation silently breaks.Admin
So we can use true, false, and FILE_NOT_FOUND
Admin
The data is of type char. pName happens to be a pointer to the data rather than the actual data.
It wasn't an arbitrary syntax decision. It was done that way because the type system was designed around the view I mentioned before. It's also why it's "char data[10];" and not "char[10] data;"
C syntax makes a lot more sense when you understand why it is like it is.
Admin
This is a wank. There is nothing wrong with C strings. There can be a LOT wrong with length-specified strings, especially if the length field is too small, eg. 8 bit, ala classic Pascal.
CAPTCHA - putting in your CAPTCHA is also a wank.
Admin
Admin
Nothing funny you can say about strings? Pshaw!
Two strings walk into a bar. The bartender asks them, "What'll you have?" The first string says, "I'll have a bourbon and Coke." The second string says, "I'll have a beer.zxd43ezz 01020304^M\032". So the first string says, very quietly, "You'll have to excuse my friend. He's not null-terminated."
Admin
I still think Delphi has the most elegant way of expressing strings. Doesn't have the speed issues of java/lua etc, nor the sentinel of c. For those that don't know how it works, it acts exactly like a c-string - is null terminated, to be compatible with windows apis. But the 4 bytes preceding the first character are the length, and the 4 bytes before that are a reference count for automatic garbage collection.
Admin
But in general, counted strings are very good. Two key benefits are that they 1: can contain binary data safely (useful annoyingly often), and 2: allow really efficient processing.
The second point is important, and stems from the fact that if you're not looking for a zero byte, you can (or rather your compiler can generate the code to) actually move or otherwise process many bytes at once rather than one at a time. It also allows you to avoid the O(n) operation strlen.
Admin
:P
Admin
Yeah, delimiters suck. Bring back Hollerith constants.
(Hint: "This is some text" <-- sentinel value fixed-size count --> 17HThis is some text )
Admin
It starts to scan from the given character, until it find things unrelated to the start of the string. Then it would not the length without a need for sentinel. Now the code makes sense :)
Nadav
Admin
OK, now I'm feeling stupid. I thought c-strings were either null-terminated, or terminated by the end of the array. I can, however, not quickly find any support for this belief (and plenty for the opposite). I know this used to be a problem securitywise.
If you think about it this works fine for strings. Just read untill the 0 or end of allocated memory. No need to keep track of size or allocate an extra byte for the 0. I guess I'm wrong.
However I would like to point out that strings aren't a primitive type in C. So if you don't like the implementation, you can do something about it.
I would also like to point out that C is really usefull for writing programs that need to use procesor and memory efficiently. Considering they used lose the first 2 digits of the year to save space, one can understand null-terminated strings. If you don't need to do things efficiently, then you can go play with Java or some other high level language. In fact, you should only use C when it fulfills a requirement.
Languages are all made for a purpose. Just because I can do everything you do in Java in ps, doesn't mean I should (well, ok I can't, but someone can. In fact, I know a guy who used to write ps programs so he could use the printer for extra processing power). I can also use a shotgun to swat flies, but then I shouldn't complain about the holes in the wall.
Admin
I always liked the VAX version of string descriptors. The string descriptor would contain, among other information, the length of the string (16 bits) and the memory address where the string is stored.
This is similar to the way Pascal and direct ancestors to C (such as BCPL) encode strings. The problem with BCPL is that the length is encoded as a byte, meaning your strings couldn't be longer than 255 characters ...
Admin
And according to another poster, it's how Pascal (at least some variants) does it as well - could've sworn it was 16 bits.
Personally, I tend to use a simple string structure when I'm coding in C, something like:
Admin
Not correct. sizeof in the original code will provide you with the size of "pointer to string", which is 4 (on the platform I use most often) irrespective of the length of the string.
Admin
Make that "pointer to char", please...
Admin
Yes, you can do something about it if you don't mind remaining compatible with the seventeen zillion APIs that use null-terminated strings.
(Okay, so this isn't actually hard to do, but you would have to keep it in mind.)
You can't tell me that they weren't ALSO concerned about speed though, and turning an O(1) operation into O(n) in order to save one extra byte (at the time, 2 bytes for the length would have worked fine) in SOME strings (if you assume a random distribution, a word size of two bytes, and that objects are aligned on word boundaries, then half the time) doesn't seem like a good trade-off to me.
Admin
Admin
sizeof is a unary operator, not a function. If you write it sizeof() that makes it look like a function.
You could just write the line like this: int nLen = sizeof mystring;
The C grammar has a specific production which recognizes sizeof and then a typename inside of parentheses as a case of a unary expression.
If you've already got an expression, putting it inside of parentheses turns it into a postifix expression and then a unary expressionwhich matches another production which recognizes sizeof followed by a unary expression.
So, even when you need the parentheses on a typename [like sizeof (int) does] it's still not a function call.
Admin
In fact, if you give it an array, it does give you the size of the array. Not if you give it a pointer, though.
Admin
So the "*" goes with the TYPE you say? Ever looked up what types
declares?
Admin
Not to be rude, but constant printf format strings should be dissected by decent-enough compilers into calls to RTL functions that do the actual work. This is standard practice on embedded platforms, where the combined monstrosity of a printf may not even fit into the code memory.
Admin
What if you're halfway through a longjmp that modifies x? In a multithreaded program? Where x could be anywhere and your head's up your rear?
I've had people ask me before why I've had code that looks much like that but with a static thrown in before the char declaration. To them, I say, "Braaaaaaaaaaaaaaains."
I have a OCR that could probably recognise that captcha.
Admin
It returns 8 on my system.
Admin
I'd say Roman C strings would always be 100 characters?
-P.P.
Admin
Just get it over with and use Assembler instead.
Admin
snprintf was not added to the C standard until 1999. Before then, there were various implementations of it around with slightly different behaviour. I suppose it is good that MS gave theirs a different name (_snprintf) to show that it might be non-standard. Another point of difference is that some of these older versions of snprintf would return different values than the current standard specifies.
Admin
Oh horror, that reminds me of the Cyber with 60 bits words and 6 bit characters. Aaaaargh!
M4
Captcha: validus. No this kind of madness is never validus!
Admin
Back in my day, we only used six (FIELDATA, anyone?).
Rumors have it that before my days, only five bits were used (punched paper strips).