alloca and placement new[] weirdness

Started by
18 comments, last by Naurava kulkuri 15 years, 5 months ago
When I first read the thread's title I was reminded of something: don't try to use alloca as the allocator inside of a placement new. [headshake]
Mike Popoloski | Journal | SlimDX
Advertisement
Zahlman, the code you wrote is exactly the same as mine except you used reinterpret_cast where only static_cast is required, you made the code exception-unsafe, and on top of everything made it bogus and non-compilable.

It's not
assert (new (spams) Spam) == spams;

but
Spam* tmp = new (&spams) Spam;assert(tmp == &spams);
Quote:Original post by the_edd

You are mistaken :( (unless I misunderstand what you're saying).

If you use placement new to create an object in an array of chars (for example), then the destructor for that array will be called when its lifetime ends. Of course, the destructor for an array of chars is essentially a no-op. It doesn't know that placement new has put something special there.

Each placement new must have a matching manual destructor invocation.


Yes. But I meant if an exception is thrown by a constructor, new[] should be able to destruct the successfully created instances and rethrow the exception. If it wasn't like that there would be no way to tell how many objects you need to destruct which would render new[] practically useless (as the code above my post indicated).

Quote:Original post by loufoque
Zahlman, the code you wrote is exactly the same as mine except you used reinterpret_cast where only static_cast is required, you made the code exception-unsafe, and on top of everything made it bogus and non-compilable.

It's not
assert (new (spams) Spam) == spams;

but
Spam* tmp = new (&spams) Spam;assert(tmp == &spams);


Er, didn't read your post. And of course the addresses are wrong and assert is a function or macro (so there should be parentheses around the whole thing), but it should still be doable on one line. (Also, '&spams[i]' should be writable as 'spams + i'.)

Blah. I guess the short version is, never mind me. :)
Quote:Original post by Zahlman
"{ assert (new (spams) Spam) == spams; }"
--
And of course the addresses are wrong and assert is a function or macro (so there should be parentheses around the whole thing)


The parentheses should be done by the macro-writer, this is not the problem. The real problem with that line is, that when _NDEBUG / NDEBUG is defined before inclusion of the assert-header, then your whole compound becomes

{ ((void) 0) == spams; }


Even without NDEBUG, it could be
{ (0 != (new (spams) Spam)) == spams; }

edit: My failure this time. Of course the non-NDEBUG assert will probably emit some if-statement or inline-assembly, so compilation with NDEBUG may work, but yields undefined behaviour, and without NDEBUG will probably always fail. I hate bugs, i love them!


Which is not what you intended, I guess.

And afaik, assert() is supposed to be of type void, which drives both of my statements to undefined behaviour nevertheless.


edit: fixed typo
But if I do put parentheses around the whole thing, then the whole thing will be interpreted as the macro argument, and then the statement just becomes '((void)0);', right? :/
Indeed.
Putting anything else than a test in assert is bogus, because it will only be executed in debug mode.
Oh bahhhh I hadn't even considered that the placement new part is something I want to do in both cases... >_< Several bad days in a row apparently.
Quote:Original post by Zahlman
But if I do put parentheses around the whole thing, then the whole thing will be interpreted as the macro argument, and then the statement just becomes '((void)0);', right? :/


"whole thing" could be anything :D
But yes, it would be "((void)0);" then, a nop ;)
Raymond Chen, the man who knows the ins-and-outs of Windows systems and compilers, blogs about this very issue in hist post Mismatching scalar and vector new and delete. Indeed, reinterpret_cast should work for now, as noted by Extrarius. Be sure to check the comments as well as the answers.

[Edited by - Naurava kulkuri on November 17, 2008 1:16:31 PM]
---Sudet ulvovat - karavaani kulkee

This topic is closed to new replies.

Advertisement