Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!cornell!uw-beaver!apollo!mrst!sdti!turner From: turner@sdti.SDTI.COM (Prescott K. Turner) Newsgroups: comp.lang.c++ Subject: Re: Isn't sizeof an operator? Summary: Why not? Message-ID: <374@sdti.SDTI.COM> Date: 21 Mar 89 15:50:00 GMT References: <123@riunite.ACA.MCC.COM> <2352@buengc.BU.EDU> Reply-To: turner@sdti.UUCP (0006-Prescott K. Turner, Jr.) Organization: Software Development Technologies, Sudbury MA Lines: 35 In article <2352@buengc.BU.EDU> bph@buengc.bu.edu (Blair P. Houghton) writes: >In article <123@riunite.ACA.MCC.COM> rfg@riunite.UUCP (Ron Guilmette) writes: >["why isn't sizeof declarable as an operator?"] > >Because it's a compiler directive, not a C-language operator. What difference does that make? Note that "sizeof" is in the table of operators in "The C++ Programming Language". >The result of sizeof (something) is an integer _constant_. That was right for C. >If you try to take the size of something that has a varying size, >you will get only the size of that thing at compile time. If "base" is a class with virtual functions, then a pointer declared like base * base_object_pointer; provides the behavior of the derived class. For *base_object_pointer, "the size of that thing" is not available "at compile time", so the compiler's result can be plain wrong. If the program does pointer arithmetic with base_object_pointer, cfront assumes that the declared type of the array elements is "base" -- an assumption which is dangerous most of the time. >If you're creating varying-sized data elements, you should keep track of >their sizes yourself. This is not satisfactory because sizeof and pointer arithmetic are in the language and are considerably simpler to use than known substitutes -- and there's no way to make them work right when you need it. -- Prescott K. Turner, Jr. Software Development Technologies, Inc. 375 Dutton Rd., Sudbury, MA 01776 USA (508) 443-5779 UUCP: ...{harvard,mit-eddie}!sdti!turner Internet: turner@sdti.sdti.com