Topic: questions about delete operators


Author: Masao Morita <m-morita@pdss7.trc.rwcp.or.jp>
Date: 1999/12/04
Raw View
Hi, I have same questions about delete operators.

The standard (ISO/IEC 14882:1998E) shows me following examples. (12.5p6)

  class X {  // (A) case
     // ...
     void operator delete(void*);
     void operator delete[](void*, size_t);
  };

  class Y {  // (B) case
     // ...
     void operator delete(void*, size_t);
     void operator delete[](void*);
  };

I think the following is appropriate for (A).

  int main() {
     X* xp = new X;
     X* xap = new X[10];
     // ...
     delete xp;  //  calls operator delete(xp)
     delete [] xap; // calls operator delete [] (xap,10)
  }

But, in (B) case the following is not good.

  int main() {
     Y* yp = new Y;
     Y* yap = new Y[10];
     // ...
     delete yp; // can not match, therefor ill-formed?
     delete [] yap; // can not match, therefor ill-formed?
  }

A programmer could call with the keyword 'operator' like below:

    Y::operator delete(yp,Any_size); // Any_size?
    Y::operator delete[](yap); // can not specify the size of the array

But I think it dose not make sense. Is there the other usage for (B)'s
operator?

I have one more question.
One translation unit:
  void foo(X*);
  int main() {
     X* xap = new X[10];
     foo(xap);
  }

Another translation unit:
  void foo(X* xap) {
     delete [] xap;
  };

How does the compiler know the size of the array?
Am I missing something?

Thanks in advance.

M. Morita


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]