Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!samsung!brutus.cs.uiuc.edu!lll-winken!ames!pacbell!att!cbnewsc!lgm From: lgm@cbnewsc.ATT.COM (lawrence.g.mayka) Newsgroups: comp.lang.misc Subject: Re: Relationship between C and C++ Message-ID: <14539@cbnewsc.ATT.COM> Date: 21 Mar 90 02:25:11 GMT References: <5200048@m.cs.uiuc.edu> Reply-To: lgm@cbnewsc.ATT.COM (lawrence.g.mayka,ihp,) Organization: AT&T Bell Laboratories Lines: 55 In article <5200048@m.cs.uiuc.edu> robison@m.cs.uiuc.edu writes: >Most C implementations do not do bounds checking on pointers, >but I fail to see why pointer arithmetic is inherently evil. My own objection to pointer arithmetic in a purportedly object-oriented language such as C++ is that pointer arithmetic assumes that the storage cells actually referenced by the pointer at run time have the precise size denoted by the pointer's compile-time declaration. Thus, if I construct an array of objects of class Derived (derived from Base), and pass that array as argument to a function declared to accept a pointer to a Base - a perfectly legal and automatic coercion - and the function attempts to index into the array/off the pointer, the result is random garbage. If Derived has virtual functions, I might even overwrite a pointer to its virtual function table. The next virtual function call then gives "Memory fault, core dumped." An attempt to delete the array of Derived through a Base pointer can cause less debuggable havoc - repeatedly calling the Base destructor with incorrect pointers - and this is even if one remembers to specify the size of the array (which the requestor of the deletion is expected to know, but whose omission is quite legal and prevents the necessary calling of the destructor on each array element). In short, the C array is a poor substitute for an indexable collection type, and the C pointer is a poor iterator for such a type. >Can anyone clue me as to the basis for pointer paranoia? Programming languages such as Common Lisp and Smalltalk support an abstraction of storage (memory) itself, in which memory consists exclusively of typed objects, and those objects are referenced by names (bindings). Assignment is essentially a rebinding; physical copying is a rather unusual operation, performed only when one specifically wishes to preserve a mutable object's current state in the face of possible future alteration by others. Pointers vitiate this abstraction by encouraging the more primitive view of memory as merely an enormous array of untyped machine words, to be used or abused in whatever way the programmer desires. This loss of abstraction is particularly acute when pointer casting is permitted and encouraged, and especially when pointers are often coerced automatically and silently by the compiler. Many languages outright encourage a pointer implementation in which an integer or other datum can have the same bit pattern as a pointer. This almost completely rules out truly reliable garbage collection, further invalidating the objects/bindings abstraction. Lawrence G. Mayka AT&T Bell Laboratories lgm@ihlpf.att.com Standard disclaimer.