News for hackers. Stuff that matters.
Re:Conflating pointers with arrays: C's biggest mistake? (2009) by WalterBright on Sunday July 22, 2018 @03:48AM (#17585724)
Your example does save on code size (but not performance, due to the 3 strlen()'s). But where it won't save on code size is when, say, one wants to retrieve just the filename from filename.ext. One has to strlen it, allocate memory, copy the memory, then remember to free it exactly once.

With dynamic arrays, just return a slice.

Re:Conflating pointers with arrays: C's biggest mistake? (2009) by kyberias on Sunday July 22, 2018 @04:06AM (#17585780)
You say strlen() but you probably mean "code has to iterate all the string elements".

This doesn't happen 3 times, though:

> char foo[9]; strcpy(foo,"abc"); strcat(foo,"1234");

Strcpy has to iterate through all the elements because it copies them. This would happen regardless. It doesn't do strlen.

Strcat has to find the end of the destination string, so it has to iterate (or call strlen). Then it's just strcpy again.

Instead of 3 strlens, there is 1.

Do you not understand how C string/arrays work, or why do you insist on 3 strlens?

Re:Conflating pointers with arrays: C's biggest mistake? (2009) by tjoff on Sunday July 22, 2018 @04:30AM (#17585852)
> Strcpy has to iterate through all the elements because it copies them. This would happen regardless. It doesn't do strlen.

Iterate all the elements and copy a fixed size is two very different things.

strcpy has to read, byte for byte, and check it for null (which is exactly what strlen does). A "real" copy would just blindly copy a chunk of memory with no other processing on it. The speed difference is huge.

Re:Conflating pointers with arrays: C's biggest mistake? (2009) by WalterBright on Sunday July 22, 2018 @04:37AM (#17585864)
You're right. I apologize for using strlen metaphorically rather than literally.
Re:Conflating pointers with arrays: C's biggest mistake? (2009) by jibal on Monday July 23, 2018 @05:59AM (#17591662)
> You say strlen() but you probably mean "code has to iterate all the string elements".

Yes, but that's much more expensive than a memcpy (of the two source strings) or just knowing the length (of the string in the target buffer).

> Strcpy has to iterate through all the elements because it copies them. This would happen regardless.

No, it wouldn't; memcpy is generally a lot faster than strcpy.

> Do you not understand how C string/arrays work

Do you not understand that he's written a few C compilers, and designed and implemented a language that is known for its runtime compatibility with C?

> or why do you insist on 3 strlens?

Um, you already noted that he really means "code has to iterate all the string elements". What he should have said is that you have to find 3 NULs. That's an expensive operation even when you're copying the string while finding it.

Re:Conflating pointers with arrays: C's biggest mistake? (2009) by WalterBright on Sunday July 22, 2018 @04:28AM (#17585842)
See my other reply to this above.