Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!sun-barr!decwrl!infopiz!lupine!rfg From: rfg@NCD.COM (Ron Guilmette) Newsgroups: comp.lang.c++ Subject: Re: Smart pointers and stupid people (was: garbage collection...) Message-ID: <3779@lupine.NCD.COM> Date: 8 Feb 91 04:22:04 GMT References: <3344@lupine.NCD.COM> <4174@osc.COM> Distribution: comp Organization: Network Computing Devices, Inc., Mt. View, CA Lines: 57 In article tom@ssd.csd.harris.com (Tom Horsley) writes: +>>>>> Regarding Re: Smart pointers and stupid people (was: garbage collection...); tma@osc.COM (Tim Atkins) adds: + +tma> Now, if I call something that causes the object to move, then these +tma> stored away pointers are now invalid. It may seem to be a trivial point, but please keep in mind that C++ objects do not "move". You can copy the value of one C++ object into another, and then destroy the first object, but the objects themselves don't move. Still, if we modify the statement of the problem a bit, we can see that there is in fact still a potential problem here. If we change the word "move" to "be destroyed" then we will see that it would indeed be a problem if you had a sequence of actions like: create a (dumb) pointer value to some object invoke something which may cause the object to be destroyed use the (now stale) pointer That's definitely a problem. So what else is new? My suggestion for two minor language changes does not (and did not) attempt to make such cases impossible. My suggested language changes were aimed only at giving the programmer the ability to *encapsulate* such problems (arising from messed up uses of dumb pointers) into a limited area of the program so that when your program crashes, you will know where to start looking for the problem. No language can totally prevent you from writing code which contains errors. Some languages (and language rules) however do make it *harder* to write incorrect code and/or make it easier for you to find the bugs that you *do* create. I do not understand why people seem to want to insist that any minor change made to the language should instantly make it impossible to write code which has bugs. Sorry folks. I cannot propose any change which will make it impossible to write programs which have bugs. +This is the other key point. How do you make sure that 'this' is also a +"smart pointer" so the compiler will know it had its address taken and know +it is aliased? I claim that it is not important to do that. Remember that `this' can only be used within members functions of a given class. Thus, `this' is already *encapsulated* and thus any logical errors in its use are also encapsulated (within the class). +The only way (without language changes) seems to be following a coding +convention that will avoid ever having 'this' move. Remember, C++ objects don't move. -- // Ron Guilmette - C++ Entomologist // Internet: rfg@ncd.com uucp: ...uunet!lupine!rfg // Motto: If it sticks, force it. If it breaks, it needed replacing anyway.