Topic: operator new[](size_t, void*)': clarification sought


Author: tony@online.tmx.com.au (Tony Cook)
Date: 1996/09/02
Raw View
Scott Johnson (sj@aracnet.com) wrote:
: In article <4vbb09$fpo@news.BelWue.DE>,
: Dietmar Kuehl <dietmar.kuehl@uni-konstanz.de> wrote:
: >Hi,
: >
: >I have some trouble understanding the use of 'operator new[](size_t
: >size, void *ptr)' defined in lib.new.delete.placement: Is there any
: >portable use of this function? This function does nothing except
: >returning 'ptr', its second argument (according to
: >lib.new.delete.placement). This pointer is expected to point to a chunk
: >of memory containing 'size' (i.e. the value of the first argument)
: >bytes (this follows from expr.new paragraph 13). So far, so good... If
: >I know 'size' in advance, I can allocate enough memory. But here comes
: >the problem: There is no way to know 'size' in advance!  This results
: >from 'expr.new', paragraph 12.  There the following is explicitly found
: >(excerpt):

: [snip]

: That's because "placement new" is not to be used for memory allocation,
: despite the same name as normal new.

He's not talking about
 operator new(size_t, void *)
but
 operator new[](size_t, void *)

which, as he points out, has very different problems on it's use.

--
        Tony Cook - tony@online.tmx.com.au
                    100237.3425@compuserve.com


[ 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         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: sj@aracnet.com (Scott Johnson)
Date: 1996/08/24
Raw View
In article <4vbb09$fpo@news.BelWue.DE>,
Dietmar Kuehl <dietmar.kuehl@uni-konstanz.de> wrote:
>Hi,
>
>I have some trouble understanding the use of 'operator new[](size_t
>size, void *ptr)' defined in lib.new.delete.placement: Is there any
>portable use of this function? This function does nothing except
>returning 'ptr', its second argument (according to
>lib.new.delete.placement). This pointer is expected to point to a chunk
>of memory containing 'size' (i.e. the value of the first argument)
>bytes (this follows from expr.new paragraph 13). So far, so good... If
>I know 'size' in advance, I can allocate enough memory. But here comes
>the problem: There is no way to know 'size' in advance!  This results
>from 'expr.new', paragraph 12.  There the following is explicitly found
>(excerpt):

[snip]

That's because "placement new" is not to be used for memory allocation,
despite the same name as normal new.

Placement new exists so you can construct an object at an address you
specify--it is assumed that the memory is already there for your use.
Without placement new, you cannot construct an object in previously
allocated space.  (The user, remember, may NOT call an object's
constructor directly; in addition, there is no way to tell the constructor
where to construct the object.)

Placement new solves both of these problems.


Scott

--
/--------------------------------------------------------------------------\
|Scott Johnson -- Professional (sometimes) SW Engineer and all-purpose Geek|
|I don't speak for nobody but myself, which everyone else is thankful for  |
\--------------------------------------------------------------------------/
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]





Author: kuehl@uzwil.informatik.uni-konstanz.de (Dietmar Kuehl)
Date: 1996/08/20
Raw View
Hi,

I have some trouble understanding the use of 'operator new[](size_t
size, void *ptr)' defined in lib.new.delete.placement: Is there any
portable use of this function? This function does nothing except
returning 'ptr', its second argument (according to
lib.new.delete.placement). This pointer is expected to point to a chunk
of memory containing 'size' (i.e. the value of the first argument)
bytes (this follows from expr.new paragraph 13). So far, so good... If
I know 'size' in advance, I can allocate enough memory. But here comes
the problem: There is no way to know 'size' in advance!  This results
from 'expr.new', paragraph 12.  There the following is explicitly found
(excerpt):

  --new(2,f) T[5] results in a call of
    operator new[](sizeof(T)*5+y,2,f).

    Here,  x  and y are non-negative, implementation-defined values repre
    senting array allocation overhead.  Their value might  vary  from  one
    invocation of new to another.

'y' may vary from one invocation to another! There is also no upper
bound, say 'overhead', on 'y' such that the following would be
portable:

  void *ptr = operator new(sizeof(T) * 5 + overhead);
  new(ptr) T[5];

Thus, the call to 'operator new[](size_t, void*)' is always dangerous
and, besides, will never have the effect that the array of objects is
placed at the address pointed to by 'ptr' in the above example.

Is there a mistake in my argumentation? If not, the specification of
'operator new[](size_t, void*)' or of the new-expression 'new(ptr)
T[s]' is, IMO, broken...

Thank you very much for any clarification,
  Dietmar Kuehl
--
<mailto:dietmar.kuehl@uni-konstanz.de>
<http://www.informatik.uni-konstanz.de/~kuehl/>
I am a realistic optimist - that's why I appear to be slightly pessimistic
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]