Path: csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsreader4.netcologne.de!news.netcologne.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Ian Collins Newsgroups: comp.lang.c++ Subject: Re: Length of C++ arrays allocated by operator new[] Date: Mon, 22 Aug 2011 09:08:12 +1200 Lines: 46 Message-ID: <9bdadsFr0aU1@mid.individual.net> References: <4e4fb013$0$6568$9b4e6d93@newsspool3.arcor-online.net> <0YKdndDjrZg9f9LTnZ2dnUVZ8jSdnZ2d@giganews.com> <4e516d11$0$7610$9b4e6d93@newsspool1.arcor-online.net> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 8bit X-Trace: individual.net ZJIBx+aw4JiOZYK5qAuRFgaMsza8sUmdbC+B2OUxL3c+ZVa4ai Cancel-Lock: sha1:ULEXh0OclSjreEDFzCvsK28SoEs= User-Agent: Mozilla/5.0 (X11; U; SunOS i86pc; en-US; rv:1.9.2.9) Gecko/20101021 Lightning/1.0b2 Thunderbird/3.1.4 In-Reply-To: <4e516d11$0$7610$9b4e6d93@newsspool1.arcor-online.net> Xref: x330-a1.tempe.blueboxinc.net comp.lang.c++:9138 On 08/22/11 08:39 AM, Marcel Müller wrote: > On 20.08.11 18.36, Leigh Johnston wrote: >> On 20/08/2011 17:26, A wrote: >>>> SomeClass* array = new SomeClass[10]; >>>> >>>> size_t length = get_length_of_array(array); // Impossible >>> >>> any particular reason you don't want to use vector instead and then use >>> vect::size ? > > - The old platform does not support STL well. > - STL causes the executable size to explode. Only if you have a very poor linker. > One disadvantage of > template meta programming over generics. (Of course, there are many > advantages on the other side.) Who mentioned template meta-programming? >> Yes, well said. std::vector should be preferred over dynamic arrays for >> most cases; > > std::vector has the major disadvantage that any piece of code that has > write access to its elements can also change its size and, more > importantly, cause reallocations. That's sometimes not wanted and could > cause hard to find bugs with UB, especially if the array is shared > between threads. Then derived privately and only expose a subset of members. Or simply write your own array class. Either way, the "avoid naked arrays" rule of thumb is still valid. >> about the only case where one still needs to use dynamic >> arrays is if you want to allocate but not initialize a buffer. > > Why? vector will always initialize /less/ or same than operator new[]. > The allocation size of the vector may be larger than logical size, > containing additional uninitialized storage. new[] in contrast always > invokes the standard constructors (if any). So does vector's sized constructor. -- Ian Collins