🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: new T[0] and sizeof(T)

#3191
From: Steve Clamage
Date: Fri, 02 Feb 1996 01:38
62 lines
3298 bytes
In article 4676@iiasa.ac.at, marek@iiasa.ac.at (Marek  MAKOWSKI) writes:
>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) ?

If you allocate an object with new[], you delete it with delete[]. In
addition, it is always safe to delete a null pointer.

>	In other words: is it guaranteed that:
>   (v = new T[0]) == 0;

No. The result of 'new' is never a null pointer (unless the allocation fails
and you are using a pre-exception or "nothrow" version of new). A request of
zero size results in a pointer value distinct from all other pointer values
in the program. So if you write
	T* t1 = new T[count];
	T* t2 = new T[count];
and if the allocations succeed, it must be true that
	( t1 != 0 && t1 != t2 && t2 != 0 )
even if count is zero. The expression "new T[0]" is invalid, but if you
use a non-const expression for the count, it is OK if the value is zero.

Since you have requested zero objects, you cannot dereference the resulting
pointer -- it might not point at usable storage. But since you acquired the
pointer value from "new" you can (and should) pass it to "delete".

>2. Is it correct to assume that no destructor is called by this statement
>	if old was set as: old = new T[0]  ??

As noted above, "new T[0]" isn't valid, but if you ask for zero objects,
no objects are created. When you delete the array, no destructors
should be called. I can believe that a compiler might get some part of
these requirements wrong, since this sort of thing hardly ever happens and
bugs might slip through testing.

>3. Is there any risk involved in using sizeof(T) in this statement ?

If T is a completely-defined type, you can always ask for sizeof(T); its
value is a compile-time constant. I'm afraid I don't understand this
question.

---
Steve Clamage, stephen.clamage@eng.sun.com
---
[ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy is
  in http://reality.sgi.com/employees/austern_mti/std-c++/policy.html. ]
Technical Details
Message-ID: <4erovm$4hh@engnews1.Eng.Sun.COM>
Path: arc1-ca.pugleaf.net!fido.asd.sgi.com!austern
References: <1996Feb1.091641.4676@iiasa.ac.at>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!fido.asd.sgi.com!austern
From: clamage@Eng.Sun.COM (Steve Clamage)
Newsgroups: comp.std.c++
Subject: Re: new T[0] and sizeof(T)
Date: 01 Feb 1996 17:38:53 PST
Organization: Sun Microsystems Inc.
Approved: austern@isolde.mti.sgi.com
Message-ID: <4erovm$4hh@engnews1.Eng.Sun.COM>
References: <1996Feb1.091641.4676@iiasa.ac.at>
Reply-To: clamage@Eng.Sun.COM
NNTP-Posting-Host: isolde.mti.sgi.com
X-Original-Date: 2 Feb 1996 01:21:58 GMT
X-Auth: PGPMoose V1.1 PGP comp.std.c++
 iQBVAwUBMRFrSky4NqrwXLNJAQGaUAH/UWFgkuOpuUIHGy2zhqatGNaazlVKADMt
 xS6Va48FFyp/DExHx7Bz+dnLdeF3kl81bpBnmuPvpswydFuQf/IdMg==
 =+3E+
Originator: austern@isolde.mti.sgi.com