Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!uunet!cimshop!davidm From: cimshop!davidm@uunet.UU.NET (David S. Masterson) Newsgroups: comp.lang.c++ Subject: Re: delete [] Message-ID: Date: 3 Aug 90 03:57:38 GMT References: <641@atcmpe.atcmp.nl> <1990Jul27.231220.8396@cerc.utexas.edu> <4937@pegasus.ATT.COM> <4942@pegasus.ATT.COM> Sender: davidm@cimshop.UUCP Distribution: comp Organization: Consilium Inc., Mountain View, California. Lines: 87 In-reply-to: hansen@pegasus.ATT.COM's message of 31 Jul 90 15:51:59 GMT Whoa! What a reply (certainly made things a lot more clearer for me). Of course, my question was not meant to argue language definition (you're far more versed in it than I ;-). On the contrary, my question was more of a should it be this way. I think you got both sides of it, though, which was good. :-) However, I have a few more questions... In article <4942@pegasus.ATT.COM> hansen@pegasus.ATT.COM (Tony L. Hansen) writes: >Let's start at the beginning and work forward. > > "X *x = new X" means to allocate an object of type X, using either > operator X::new() or the global operator ::new(), invoke the appropriate > constructor for that object, and assign a pointer to it to the pointer > x. > > "X *x = new X[m]" means to allocate an array of objects of type X, using > only the global operator ::new(), invoke the appropriate constructor for > x[0] through x[m-1], and assign a pointer to the array to the pointer x. > The 1990 definition of C++ also mandates that the C++ environment > remember the value m associated with that pointer. Hmmmm. If someone had developed X for an embedded system such that X.new() allocates space for X in a special memory area, then X could never "validly" be used in an array form because "new X[m]" would cause it to be allocated in standard memory. Does anyone see a problem with this? > "delete x" means to take the single object (of type X) that x points to > and invoke ~X() followed by either operator X::delete() or the global > operator ::delete(). > > "delete [m] x" means to take the pointer that x points to, treat it as > an array, invoke ~X() on x[0] through x[m-1], and then invoke the global > operator ::delete(). This syntax is part of all versions of C++ from > 1985 through 1990. As of 1990, the syntax is deprecated, meaning that it > will go away at some point. > > "delete [] x" means to take the pointer that x points to, treat it as an > array, invoke ~X() on x[0] through x[m-1], where m is the number that was > saved away by operator ::new when the array was allocated, and then > invoke the global operator ::delete(). This syntax is part of the 1990 > definition of C++. Some compilers, in particular AT&T 2.1 cfront, > support this syntax. The 2.1 cfront ignores any value passed within the > [], just giving a warning message indicating that it is doing so. > Note that the compiler is only supposed to look up the number if it is > told that the pointer is pointing to an array by using the [] in > "delete[] x". It does not look up the number for "delete x". Here too, if X only points to special memory, then everything is fine, but if X was supposed to totally reside in a special area, then X could not be allocated as an array. Does anyone see a problem with this? In both cases, it may be a non-problem. I guess its just a quirk of the language and people program around it by never writing their classes such that they are supposed to wholly reside in special areas but rather point to pieces that are special. Programmers may even be doing this without realizing the other side of the coin (thus its a non-problem). By the way, it is true that the delete() operator (global or class specific) is called automatically after the destructor, right? (I'm still working with a V1.2 compiler) By this, I mean that there isn't a need to have the destructor call delete()? (Your later example confused me slightly on this.) ><<< I don't know what delete [] m means. ><< As I said, your reference is outdated. The latest C++ rules say that the ><< number within the [] is unnecessary and the environment is required to ><< keep track of how many elements are in the array. >< Isn't this a contradiction of the previous statement? If the environment >< is keeping track of how many elements are in the array, then doesn't it >< know that the variable names an array? > >Perhaps it could, or even should, but the answer is no, the environment is >only required to be queried for the size of the array when the objects are >deleted via the "delete[]x" syntax, not for the "delete x" syntax. Is this "size of the array" in the compiler's environment (in other words, determined at compile time) or in the run-time environment (in other words, somehow tacked onto the memory that is allocated)? There must be plusses and minusses with either way. -- =================================================================== David Masterson Consilium, Inc. uunet!cimshop!davidm Mt. View, CA 94043 =================================================================== "If someone thinks they know what I said, then I didn't say it!"