Topic: Ok this compiles but is it std C++


Author: hendrik@vedge.com (Hendrik Boom)
Date: Tue, 31 Aug 1993 15:06:11 GMT
Raw View
alan@saturn.cs.swin.oz.au (Alan Christiansen) writes:
: The code shown below compiles with a BC3.1 compiler. The compiled
: code actually does what I think it should do. I have read a disassembly
: of it.
:
: weird 1.
: Use of const inside a class to indicate that a variable can only
: be intialised by the constructor. Is this standard c++ or a quirk of
: BC3.1.

I believe this is standard.
:
: weird 2.
: N types of new operator.
:  My manual (the FM) says that
: Programmers Guide, P 137, top of page;
:  "A user-defined operator NEW must return a VOID * and must have a
: SIZE_T as its first argument. A user-defined operator DELETE must have
: a VOID return type and VOID* as its first argument; a second argument
: of type SIZE_T is optional. For example, .... "
: (CAPITALS were bold in th oiginal.)

A 'new' operator can have more arguments; these match the arguments
provided in the 'placement' syntax in the constructor.
These arguments can be used, for example, to determine what kind
of storage the new object will need.  This is standard.

As far as I know, no such extra arguments are allowed in the delete syntax.
If Borland allows them, they are nonstandard.
Presumably, if you need to know what kind of storage or such is being
freed, you will have to make a note in the object itself.









--
-------------------------------------------------------
Try one or more of the following addresses to reply.
at work: hendrik@vedge.com,  iros1!vedge!hendrik
at home: uunet!ozrout!topoi!hendrik




Author: alan@saturn.cs.swin.oz.au (Alan Christiansen)
Date: 28 Aug 93 10:06:33 GMT
Raw View
The code shown below compiles with a BC3.1 compiler. The compiled
code actually does what I think it should do. I have read a disassembly
of it.

weird 1.
Use of const inside a class to indicate that a variable can only
be intialised by the constructor. Is this standard c++ or a quirk of
BC3.1.

weird 2.
N types of new operator.
 My manual (the FM) says that
Programmers Guide, P 137, top of page;
 "A user-defined operator NEW must return a VOID * and must have a
SIZE_T as its first argument. A user-defined operator DELETE must have
a VOID return type and VOID* as its first argument; a second argument
of type SIZE_T is optional. For example, .... "
(CAPITALS were bold in th oiginal.)

The way I read this delete can have an optional second argument
but it is new that can have an optional second argument.
The first sentence however talks about new's "first" argument
thus implying there migh be more. I have not managed to get
the compiler to accept any delete operator declarations with
extra arguments.
I can find no other reference or any example code of new accepting
two arguments....  except.  (which is where I started.)

Borland use this type of new (2 args) in their MemManger for their
classlibrary. They use the extra parameter to specify extra space
much as I did below with malloc(N1+N2). Having found the manual
less than complete I tried the Penguin code and found it compiled and ran
also.

What gives. Is the 2 arg new a kludge of C++. IS the penguin new
a hole created by allowing new to be an operator with two
size_t args.
I can see the penguin new being used ala

Sheep *pS = new(Heap1) Sheep(65);
Sheep *pS = new(Heap2) Sheep(665);
ie I want new space but on which heap !
Note HeapN is passed as the last value perhaps it can have a default
value.

Alan
PS  I think but cant clearly remeber tha BC even takes 3 args.

:----SNip........Compiled error free BC3.1 code .........

//weird 1
class A {

 public:
 A(int Z): Y(Z) { }

 const int Y;

};

//weird 2

class Penguin {     //pointless demo class
 public:
 Penguin() :F(0) {}
 int F;
 void SetVal(int G) {F=G;}
 // ....
 };

#include <stdlib.h>

class WeirdNew {
 public:

 void * operator new (size_t N, Penguin &P);
 void * operator new (size_t N1, size_t N2);
 void * operator new (size_t N);

 float W[3];
};
  // pointless demo funcs
 void * WeirdNew::operator new (size_t N, Penguin &P)
  { //... Whatever
    P.SetVal(N);
    return malloc(N);
  }
 void * WeirdNew::operator new (size_t N1, size_t N2)
  { //... Whatever
    return malloc(N1+N2);
  }
 void * WeirdNew::operator new (size_t N)
  { //... Whatever
    return malloc(N);
  }
/*  Compiles Zero Errors Zero Warnings.
*/
#include <stdio.h>
int main()
{
  Penguin penguin;
  WeirdNew *p1,*p2,*p3;

  p1 = new WeirdNew;          // this is normal
  p2 = new( 4 ) WeirdNew;     // this is strange is it standard

  p3 = new(penguin) WeirdNew; // this is WEIRD.

  printf(" %p %p %p ",p1,p2,p3); // keep the optimisers mits off my vars
     // keep zero warnings
  printf(" %d \n",penguin.F);
  return 0;
}
--
Everyone at this site (including me) has my name in their kill file so
I guess I dont speak for anyone.
I guess that that is fair enough no one speaks for me either.
Mail address   alan@saturn.cs.swin.oz.au