On Friday, January 30, 2015 at 10:39:12 PM UTC+5:30, Michael Torrie wrote: > On 01/30/2015 09:27 AM, Rustom Mody wrote: > > ... if I restate that in other words it says that sufficiently > > complex data structures will be beyond the reach of the standard > > RAII infrastructure. > > > > Of course this only brings up one side of memory-mgmt problems > > viz. unreclaimable memory. > > > > What about dangling pointers? > > C++ apps are prone to segfault. Seems to suggest > > (to me at least) that the memory-management infrastructure > > is not right. > > > > Stroustrup talks of the fact that C++ is suitable for lightweight > > abstractions. In view of the segfault-proneness I'd say they > > are rather leaky abstractions. > > > > But as I said at the outset I dont understand C++ > > Yes I can tell you haven't used C++. Compared to C, I've always found > memory management in C++ to be quite a lot easier. The main reason is > that C++ guarantees objects will be destroyed when going out of scope.
I hear you and I trust you as a gentleman but I dont trust C++ :-) The only time in some near 15 years of python use that I got it to segfault was when Ranting Rick gave some wx code to try [at that time he was in rant-against-tk mode] Sure enough it was related to the fact that wx is written in C++ and some expectations were not being followed. > So when designing a class, you put any allocation routines in the > constructor, and put deallocation routines in the destructor. And it > just works. This is something I miss in other languages, even Python. > > And for many things, though it's not quite as efficient, when dealing > with objects you can forgo pointers altogether and just use copy > constructors, instead of the > > blah *a = new blah_with_label("hello") //allocate on heap > //forget to "delete a" and it leaks the heap *and* anything > //that class blah allocated on construction. > > just simply declare objects directly and use them: > > blah a("hello") //assuming there's a constructor that takes a string > //deletes everything when it goes out of scope > > So for the lightweight abstractions Stroustrup talks about, this works > very well. And you'll rarely have a memory leak (only in the class > itself) and no "dangling pointers." And what about the grey area between lightweight and heavyweight? You say just use copy constructors and no pointers. Can you (ie C++) guarantee that no pointer is ever copied out of scope of these copy-constructed objects? > > For other things, though, you have to dynamically create objects. But > the C++ reference-counting smart pointers offer much of the same > destruction semantics as using static objects. It's really a slick > system. Almost makes memory management a non-issue. Circular > references will still leak (just like they do on Python). But it > certainly makes life a lot more pleasant than in C from a memory > management perspective. -- https://mail.python.org/mailman/listinfo/python-list