Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > What is this feature

Reply
Thread Tools

What is this feature

 
 
parag_paul@hotmail.com
Guest
Posts: n/a
 
      02-27-2008
hi All,
I just saw some code that
looks like this


(void*) new (h) className ( arg1, arg2 );
all by itself. Is this an accepted syntax. I have never seen something
similar to this.
If yes, how does it work?
-Parag
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      02-27-2008
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> I just saw some code that
> looks like this
>
>
> (void*) new (h) className ( arg1, arg2 );
> all by itself. Is this an accepted syntax. I have never seen something
> similar to this.
> If yes, how does it work?


Without knowing what 'h' is, it's hard to go into details, but

It calls

className:perator new( size_t, decltype(h) )

, from which it receives a pointer to some memory in which it then
constructs an instance of 'className' using the constructor that
accepts the two arguments. Since the entire expression returns
a pointer to the newly created object, the code seems to discard it
(no assignment anywhere). The cast to 'void*' is extraneous, AFAICT.

Of course, questions of the nature "what does that code do" are
better addressed to those who wrote the code.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
 
 
 
dp1978x@gmail.com
Guest
Posts: n/a
 
      02-27-2008
On Feb 27, 3:32 pm, "Victor Bazarov" <(E-Mail Removed)> wrote:
> (E-Mail Removed) wrote:
> > I just saw some code that
> > looks like this

>
> > (void*) new (h) className ( arg1, arg2 );
> > all by itself. Is this an accepted syntax. I have never seen something
> > similar to this.
> > If yes, how does it work?

>
> Without knowing what 'h' is, it's hard to go into details, but
>
> It calls
>
> className:perator new( size_t, decltype(h) )
>
> , from which it receives a pointer to some memory in which it then
> constructs an instance of 'className' using the constructor that
> accepts the two arguments. Since the entire expression returns
> a pointer to the newly created object, the code seems to discard it
> (no assignment anywhere). The cast to 'void*' is extraneous, AFAICT.
>
> Of course, questions of the nature "what does that code do" are
> better addressed to those who wrote the code.
>


In other words it constructs an object within an existing buffer "h"
instead of allocating a new one.

D.
 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      02-27-2008
(E-Mail Removed) wrote:
> On Feb 27, 3:32 pm, "Victor Bazarov" <(E-Mail Removed)> wrote:
>> (E-Mail Removed) wrote:
>>> I just saw some code that
>>> looks like this

>>
>>> (void*) new (h) className ( arg1, arg2 );
>>> all by itself. Is this an accepted syntax. I have never seen
>>> something similar to this.
>>> If yes, how does it work?

>>
>> Without knowing what 'h' is, it's hard to go into details, but
>>
>> It calls
>>
>> className:perator new( size_t, decltype(h) )
>>
>> , from which it receives a pointer to some memory in which it then
>> constructs an instance of 'className' using the constructor that
>> accepts the two arguments. Since the entire expression returns
>> a pointer to the newly created object, the code seems to discard it
>> (no assignment anywhere). The cast to 'void*' is extraneous, AFAICT.
>>
>> Of course, questions of the nature "what does that code do" are
>> better addressed to those who wrote the code.
>>

>
> In other words it constructs an object within an existing buffer "h"
> instead of allocating a new one.


*That* cannot be asserted without knowing what 'h' is. Yes, it uses
the syntax the placement new uses. But if 'h' is an integer or some
other type, all that can be concluded is that a special form of 'new'
is invoked, the one that takes an extra argument. Hell, if 'h' is
a macro, then who know what the expression can contain after the
preprocessor is done with it...

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
peter koch
Guest
Posts: n/a
 
      02-27-2008
On 27 Feb., 21:53, (E-Mail Removed) wrote:
> On Feb 27, 3:32 pm, "Victor Bazarov" <(E-Mail Removed)> wrote:
>
>
>
>
>
> > (E-Mail Removed) wrote:
> > > I just saw some code that
> > > looks like this

>
> > > (void*) new (h) className ( arg1, arg2 );
> > > all by itself. Is this an accepted syntax. I have never seen something
> > > similar to this.
> > > If yes, how does it work?

>
> > Without knowing what 'h' is, it's hard to go into details, but

>
> > It calls

>
> > * * className:perator new( size_t, decltype(h) )

>
> > , from which it receives a pointer to some memory in which it then
> > constructs an instance of 'className' using the constructor that
> > accepts the two arguments. *Since the entire expression returns
> > a pointer to the newly created object, the code seems to discard it
> > (no assignment anywhere). *The cast to 'void*' is extraneous, AFAICT.

>
> > Of course, questions of the nature "what does that code do" are
> > better addressed to those who wrote the code.

>
> In other words it constructs an object within an existing buffer "h"
> instead of allocating a new one.
>
> D

Maybe! We do not know what h is, so that is one possibility. Another
one is that the memory is allocated from some special arena.

/Peter
 
Reply With Quote
 
parag_paul@hotmail.com
Guest
Posts: n/a
 
      02-27-2008
On Feb 28, 2:01 am, "Victor Bazarov" <(E-Mail Removed)> wrote:
> (E-Mail Removed) wrote:
> > On Feb 27, 3:32 pm, "Victor Bazarov" <(E-Mail Removed)> wrote:
> >> (E-Mail Removed) wrote:
> >>> I just saw some code that
> >>> looks like this

>
> >>> (void*) new (h) className ( arg1, arg2 );
> >>> all by itself. Is this an accepted syntax. I have never seen
> >>> something similar to this.
> >>> If yes, how does it work?

>
> >> Without knowing what 'h' is, it's hard to go into details, but

>
> >> It calls

>
> >> className:perator new( size_t, decltype(h) )

>
> >> , from which it receives a pointer to some memory in which it then
> >> constructs an instance of 'className' using the constructor that
> >> accepts the two arguments. Since the entire expression returns
> >> a pointer to the newly created object, the code seems to discard it
> >> (no assignment anywhere). The cast to 'void*' is extraneous, AFAICT.

>
> >> Of course, questions of the nature "what does that code do" are
> >> better addressed to those who wrote the code.

>
> > In other words it constructs an object within an existing buffer "h"
> > instead of allocating a new one.

>
> *That* cannot be asserted without knowing what 'h' is. Yes, it uses
> the syntax the placement new uses. But if 'h' is an integer or some
> other type, all that can be concluded is that a special form of 'new'
> is invoked, the one that takes an extra argument. Hell, if 'h' is
> a macro, then who know what the expression can contain after the
> preprocessor is done with it...
>
> V
> --
> Please remove capital 'A's when replying by e-mail
> I do not respond to top-posted replies, please don't ask


Sorry to have been so brief earlier.
The 'h' in the discussion is actually
something like the following

className h ;

(void*) new (h) className ( arg1, arg2 );
 
Reply With Quote
 
dp1978x@gmail.com
Guest
Posts: n/a
 
      02-27-2008
On Feb 27, 4:41 pm, "(E-Mail Removed)" <(E-Mail Removed)>
wrote:
> On Feb 28, 2:01 am, "Victor Bazarov" <(E-Mail Removed)> wrote:
>
>
>
> > (E-Mail Removed) wrote:
> > > On Feb 27, 3:32 pm, "Victor Bazarov" <(E-Mail Removed)> wrote:
> > >> (E-Mail Removed) wrote:
> > >>> I just saw some code that
> > >>> looks like this

>
> > >>> (void*) new (h) className ( arg1, arg2 );
> > >>> all by itself. Is this an accepted syntax. I have never seen
> > >>> something similar to this.
> > >>> If yes, how does it work?

>
> > >> Without knowing what 'h' is, it's hard to go into details, but

>
> > >> It calls

>
> > >> className:perator new( size_t, decltype(h) )

>
> > >> , from which it receives a pointer to some memory in which it then
> > >> constructs an instance of 'className' using the constructor that
> > >> accepts the two arguments. Since the entire expression returns
> > >> a pointer to the newly created object, the code seems to discard it
> > >> (no assignment anywhere). The cast to 'void*' is extraneous, AFAICT.

>
> > >> Of course, questions of the nature "what does that code do" are
> > >> better addressed to those who wrote the code.

>
> > > In other words it constructs an object within an existing buffer "h"
> > > instead of allocating a new one.

>
> > *That* cannot be asserted without knowing what 'h' is. Yes, it uses
> > the syntax the placement new uses. But if 'h' is an integer or some
> > other type, all that can be concluded is that a special form of 'new'
> > is invoked, the one that takes an extra argument. Hell, if 'h' is
> > a macro, then who know what the expression can contain after the
> > preprocessor is done with it...

>
> > V
> > --
> > Please remove capital 'A's when replying by e-mail
> > I do not respond to top-posted replies, please don't ask

>
> Sorry to have been so brief earlier.
> The 'h' in the discussion is actually
> something like the following
>
> className h ;
>
> (void*) new (h) className ( arg1, arg2 );


Is the destructor called explicitely somewhere before "new"? If not
it's likely a bug.
 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      02-27-2008
(E-Mail Removed) wrote:
> On Feb 27, 4:41 pm, "(E-Mail Removed)" <(E-Mail Removed)>
> wrote:
>> On Feb 28, 2:01 am, "Victor Bazarov" <(E-Mail Removed)> wrote:
>>
>>
>>
>>> (E-Mail Removed) wrote:
>>>> On Feb 27, 3:32 pm, "Victor Bazarov" <(E-Mail Removed)>
>>>> wrote:
>>>>> (E-Mail Removed) wrote:
>>>>>> I just saw some code that
>>>>>> looks like this

>>
>>>>>> (void*) new (h) className ( arg1, arg2 );
>>>>>> all by itself. Is this an accepted syntax. I have never seen
>>>>>> something similar to this.
>>>>>> If yes, how does it work?

>>
>>>>> Without knowing what 'h' is, it's hard to go into details, but

>>
>>>>> It calls

>>
>>>>> className:perator new( size_t, decltype(h) )

>>
>>>>> , from which it receives a pointer to some memory in which it then
>>>>> constructs an instance of 'className' using the constructor that
>>>>> accepts the two arguments. Since the entire expression returns
>>>>> a pointer to the newly created object, the code seems to discard
>>>>> it (no assignment anywhere). The cast to 'void*' is extraneous,
>>>>> AFAICT.

>>
>>>>> Of course, questions of the nature "what does that code do" are
>>>>> better addressed to those who wrote the code.

>>
>>>> In other words it constructs an object within an existing buffer
>>>> "h" instead of allocating a new one.

>>
>>> *That* cannot be asserted without knowing what 'h' is. Yes, it uses
>>> the syntax the placement new uses. But if 'h' is an integer or some
>>> other type, all that can be concluded is that a special form of
>>> 'new' is invoked, the one that takes an extra argument. Hell, if
>>> 'h' is
>>> a macro, then who know what the expression can contain after the
>>> preprocessor is done with it...

>>
>>> V
>>> --
>>> Please remove capital 'A's when replying by e-mail
>>> I do not respond to top-posted replies, please don't ask

>>
>> Sorry to have been so brief earlier.
>> The 'h' in the discussion is actually
>> something like the following
>>
>> className h ;
>>
>> (void*) new (h) className ( arg1, arg2 );

>
> Is the destructor called explicitely somewhere before "new"? If not
> it's likely a bug.


Again, you're rushing into some conclusions without any basis, or
at least it would seem so. The code does not have the address of
'h' in the 'new' expression. If it did, we could claim in-place
re-initialisation of sorts, like this:

className h;
new (&h) className(arg1, arg2);

which, of course, as you point out is not a good idea, the object
at the location '&h' is constructed twice (and will most likely
be destructed only once).

But it's not the case. Apparently, the OP can't get his ducks in
a row and post the complete piece of code from which something
could be concluded. Until then, my guess is as good as yours.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
Krice
Guest
Posts: n/a
 
      02-27-2008
On 27 helmi, 22:23, "(E-Mail Removed)" <(E-Mail Removed)>
wrote:
> Is this an accepted syntax.


No it's not. I wont accept it.
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      02-28-2008
On Feb 27, 10:41 pm, peter koch <(E-Mail Removed)> wrote:
> On 27 Feb., 21:53, (E-Mail Removed) wrote:
> > On Feb 27, 3:32 pm, "Victor Bazarov" <(E-Mail Removed)> wrote:


> > > (E-Mail Removed) wrote:
> > > > I just saw some code that
> > > > looks like this


> > > > (void*) new (h) className ( arg1, arg2 );
> > > > all by itself. Is this an accepted syntax. I have never seen something
> > > > similar to this.
> > > > If yes, how does it work?


For reference, it's (miss-)called "placement new". Be careful,
though, as people (including the standard) use this expression
with two different meanings: either for any new expression with
this syntax, and the operator new function it calls, or for the
particular instance of this syntax where the argument (here, h)
is a void*, or can be converted into a void*.

> > > Without knowing what 'h' is, it's hard to go into details, but


> > > It calls


> > > className:perator new( size_t, decltype(h) )


> > > , from which it receives a pointer to some memory in which it then
> > > constructs an instance of 'className' using the constructor that
> > > accepts the two arguments. Since the entire expression returns
> > > a pointer to the newly created object, the code seems to discard it
> > > (no assignment anywhere). The cast to 'void*' is extraneous, AFAICT.


> > > Of course, questions of the nature "what does that code do" are
> > > better addressed to those who wrote the code.


> > In other words it constructs an object within an existing buffer "h"
> > instead of allocating a new one.


> Maybe! We do not know what h is, so that is one possibility.
> Another one is that the memory is allocated from some special
> arena.


Well, we do know what happens at one level. The compiler will
generate code to call "operator new( sizeof( className ), h )",
then call the constructor of className with the arguments arg1
and arg2 on the results. Beyond that:

-- there must be a declaration for the corresponding operator
new function somewhere in scope, either as a member function
of className or one of its bases, or in global scope,

-- if that function is declared with a throw() exception
specifier, the compiler will check for null before calling
the constructor, and

-- if the constructor exits via an exception, and there is a
corresponding placement delete in scope, it will be called
with the return value of the operator new function.

As for the question "is this an accepted syntax?", it's too
vague to answer. It's certainly acceptable to the compiler
(provided the corresponding operator new function has been
declared). For readers? It depends on the context: I would, in
fact, be very surprised not to find something similar in an
implementation of std::vector. In fact, in
std::allocator<>::construct(), in the standard vector, but
probably directly in the vector class in any pre-standard
implementations, which didn't use allocators. It was certainly
present in my pre-standard array classes. (Except that I didn't
bother with the cast to void*. Usually, in such cases, you're
ignoring the return value completely---some people insert a cast
to void (not void*) in such cases, to shut up various lint
tools.)

--
James Kanze (GABI Software) email:(E-Mail Removed)
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
API for signal strength value with zero configuration feature on? =?Utf-8?B?a2Fu?= Wireless Networking 3 09-04-2005 11:13 PM
Disable the wireless network feature =?Utf-8?B?UmFqYXNla2FyYW4=?= Wireless Networking 1 05-04-2005 01:46 AM
Begging for a new feature! Alan P Firefox 2 04-03-2004 09:34 AM
bug or feature? Pedro Graca Firefox 3 01-02-2004 06:42 PM
feature question Jacob Farr Firefox 2 11-13-2003 09:50 AM



Advertisments