3 ms·
C null terminated strings are not magic. Protocols between the sender and receiver have to be defined. You face exactly the same problem regarding byte order, s
by WastingMyTime89 4y ago
C null terminated strings are not magic. Protocols between the sender and receiver have to be defined. You face exactly the same problem regarding byte order, size consideration and marshalling with C strings.
You are basically arguing that C strings are a good default because they are the default. If they were something else, well, we would have saner FFI in a lot of place.
C strings are a terrible default. They are fundamentally unsafe. Mishandle the null char for any reason and you now face a serious security issue.
- kazinator 4y ago> You face exactly the same problem regarding byte order, size consideration and marshalling with C strings. Not char/byte C strings. You might be thinking of wchar_t strings. > Mishandle the null char for any reason and you now face a serious security issue. With what string data structure can applications peak into and mishandle an implementation detail and not risk creating a security problem? Same you have a structure with the length and a pointer. Mishandle the length field or pointer and you have a problem. It certainly is a problem and it's common for C applications to take on responsibilities for manipulating the internals of the string structure.
- WastingMyTime89 4y ago> Not char/byte C strings. You might be thinking of wchar_t strings. No I’m talking about the classic byte arrays C pretends are strings. If the receiver you are talking to is not expecting a chain of bytes where each byte is a character and one special value means the chain is over, well, you will have to transform what you are sending to what your receiver is expecting.
- kazinator 4y agoIf somebody's expecting something other than a C string then we unsurprisingly have gratuitous complications. If everyone agrees on the C string though, it just interoperates as-is. Code compiled for some 16-bit microcontroller can just shove the C string into its transmit buffer, and on the other end, code compiled for 64 bit big-endian Power PC receiver just uses it as-is.
- timbit42 4y agoC is the biggest mistake in the software industry since the beginning of software. The people that worship C only do so because they haven't seen the better paths the software industry could have chosen.
- kazinator 4y agoE.g. Donald Knuth hadn't seen better paths. "I think C has a lot of features that are very important. The way C handles pointers, for example, was a brilliant innovation; it solved a lot of problems that we had before in data structuring and made the programs look good afterwards. C isn't the perfect language, no language is, but I think it has a lot of virtues, and you can avoid the parts you don't like. I do like C as a language, especially because it blends in with the operating system (if you're using UNIX, for example). All through my life, I've always used the programming language that blended best with the debugging system and operating system that I'm using. If I had a better debugger for language X, and if X went well with the operating system, I would be using that." Dec 7, 1993, Computer Literacy Bookshops Interview.
- asveikau 4y agoThis is ridiculous hyperbole. Nobody "worships" C and people who have positive experiences with C often have exposure to higher level languages. You can find strengths or upsides in C and still acknowledge faults, and still acknowledge merits elsewhere.