Strange code breaks build in MSVC. What does it mean?
Asked Answered
H

4

5

I am trying to include rapidxml into my current project. However, it would not build.

Visual Studio would complain about this piece of code (rapidxml.hpp:419+451):

419: void *memory = allocate_aligned(sizeof(xml_attribute<Ch>));
420: xml_attribute<Ch> *attribute = new(memory) xml_attribute<Ch>;

The compiler would say

rapidxml.hpp(420): error C2061: syntax error : identifier 'memory'

And I kind of see how this would confuse the compiler. It actually confuses me quite a bit, too. What is the (memory) part of new(memory) xml_attribute<Ch> doing there?

If I delete that (memory) part, it compiles just fine.
Also, gcc compiles it just fine with (memory) included.

Edit:
Oh, and I overloaded new with DEBUG_NEW to do some memory debugging. DEBUG_NEW does not support placement new.

Hoofbound answered 21/1, 2011 at 8:43 Comment(1)
It should always be easier to confuse an user than the compiler... The compiler should know its language and all gory details, while we users are limited and can only know so much.Baku
A
1

This is called "placement new". It instantiates an instance of xml_attribute in memory instead of allocating new memory for it. See:

http://www.parashift.com/c++-faq-lite/dtors.html#faq-11.10

I'm not sure why VC2010 has a problem with the syntax though.

Attemper answered 21/1, 2011 at 8:49 Comment(0)
D
3

This is my suggestion. The "memory" itself is defined somewhere also as a macro, and is getting expanded which causes the problem. So search for #define.memory (using regular expressions) to see if memory is defined as a macro.

As for the next statement, this form:

new(allocator) ObjectType(...)

is used when you want to use your own memory allocator to allocate memory for you.

Hope this helps.

Default answered 21/1, 2011 at 8:46 Comment(1)
Worth noting that once you want some specific class objects to be allocated in custom way you should just overload its operator new member function instead of using placement-new.Ectype
P
3

Have you got #include <new> in that file?

Perspiration answered 21/1, 2011 at 8:53 Comment(2)
@David: I don't know about VC, but g++ requires it.Perspiration
I do, there is #include <new> in that file.Hoofbound
A
1

This is called "placement new". It instantiates an instance of xml_attribute in memory instead of allocating new memory for it. See:

http://www.parashift.com/c++-faq-lite/dtors.html#faq-11.10

I'm not sure why VC2010 has a problem with the syntax though.

Attemper answered 21/1, 2011 at 8:49 Comment(0)
A
1

The syntax new (pointer) type( argument ) is called placement new, and it basically represents a call to the type constructor with the given argument over the memory previously allocated in pointer.

The syntax, in as much as it is shown, is correct. Maybe a couple of lines above there is a missing semi-colon or syntax error that is confusing the parser, but memory as an identifier is correclty used defined in the previous line. (And it is not reserved by the language for the implementation)

Arquit answered 21/1, 2011 at 8:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.