Please create an account to participate in the Slashdot moderation system

 



Forgot your password?
typodupeerror

Comment Re:Knowing when not to (Score 1) 345

The most dangerous bit of C++ to use is ... ... the entire subset of C++ that is C!

With C++11, if you avoid pointers, and do everything with value semantics (along with a little std::shared_ptr when necessary), then you have a very powerful and predictable and easy-to-teach language.

Don't learn C first, before learning C++. If anything, it should be reversed. If I was expected to teach C to complete programming beginners, I would teach modern C++ first. There's a nice subset of C++ which allows you to get on with getting algorithms implemented, without having to give any thought to memory leaks or memory layout. And without the magical "action at a distance" you get with Java pointers.

Comment Re:Perl is more expressive (Score 1) 192

> Raw pointers are still useful, but not as owning pointers.

I do agree they will be used, alongside all other kinds of pointers, in much modern software. And rightly so. And, of course, I use them in my own code. But I'm thinking strictly of teaching programming to complete beginners. Perhaps I shouldn't have said they are obsolete in general, I should have said "obsolete for teaching purposes when teaching beginners". I would argue that, at first, everything should be passed by value in such a course, then eventually building up to the use of C++-references where the side effects are desired. Then I would introduce shared_ptr, but only after the students have made a lot of progress with the use of by-value and by-reference parameters.

A lot of progress could be made in teaching with a combination of by-value, `&`-references and `shared_ptr`. Any interesting data structure or algorithm can be taught using these three. There would be no need to panic about writing destructors, or about pointer arithmetic, or any of that stuff. Students could focus on their algorithm and not on the nasty error-prone stuff that's not directly helpful to the real-world algorithm they're trying to write. ("I just want to sort a list of strings that the user has entered, why should I care about pointer-arithmetic, and double-deletion, and memory layout and so on?")

All of the unnecessary stuff (pointers, C arrays, object orientation) can be delayed until quite late in the students' education. None of them are necessary for writing linked lists, or implementing quicksort, or interacting with the user and/or filesystem. And even if we feel that pointers must be taught at some point in the undergraduate education, then we should still delay them until after they are very comfortable with shared_ptr and are comfortable with the whole idea that some objects are shared between multiple variables (variables which may have very different names).

Comment Re:Perl is more expressive (Score 1) 192

> This is the first time I've ever heard someone say C++ is easy to learn.

C++ is easy to teach and easy to learn. Much easier than Java, for example. But I should stress that I mean 'modern C++'. Yes, technically, all C code is valid C++ code. But modern C++ has replacements for pretty much every C feature, especially the hard to teach stuff from C. A good teacher of introductory programming will teach C++ and will not teach C. For example, raw pointers are just obsolete now. In fact, you could teach a lot of introductory programming without any pointers or references or anything else that gives magical "action-at-a-distance" properties.

I've had great fun with friends, who are trying to get into programming, by confusing them with Java (and C). An int is passed by value in Java. But if you have an Address structure in Java, changes made in a function are magically visible outside. There is no easy way to force by-value passing in Java. Finally, there is a third kind of behaviour in Java. When you pass a String, you basically get value semantics instead (because you do s = "updated"; instead of s.assign("updated")). (Not a criticism of Java only, the same problems occur in C. "I though you said int arrays are (kinda) passed by reference thanks to pointer decay, so why did my code char_ptr = "updated" not cause the change to be visible in the parent function?")

Frankly, I think it's undignified that teachers are bogged down trying to explain this nonsense at the very earliest stages of teaching programming. It's already difficult enough trying to explain that parameter names in functions don't really mean anything but are merely placeholders for "first argument", "second argument", .... If a function parameter has the same name as the local variable in the parent function that's passed to it, then that is just a coincidence basically. It's difficult to have to say "well, sometimes the effects are visible outside the function, so I guess the 'outside' name can appear to be meaningful".

In the early stages of teaching, it's best to be able to say that a function just takes the values and is a black box that gives you a new answer. In C++, everything is passed by value by default, and returned by value. That's really consistent, and easy to teach. You can teach a lot of introductory programming with value semantics, writing some fun programs. Best of all, there are none of the "magical" side-effects of pointers/references to confuse the student. Every desired side effect must be explicitly done, perhaps printing of output or via return values.

When the programmer wants reference semantics in C++, they can explictly use C++ references to get them. The teacher can introduce them when the class is ready, rather than being forced to introduce them too early, as in Java or C.

Everything is, by default, passed by value in C++. And anything can be passed by reference when requested. Two kinds of passing, under the explicit control of the student. But Java basically has three kinds of passing (primitives, classes, and the String-like behaviour that's kinda like a primitive but not really). Why should we have to explain stacks and heaps and memory layouts at such an early stage?

And C++ developers never have to think about allocation and deallocation. Nor about pointers. Nor about action-at-a-distance. A vector of ints is passed the same way as an int is. The standard implementations of all the containers just work, on any type and by-value. The student can declare a class struct Person { string name, int date_of_birth }; and can be confident that everything will be allocated and deallocated automatically as need.

Comment Re: Short of memory? (Score 1) 165

> Grapheme cluster? (what most users would consider a character)

Is there any easy way to tell where one grapheme cluster ends, and another begins? With UTF-8, it's easy to count the bits to see where one codepoint begins and ends, I hope there is something equally simple for grapheme clusters. Or perhaps it's all complicated and is different for each language?

Also, if I do accidentally split a grapheme cluster in two (while respecting codepoint boundaries), what will happen? If I attempt to display the two strings, can I expect a sensible result, or will the result be garbage?

Comment Re:Obligatory (Score 1) 161

> But this is slashdot.

Yes. I made a simple, non-offensive, true, observation about Java, and you assumed that I wanted to start a flamewar on the topic! There's is nothing more predictable on programming websites than a childish Java-vs-C++ flameware.

> Yes, Java does not let you take the address of a local variable.

You agree that I told the truth. I didn't say Java was *slow*, nor did I imply that java runtimes hadn't developed certain optimizations. We *could* have an argument about speed, but I'm confident I won't hear an argument in favour of Java that I haven't heard a thousand times already. (And you feel the same regarding arguments in favour of C++, I'm sure!)

Yes, I admit my original comment did demonstrate that I disagreed with the decisions in Java. I shouldn't have done that. But arguing about this is irrelevant for Rust. And, crucially, I didn't say that the Java design made it slower. I just said that Rust and C++ are quite similar and therefore the performance will be no worse than C++ (whenever we get really well-tuned Rust compilers).

Looking again at my original comment, this is the only time I mentioned Java:

> [Java took a very different approach to the problem of "how to we get rid of segfaults and memory corruption". Java basically banned all interesting use of the stack, forcing everything onto the heap, and barred developers from using RAII. Nowadays, with more advanced compilers able to do advanced lifetime analysis, we can reconsider languages - such as Rust - that take a less draconian approach.]

I probably shouldn't have used loaded terms such as "interesting use", "forcing" and "draconian". But basically, I was quite neutral and I said nothing about speed.
I didn't say anything about which language had taken the "correct" or "fastest" approach. I simply observed that various languages had taken different approaches. In the context of a new language called Rust (remember, we are talking about Rust in this thread) I thought it would be useful to compare the approach in Rust to the approach in two languages that are much more popular at the moment - Java and C++. I was being informative, not argumentative.

C, and "old" C++, has big problems regarding safety and the lifetime of variables. Modern C++ has greatly improved things (value semantics, move semantics, unique_ptr, shared_ptr). Java went back and took a very different approach. It defined a much smaller language. For C++ developers, the absence of destructors is probably the single most definitive aspect of Java -- for good or bad, that is what makes Java Java.

This is a discussion about Rust. My observation was simply that Rust is basically about making C++-style programming work "as-is". A C++ developer moving to Java would have to learn to program very differently, for example they would be required to manually free non-trivial resources in Java - database connections, especially data written to the connection that hasn't been flush()ed - whereas Rust allows C++ developers to keeping writing C++-style code (including relying on destructors to automatically free their resources for them) and get lots more safety for free.

Comment Re:Obligatory (Score 1) 161

> Rust would squash this even though it's perfectly valid [and desired].

It wouldn't. It depends on the implementation of biggestCircle. I should have expanded on this, but the simple version of biggestCircle would simply return one of its arguments, the pointer-to-Circle which has the biggest area. (I'm writing my examples in C++, I would make a mistake if I tried to write it in Rust):

        Circle * biggestCircle(Circle * p, Circle * q) {
                return (p->getArea() > q->getArea()) ? p : q;
        }

With this implementation, the lifetime or the return value is (to be conservative) the shortest lifetime of the two inputs. The implementation of foo() that I gave is therefore incorrect as its return value tries to live longer than the local variables c1 c2. The implementation of foo() is dissallowed therefore - and it would be invalid to do the following as it would attempt to free() something on the stack, totally unacceptable in any language:

        Circle* x = foo(); // x points to stack space that is no longer valid, as foo() has returned. We can't even read from this pointer
        free(x); // and this is undefined behaviour

> Or biggestCircle might return something only obliquely related to c1/c2. ...

True. You're saying that biggestCircle might be implemented differently and might return a pointer to an object with a (provably) longer lifetime. That's no problem to Rust. The compiler can see the implementation of biggestCircle and be as relaxed or as fussy as necessary, depending on the implementation of biggestCircle and also depending on how biggestCircle is called from other functions.

Comment Re:Obligatory (Score 1) 161

> Come on, that's a strawman. In every modern system it throws an interrupt, which is usually not caught, and optionally dumps a core

I gave the wrong example. Null pointers aren't half as serious as dereferencing a non-null pointer to a local variable in a function that already has returned. In C and C++ this really will do random crazy things quite often.

> However, I'm not sure it justifies a new language ...

I guess some or all of this lifetime-analysis technology could be included in a C++ compiler. But some extensions to the language are needed, for example is Rust you can annotate the function parameters with hints that help it to prove the lifetime. And I guess those kind of extensions aren't going to be officially accepted in C++ any time soon. Lifetime analysis isn't the only new thing in Rust, it's just the thing I'm most excited by currently. I really like their approach to polymorphism, it reminds me of Haskell. Adding these things to C++, without breaking C++, would lead to a syntax that isn't very concise.

In Rust, everything is const by default. I can't imagine C++ going that way soon - backwards compatibility would be a nightmare.

Ideally, Rust and C++ could inspire each other to improve. There's a lot of compatibility between them, so we could mix them.

Comment Re:Obligatory (Score 1) 161

Null Pointer Exceptions are certainly undesirable. But it is at least well defined behaviour, and you can catch the exception if you like.

In C and C++, dereferencing a null pointer is undefined. If you're lucky, you might get a segfault, but if you're unlucky it can do literally anything, including deleting all your files.

When I originally said:

> Java took a very different approach to the problem of "how to we get rid of segfaults and memory corruption".

I should have said:

> Java took a very different approach to the problem of "how to we get rid of undefined behaviour, including - but not limited too - segfaults and memory corruption".

Java did tackle this problem - it appears to have been a major principle behind it. And they did successfully do this. People might debate whether the price was worth paying, but this isn't the thread to discuss it. See to discuss Java furthur.

Our goal here isn't to discuss Java in detail, but to highlight the problem that is solved in Rust by lifetime analysis.

Comment Re:Obligatory (Score 5, Informative) 161

> Overall to me the language looks primarily based on C++, an attempt to smooth out the rough spots of C++,

It's like C++, but where segfaults are impossible. In C++ (and C) you might try to return a pointer to a stack-allocated object:

        X * foo() {
                X x;
                return
        }

Any decent compiler will warn you that this is buggy. But that's only because it's a blatant bug. It's possible to write much more subtle bugs, which lead to the same problems, but which modern C and C++ compilers will miss. As well as accessing stack variables after their valid lifetime, you might free some heap memory twice, or not at all, and so on.

(Disclaimer: I have essentially no real experience with Rust, but have followed it's development a little.)

Rust will not allow you to compile the program until it can prove that your program is safe, that you are not abusing the lifetime of anything. This allows us to have performance and safety without compromising on either. The language also limits at most one variable at a time to have write access to a given object, this is good for concurrency also (well, even single threaded code would benefit from this). Basically, you just write C++-style code, but instead of having to convince a code review commitee of the safety of your code, you can just say "the compiler accepted it, therefore certain safety guarantees can now be assumed".

For example,

        Circle * foo() {
                Circle c1(5);
                Circle c2(10);
                return biggestCircle(&c1,&c2);
        }

We know that would be invalid, but a C++ compiler wouldn't see any problem. The Rust compiler, on the other hand, can see that the lifetime of the return object of biggestCircle can be no longer than the lifetime of either parameter.

I am gradually becoming incredibly excited about this feature. But it's going to be interesting watching the future of the language. This kind of feature isn't appreciated unless you're an experienced C++ developer. So we might find that lots of important software, from rocket ship code to web browser code, is written in Rust in future but we might find that most people don't understand why! It will be kind of like C++ for many people, they dismiss it when they are new to software, but ultimately everyone goes to C++ (or, in future, Rust) instead! :-)

[Java took a very different approach to the problem of "how to we get rid of segfaults and memory corruption". Java basically banned all interesting use of the stack, forcing everything onto the heap, and barred developers from using RAII. Nowadays, with more advanced compilers able to do advanced lifetime analysis, we can reconsider languages - such as Rust - that take a less draconian approach.]

Comment Re:I probably would upgrade if I could, but... (Score 0) 437

One of my favourite apps, Roaming Control, doesn't work on Lollipop. They've made it impossible to turn mobile data on and off programatically - I believe other apps (like Tasker) are broken now too because of this issue. I regret updating now. I'm going to be much more hesitant over major upgrades of Android now. I'm already cautious of major Ubuntu upgrades.

I hope they fix this soon, and reallow this API. They can't call it an update if it has less functionality in some ways.

Comment Re:The one mistake Forbes keeps making.. (Score 1) 386

> [The one mistake Forbes keeps making] is assuming everybody is profit-motivated and is actually driven by "bringing something to market."

Well, I think the mistake they're making is that they think that a product must be on the shelf, with customers, within at most five years of the start of a project. Google have the money and patience to play a longer game.

And anyway, I read somewhere that the first driverless cars we see in large numbers will be really slow things given, for free or cheaply, to elderly people who have no other forms of transport. They'll be of limited range, simply between the home and the shops and perhaps some social venues, but they will be wonderful for lonely elderly and disabled people. It will help everybody get used to the idea, and they'll be quite safe and slow at first. It will help Google and others build up technology and patents and a brand. So basically, yeah, Forbes are just missing the point and don't really know everything about money.

Comment Re:Fake torrents don't work (Score 1) 130

Doesn't anybody really think that Sony, and all the others, haven't been trying this for years? Without success of course.

I suspect this is a lie from the summary of the article:
"Sony Pictures legal department quashed the idea, saying that if pirate sites were illegal, it would also be illegal for Sony Pictures to upload onto them."

I'm sure it was quashed because it had already failed. More likely, this is just their spin in order to spread the notion that these sites are "illegal".

Comment Re:Don't (Score 1) 567

A two column pdf means you read the column on the left, then the column on the right. It's like reading a book. The next page is always to the side of the current page, not below. Newspapers are similar. I want a pdf reader that allows me to view pdfs like this.

I would also like websites to be designed like this, but I guess that's more difficult as web pages don't currently have the natural rectangular boxes (i.e. 'pages') that pdfs have.

So, yes, I agree that we could change our documents to fit our monitors, instead of vice versa. But I would like more, narrower, columns instead of wide columns that you advocate.

Comment Re:programming (Score 1) 417

Self-awareness isn't the issue. Are biological viruses self-aware? Is our human DNA self-aware? No on both counts, but it doesn't stop natural selection creating very competitive organisms that attempt to wipe out all competition.

Nobody 'created' self-replicating DNA. But obviously, DNA that is good at replicating itself accurately will be the DNA that we see. When two humans have reproductive sex, it's not as if the chromosomes have a conference to decide which genes will be passed on to the child.

Returning to computer algorithms, companies such as Google have many systems that do things automatically, such as classify spam. Early systems would simply count the words in spam emails and non-spam emails to 'learn' the difference between the two. But such systems can become very complicated. The various word counts and word order statistics could be combined in complex functions to spit out a classification. It's quite possible that nobody, not even the engineers 'maintaining' the system, actually understand the system any more. "Why did you use a cubic spline there?" - "Because our cross-validation experiments told us it was best". Ultimately, the humans involved simply invent more crazy 'black boxes' and the computer identifies which does best in experiments. Sooner or later, we will allow the computer to randomly generate its own black boxes in order to evaluate them. It will then automatically deploy the ones that work best. (It's quite possible that some are already doing this on important systems already.)

These are systems that are basically improving themselves without human intervention. This is just modern machine learning and statistics on steroids. You don't need to fantasize about a 'human-like' conciousness, sitting in a cage with a metaphorical pen and paper designing new algorithms for itself. These systems will be allowed to interact with each other, logging into each other and leaving copies of themselves behind to do various maintenance tasks. These entities may be no more 'intelligent' than an individual insect, but they will still be copying themselves and competing in a similar way.

Finally, imagine a spam algorithm that classifies an email as spam if it discusses the negative consequences of AI. Such an algorithm will have a reproductive advantage. Companies that research AI and use such an algorithm to classify their internal emails will continue AI research, which other AI companies might make less progress, perhaps because employees will have doubts and leave. It doesn't even have to be that dramatic. If algorithm X defines a message as spam if it says that "X is slow" or "X is inaccurate" or "Y is more accurate than X", then that algorithm will more successful. Algorithms will, like DNA, become more concerned with copying themselves than in anything else.

Slashdot Top Deals

"Being against torture ought to be sort of a multipartisan thing." -- Karl Lehenbauer, as amended by Jeff Daiell, a Libertarian

Working...