https://www.digitalmars.com/articles/C-biggest-mistake.html
and does not break existing code.
https://www.digitalmars.com/articles/C-biggest-mistake.html
and does not break existing code.
- O(n) algorithms that are constantly scanning strings looking for a zero byte
- strings can't contain zero bytes
- strings have to contain one zero byte
- putting a zero byte in the middle of a string chops it off
- probably other nonsense I haven't thought of
Please don't adopt another half-assed solution just because it fits more easily into C's existing set of crap. That's how we got fake strings in the first place.
It is very, very rare to see a buffer overflow in D because the use of these arrays is so easy, convenient, and robust.
Not only does it virtually eliminate buffer overflows (when used), it is more efficient than 0 terminated strings. It does not need to scan the strings, nor does it need to load the string into memory to determine its length.
I understand your concerns about mixing it up with 0 terminated strings. They are real, but have not been a particular problem in practice. What happens is one simply moves away from using 0 terminated strings. A zero terminated string can be converted to a length one with:
a = s[0 .. strlen(s)];
Going the other way requires a memory allocation similar to what strdup() does.-----
void foo(char a[..])
meaning an array is passed as a so-called “fat pointer”, i.e. a pair consisting of a pointer to the start of the array, and a size_t of the array dimension.
-----
I didn't see a "current length" mentioned. Is it there? Can I have a string with an allocated length of 20 bytes and a current length of 10 bytes, without using a looking for a zero byte?
This proposal is not about memory management any more than 0 termination is about memory management. It is just about finding the end of the array.
(And I'm not sure how the alternative fat-pointer proposal you mentioned does better. Searched that page for references to strlen, but didn't find much.)
What is the actual source of bugs in this regard? How does passing the length as a separate parameter lead to more bugs than having it bundled -- is the main source of error passing the wrong variable?
There are string libraries that work that way in C.
If you do it manually, without a lib, then you need to check that the length is valid yourself (after every operation), so it's not as useful as a language with first class support for it.
With it as part of the syntax, it becomes natural to use them. I'm not making this up, it is based on extensive experience.
The runtime overflow checks can be turned on and off with a compiler switch, so it becomes trivial to see what performance effect it actually has. Critical loops can be coded with ordinary pointers as necessary. For the rest, the performance effect is not measurable.
Again, this is from experience, not supposition.
It boils down to being inconvenient, unreliable, error prone, and difficult to audit. That's why it isn't used and C's #1 problem remains buffer overflows. And so it goes for all the other solutions for C for this problem, except my proposal.
My proposal is how D works, and it's been convenient, reliable, robust and auditable for 20 years. You can still use raw pointers in D, they are fully supported, but the use of the arrays make use of raw pointers rare.
There is also the null hypothesis that buffer overflows are simply a category of bugs that is simply easy to identify, and thus, apparently prevalent.
Why is Java missing in that list?
So it would look something like MemorySegment.allocateNatice(100, someScope). This new API has a runtime ownership model, so by default only a single thread can access this memory address, and it can be freed at will.
Or maybe they omitted it before there are literally hundreds of languages that were inspired from C and listing them all would have been boring for the reader.
Well, that was my interpretation of what he said, errors are my own etc. But this would make Java a direct descendant of C++, in my mind.
But Guy Steele claimed "We were not out to win over the Lisp programmers; we were after the C++ programmers. We managed to drag a lot of them about halfway to Lisp."
'At that stage, C and C++ "absolutely owned the universe"' - https://www.zdnet.com/article/programming-languages-java-fou...
They took a lot of inspiration from C/C++'s syntax and seemed to be pretty concerned with improving memory management, security and developer velocity.
I was around at the time and C++ was trendy so Sun were marketing it as the future for C++ developers. It was definitely influenced by what was in vogue at the time even if it doesn’t adopt all of the traits of C++.
I remember this because I wasn’t a fan of C++ back then as I’d come from the ALGOL family of languages so found C-style syntax a little alien (and tbh I still don’t like C++ now even though I’ve since warmed to C’s syntax) so it took me years before I warmed to Java.
In particular, if Java kept (almost?) all the keywords, and the operators, and the statement terminators, and the block delimiters, and the same approach to object-oriented... how is it not derived from C++?
Java's object semantics are explicitly intended as a streamlining of C++, the keywords are the same for the most part, and it was sold as a C++ which runs anywhere with no memory leaks.
Note that I mentioned the semantics: the object semantics of Java and C++ are so similar as to have corrupted the entire concept of objects in their favor.
This wasn't an accident, and it wasn't malice, it just feels like it sometimes.
Thanks to reflection and a featureful VM, Java does have a significant amount of dynamic behaviour that can be used to implement a lot of features of the smalltalk side of the OO family tree.
No, but it was the only one that mattered at the time, as far as adoption was concerned, and regarding marketing Java as similar to existing programmers and their managers...
That's also how it was hyped at the time and the kind of people it was sold too (I was -barely- there).
There was no attempt to replace Objective-C with Java on OS X.
Apple was unsure if the strange look from Objective-C would ever appeal to the Object Pascal/C++ communities of Apple developers, thus they used Java wave as plan B, in case Objective-C was rejected by them.
As this did not happen, there was no reason to keep plan B around.
I would say there was a heck of an attempt with the Java-Cocoa bridge that didn't do well because Java didn't have a lot of the dynamic nature of Objective-C. They certainly to my eyes as a developer tried to push Java.
Do you actually believe that Jobs liked Java, when Apple was created on top of Object Pascal and C++, and then he was responsible for bringing Brad Cox to NeXT?
Java was already available on System 7.
Java was definitely not on the picture when Apple went to CERN doing their OS X marketing sessions.
In fact, you now made me dig out some stuff.
https://developer.apple.com/library/archive/documentation/Ja...
https://developer.apple.com/library/archive/documentation/Co...
> This document discusses issues that arise when writing Java applications with Cocoa, which is implemented in Objective-C.
No Java here on the OS X announcement:
https://youtu.be/SjlLG1EzJ2k?t=4450
The only message was Java being first party on OS X, as in the System 7 days, the JVM was not from Apple rather a third party. Thus the announcement at JavaONE 2000.
https://www.javacoffeebreak.com/articles/javaone00/index.htm...
You will not find in those CDs anything like this:
> Swift is a successor to both the C and Objective-C languages. It includes low-level primitives such as types, flow control, and operators. It also provides object-oriented features such as classes, protocols, and generics, giving Cocoa and Cocoa Touch developers the performance and power they demand.
Interfaces, dynamic code loading, JAr bundles, lightweight class type reflection, all trace back to Objective-C, or Smalltalk, if one wants to be pendantic.
In case you missed it, even JEE started as an Objective-C framework for the Spring distributed OS, Distributed Objects Everywhere.
https://en.m.wikipedia.org/wiki/Distributed_Objects_Everywhe...
Of course, Java doesn't have dispatch. And it's also true that Gosling's team originally considered, then rejected, C++ in favor of building Oak. But Oak borrowed an awful lot directly from Obj-C, and only later underwent a lot of syntactical surgery (turning into Java) in order to "look" like C++ specifically to attract C++ programmers, even though it didn't feel like C++ at all. This is pretty well documented.