🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c++
Article #3181

new T[0] and sizeof(T)

#3181
From: Marek MAKOWSKI
Date: Thu, 01 Feb 1996 15:49
57 lines
2795 bytes
I would like to ask for comments on three easy questions illustrated 
by the following piece of code:
 
template <class I, class T>
void mVect<I,T>::resize(I new_size) {
   T *old = v;
   v = new T[new_size];    // T *v is a private member of mVect  
   int size_of_elem = sizeof(T);    // <--- question 3 
   // 
   // do something 
   // 
   delete[] old;     // <--- questions 1 & 2 
} 
 
I have the following questions:
1. Is it absolutely robust and portable to delete[] old, even
   if a previous call was for new_size == 0 (or if v was allocated
   by the ctor for the size == 0) ?
	In other words: is it guaranteed that:
   (v = new T[0]) == 0;
2. Is it correct to assume that no destructor is called by this statement
	if old was set as: old = new T[0]  ??
3. Is there any risk involved in using sizeof(T) in this statement ?
 
I used to be sure that the answers are:
1. Yes
2. Yes
3. No
 
However, I have recently started to port an application developed
with SunPro (previously ported to Borland and checked with the
Bounds-Checker32).
The Watcom code blows-up on the delete[] statement (if a previous
call was for new_size == 0) and gives a warning whenever it sees the 
sizeof(T).

If the answer for quaestions 1 and 2 is negative then I would like to
know the reason why the standard does not require new to return 0
for zero_size array of objects.
This would work nicely with the delete guaranteed to be safe for
the 0 argument.
If there is a good reason for allowing new to return "anything" in such
situations then one should add to every ctor setting a ptr to 0
(which indeed made the Watcom version of my application running).
 
Thanks in advance for your time.
 
Marek
-- 
                  Marek Makowski  | Email: marek@iiasa.ac.at
         International Institute  | Phone: (+43-2236) 807.561
    for Applied Systems Analysis  | Fax:   (+43-2236) 71.313
       A-2361 Laxenburg, Austria  | Web: http://www.iiasa.ac.at
---
[ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy
  is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]
Technical Details
Message-ID: <1996Feb1.091641.4676@iiasa.ac.at>
Path: arc1-ca.pugleaf.net!in2.uu.net!bounce-back

Show Full Headers
Complete Article Headers
From: marek@iiasa.ac.at (Marek  MAKOWSKI)
Message-ID: <1996Feb1.091641.4676@iiasa.ac.at>
X-Original-Date: Thu, 1 Feb 1996 09:16:41 GMT
Path: arc1-ca.pugleaf.net!in2.uu.net!bounce-back
Date: 01 Feb 96 15:49:11 GMT
Approved: fjh@cs.mu.oz.au
Newsgroups: comp.std.c++
Subject: new T[0] and sizeof(T)
Organization: IIASA, Laxenburg, Austria
X-Newsreader: TIN [version 1.2 PL2]
Content-Type: text
Apparently-To: comp-std-c++@uunet.uu.net
X-Auth: PGPMoose V1.1 PGP comp.std.c++
 iQBFAgUBMRDhCOEDnX0m9pzZAQEoLgF9Fu912ML2kPk5zZDDDnuHrPJEPdnv9Aks
 LLJcx2xXJeCS94hjc9Zgnv5yHcZ6Whzh
 =unOH