« first day (445 days earlier)      last day (4486 days later) » 

12:01 AM
it seems that you can't delete[] array initialized with placement new, so it shouldn't keep information about size
 
If you placement new something, you need to use explicit destructor calls.
So with placement new[] you need to track how many destructors to call yourself.
 
Why does placement new[] even exist? Can't we just use placement new and treat it as an array? In fact, I believe that is exactly what std::vector<T> does.
 
0
Q: Placement new requires unspecified space in the buffer?

Mooing Duck5.3.4 [expr.new] of the C++11 Feb draft gives the example: new T[5] results in a call of operator new[](sizeof(T)*5+x) with the text: Here, x and y are non-negative unspecified values representing array allocation overhead; the result of the new-expression will be offset by this amount f...

 
@FredOverflow Yes, if you have enough storage you can just construct the elements yourself.
 
Crap I meant operator new, not placement new, I hope that was obvious :)
 
12:06 AM
I linked the beginning of the chat as well, if anyone cares
 
Why does operator new[] even exist? Can't we just use operator new and treat it as an array? In fact, I believe that is exactly what std::vector<T> does.
 
@RMartinhoFernandes and destruct if exception occurs
 
@FredOverflow you already said that. And no reason at all.
 
@FredOverflow To make placement new[] expressions work.
Like @Abyx remarked, it handles exceptions.
 
hmm.. is it worth while writing an ircd <-> SO chat translator, so that any IRC client could be used to access this chat?
 
12:08 AM
@MooingDuck I replaced two instances of operator new with operator new[] in your question, I hope that was okay.
@refp Would the IRC client notice edits and deletes?
 
and please don't mention that python implementation, it sucks balls and besides.. that project seems dead
 
@FredOverflow yeah, I typo'd. Kerrek sure lept on that
 
@FredOverflow it might send those as NOTICE's, yes
 
I'm gonna goto sleep;
 
@MooingDuck No, I didn't already say that.
 
12:09 AM
@DeadMG Noooo! Use a loop!
 
@DeadMG Don't let the raptors get you in your sleep!
 
@FredOverflow though I already have a prewritten irssi module which adds support for regular expression replacements, but sure - of course edits and deletes will be taken into consideration
 
lol
 
@FredOverflow oh, a few words changed
 
@MooingDuck: There's even an FPA on allocation. That's how often it comes up.
 
12:10 AM
@KerrekSB FPA?
 
@MooingDuck Linked. A "frequently pasted answer"
 
@KerrekSB true, but I never thought about who calls the constructors before.
@KerrekSB which is sad since I've written three allocators myself :(
 
your mother does
 
@FredOverflow and of course (since I like making things overly complicated) I'll write the whole thing in c++
 
@KerrekSB: the duck was a bit careless with the question and your answer does not at all answer the intended question :( Not your fault.
 
12:13 AM
@MooingDuck You should.
@RMartinhoFernandes What's the intended question then?
 
@KerrekSB I posted a comment.
 
I don't think that's valid C++
new (buf) T[10]??
 
Looks weird doesn't it?
But there is a void* operator new[](size_t, void*) that exists just to support that.
 
@KerrekSB yeah, there was two questions. I made them both bold now.
 
@KerrekSB Actually, it is valid C++.
 
12:15 AM
@KerrekSB that's what sparked the question/debate
@RMartinhoFernandes facepalms and walks away humilated
 
@MooingDuck And I just fixed it.
 
@MooingDuck Oh OK, but that placement function just returns the second argument, doesn't it?
 
@KerrekSB yes. Well, now I know that. Your answer should mention it.
 
Yeah. Global placement-new and placement-array-new operators are required to be no-ops.
 
@KerrekSB it seems like a conforming compiler could give operator new[] my pointer + sizeof(T) and that would be ok, except my code would now write past the end of the array, which the spec clearly states is my fault :(
 
12:21 AM
Right, but the result of the new expression is offset by that + x part from the result of operator new[].
 
@RMartinhoFernandes I edited further.
 
And ...
> The address of the created object will not necessarily be the same as that of the block if the object is an array.
There must be a guarantee somewhere that void* buf = get_it(); auto* buf2 = new(buf) T[10]; assert(buf == buf2);.
 
@FredOverflow eh, I guess the second question wasn't necessary, and was distracting from the main point wasn't it?
 
OK, updated.
 
@MooingDuck Yes, absolutely.
 
12:22 AM
Without that, you can't be sure to allocate a buffer big enough.
 
@MooingDuck No, global placement-operators are required to be no-ops.
Otherwise you couldn't reliably implement any other higher-level allocation constructs.
 
@KerrekSB right, but new might give the global placement operator the buffer + unspecified value according to the quote in the question.
 
@KerrekSB But when the object is an array, the new expression is not required to return the same address as operator new[].
 
@RMartinhoFernandes yes it is, you got that backwards
 
3 mins ago, by R. Martinho Fernandes
> The address of the created object will not necessarily be the same as that of the block if the object is an array.
 
12:24 AM
@RMartinhoFernandes the new expression is not required to give operator new[] the address it was given.
 
It's in a Note though.
 
Oh, I see you're still on that.
 
@MooingDuck Yes, it is. It must pass the arguments along unchanged.
 
@RMartinhoFernandes yeah, because it might give operator new a different address than the buffer
 
@MooingDuck No!
 
12:25 AM
@RMartinhoFernandes no? Did I do it wrong again? I'm going to reread
5.3.4 [expr.new] new(2,f) T[5] results in a call of operator new[](sizeof(T)*5+y,2,f)
 
By the way, thanks for your help. I think I'll go to sleep now.
 
@MooingDuck It's only the size argument that is special.
@MooingDuck The 2 and the f are passed unchanged.
 
@RMartinhoFernandes oh, whoops, you're 100% correct
 
@RMartinhoFernandes No, indeed. The new expression returns the beginning of the array
 
@KerrekSB Right. See the issue now?
How can you make a buffer big enough to placement new an array in it?
5 mins ago, by R. Martinho Fernandes
There must be a guarantee somewhere that void* buf = get_it(); auto* buf2 = new(buf) T[10]; assert(buf == buf2);.
 
12:27 AM
@RMartinhoFernandes You just make it N * sizeof(T) big
 
@KerrekSB what about the unspecified overhead?
 
@KerrekSB But what if y > 0?
 
The *placement*-array-new expression returns the actual pointer
@MooingDuck That doesn't exist for placement-new
Only the **default**-array-new is the odd-man-out
@FredOverflow It won't be
 
@KerrekSB Find the guarantee it returns the same pointer as operator new[] and quote it in your answer.
 
@KerrekSB spec doesn't seem to say that. Interweb seems to imply it's only coincidentally adding 0 bytes of overhead
 
12:29 AM
All I can find is mentions that it can return a different one.
 
::new (buf) T[10] is essentially identical to ten constructions at buf + i
 
@KerrekSB Then why does the standard say new(2,f) T[5] results in a call of operator new[](sizeof(T)*5+y,2,f)?
 
@FredOverflow That's not the global default placement-new...
Maybe there's some confusion? There's the "standard placement new", and then there are all other "generalized placement new"s
 
@KerrekSB But the same thing applies to global placement new[]. It's quoted in the question.
 
@KerrekSB that bit of the standard applies to all new expressions as far as I can tell
 
12:31 AM
> the result of the new-expression will be offset by this amount from the value returned by operator new[]
This doesn't say the amount is zero (I previously misinterpreted it and say it did imply that).
 
chat.stackoverflow.com/transcript/message/2270765#2270765 I should keep track of how many times he's moo'd at me
 
I see.
OK then. But does it matter?
 
Good. Now you can hunt the real issue.
:)
 
You just allocate as much as you're asked
 
@RMartinhoFernandes where's your answer?
 
12:33 AM
@KerrekSB You can't know that.
 
The allocation function only sees the final value
@RMartinhoFernandes Why not? It's the argument value!
 
@KerrekSB You can't get it from a standard library function.
 
new (buf) T[10]; may call T::new[](317, buf), so you allocate 317 bytes
@RMartinhoFernandes I'm not following. Can you give me an example of where you don't know how much memory you need?
 
void* buf = get_it(); auto* buf2 = new(buf) int[10];
 
If I want to construct a 10 int array in place, I say new (buffer40bytes) int[10]; which will call operator new[](40 + y, buffer40bytes) under the hood, right? If y > 0 then the buffer is too small, so how much should I have allocated in the first place?
 
12:35 AM
Exactly.
 
Ah. You don't use placement-array-new.
 
@KerrekSB there seems to be no guarantee in the standard that placement new requires no overhead space
 
@KerrekSB lol
 
Use a loop of ordinary placement-news instead
 
@KerrekSB so it's in the language but cannot be safely used?
 
12:35 AM
Array-new is for client-code only.
 
So the answer is "Global placement array new is unusable"?
 
@KerrekSB client-code?
 
Interesting. So we still have the requirement that ::operator new[](n, p) returns p, right?
 
@MooingDuck Well, I noticed I misinterpreted that offseting part. So I don't have one yet.
 
@MooingDuck As in, not library code
 
12:36 AM
@KerrekSB yes
 
@KerrekSB Right. But ::new(p) T[n] can return p+10 if it wants.
 
Well, your own operator new[](size_t, void*) is welcome to check that the pointer points to sufficient memory...
 
(Unless you find proof to the contrary, that is)
 
@RMartinhoFernandes Hm. Apparently. Interesting.
OK, to condense the question down: Is the following guaranteed to work:
 
So, did I mention I'm starting to like Factor very much?
 
12:39 AM
void * addr = malloc(10 * sizeof(T));
T * arr = ::new (addr) T[10];
 
And that I hate the thing that I'm supposed to be learning now?
 
@KerrekSB Right.
 
That, by itself, would make a more palatable SO question! :-)
 
@KerrekSB yeah, that was the intent
 
Second question: Is arr == addr?
 
12:40 AM
@KerrekSB Are you stepping up, or should I try my failure some more?
 
Very interesting. Indeed, if it isn't, then it's hard to envisage how you'd use placement-array-new.
 
18 mins ago, by R. Martinho Fernandes
There must be a guarantee somewhere that void* buf = get_it(); auto* buf2 = new(buf) T[10]; assert(buf == buf2);.
@CatPlusPlus Yes, both. More than once.
@KerrekSB Yep.
 
Well, it's true.
 
@MooingDuck I wouldn't mind if you simplified the question a bit... :-) But no worries. Let me check the standard again.
Doesn't help that allocation is spread out through three different sections
 
@KerrekSB three? I only found two. Then again I've never found vector's constructors either.
 
12:42 AM
5, 12 and 18 I think
 
12 is Special member functions.
 
Oh, 3 as well
3.7.4
 
@KerrekSB: You're basically covering the whole standard using a monte-carlo algorithm now?
 
@RMartinhoFernandes oh right, free store member function
 
@sehe No, just trying to gather all the parts that relate to allocation ;-) It's similar to throwing random darts at the standard.
 
12:45 AM
That's what I meant
 
@RMartinhoFernandes er, what if that space is there for alignment purposes for placement-new into a local char buffer? Is that the answer to all this? Why doesn't the standard say?
 
I think it's this: 3.7.4, 5.3.4, 12.5, 18.6
I think the goal is to recover the digits of pi
 
@MooingDuck You're required to take care of alignment yourself (new char[n] is properly aligned, automatic arrays are not).
 
@RMartinhoFernandes oh right. But I haven't seen that bit in the spec during this discussion. WHere's it say that?
 
Dunno. :(
In one of the several places it talks about allocation.
 
12:48 AM
@RMartinhoFernandes wait, wrong section
 
@MooingDuck Perhaps you should change your title to "Placement array-new ..."
 
@MooingDuck lol
 
@RMartinhoFernandes I see the requirement on the allocators but not on the parameter so far
 
@Pubby What should I be expecting to see?
 
12:51 AM
@KerrekSB I gave the question a makeover.
6
Q: Placement array new requires unspecified overhead in the buffer?

Mooing Duck5.3.4 [expr.new] of the C++11 Feb draft gives the example: 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 unspecified values representing array allocation overhead; the result of the new-expression will be offset by this amount from ...

 
@RMartinhoFernandes It's not working? It's like that interactive haskell prompt except with JavaScript
 
@Pubby Ah, needs JS. Lemme try. If my browser is compromised it's your fault.
 
Great job! Now, let's do some math. You can do math through programming! To calculate 2+2, for instance, just type 2+2. Try it now!
> 3+3
==> 6
Oops, try again.
3+3 is not math.
 
Hi. Do we have a list of the 10 most common C++ questions that come up each year when college starts? ie. Why does ++c++ seem to work on my machine.
 
12:53 AM
@RMartinhoFernandes I've searched for the word "alignment" in all the sections kerrek linked, (5.3.4 twice!) and I can't find anything that says the buffer to placement new must be aligned. Only that allocators are assumed to return aligned buffers. I guess since operator new(size_t, void*) returns it's parameter, that's almost an implicit requirement....
 
@MooingDuck But since placement new doesn't align it, it's up to you.
 
@RMartinhoFernandes they should put this stuff in the standard somewhere :(
 
@LokiAstari ++c++ cannot possibly work, because it is parsed as ++(c++), but c++ is an rvalue, and you cannot increment that (unless c is an object of a user-defined type).
 
"3.11 Alignment [basic.align]"
1 Object types have alignment requirements (3.9.1, 3.9.2)
 
@MooingDuck It is. In the place that says in general you can't use stuff unaligned.
 
12:55 AM
@MooingDuck alignment is a requirement of the type in question, not of new expression
 
@LokiAstari "why does it work when I return a pointer to a local int and read and write it?"
 
Nearly all questions are about some form of UB.
 
@MooingDuck returning local ints is not a problem per se, only if the local int is automatic. If it is static, there is no problem.
 
@FredOverflow: Yes I know. The point is do we have a list of the stupid questions that are asked each year.
 
@CatPlusPlus yeah
 
12:56 AM
@FredOverflow But newbies don't even know what a static local is so they don't ask about it.
 
The rest is about crashes by stupid.
 
@MooingDuck: Why does what work?
 
@LokiAstari see what that was a reply to
 
@LokiAstari that's what a faq list should be, ideally
 
@LokiAstari I don't think so. Do we need a tag (yearly asked stupid questions)? :)
 
12:57 AM
@AlfPSteinbach nobody reads those
 
maybe the C++ FAQ Lite is what you're l00king for
?
 
Why zeros?
 
@AlfPSteinbach I don't think FAQ == stupid question.
 
why n0t?
 
FAQ is not Frequently Asked Non-Stupid Questions.
3
 
12:58 AM
so did we all give up on the placement array new question then? Fair enough
 
@FredOverflow i think we can safely assume that the most asked questions are stupid
 
Hmmm.. I think what really matters, at the heart of things, is what the array-new expression does.
 
So it includes stupid questions too.
@MooingDuck I think Kerrek is still standard-diving.
 
Ultimately I don't care if it passes a N * sizeof(T) + 1000 to the allocation function.
As long as it constructs the array starting at the specified address.
 
@KerrekSB If it does, it returns p + 1000.
 
12:59 AM
@FredOverflow It's very nice now.
 
The result of the expression is offset by the overhead.
 
@RMartinhoFernandes No, it returns the second argument!
 
@KerrekSB Cool thanks :)
 
@RMartinhoFernandes Kerrek is right, it returns the correct pointer
 
@KerrekSB The new expression, I mean.
 
12:59 AM
@RMartinhoFernandes Does it say that?
 
If new(buf) T[10] calls operator new[](sizeof(T)*10 + 1000, buf) it returns buf+1000.
 

« first day (445 days earlier)      last day (4486 days later) »