www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Array Appending Plus Postblits

reply dsimcha <dsimcha yahoo.com> writes:
I was browsing through druntime's lifetime.d for an unrelated reason and I
noticed that, when an array is copied for the purpose of appending to it, the
postblits are not called on the new array if it is an array of structs.  Is
there something I'm missing, or is this a bug that somehow noone has noticed
up to this point?
Nov 23 2010
next sibling parent reply Jonathan M Davis <jmdavisProg gmx.com> writes:
On Tuesday 23 November 2010 20:43:34 dsimcha wrote:
 I was browsing through druntime's lifetime.d for an unrelated reason and I
 noticed that, when an array is copied for the purpose of appending to it,
 the postblits are not called on the new array if it is an array of
 structs.  Is there something I'm missing, or is this a bug that somehow
 noone has noticed up to this point?
Well, if it's copied for the purpose of appending, the elements need to be moved, not copied, don't they? And if they're moved, there's no need to call the postblit constructor. - Jonathan M Davis
Nov 23 2010
parent Max Samukha <spambox d-coding.com> writes:
On 11/24/2010 07:06 AM, Jonathan M Davis wrote:
 On Tuesday 23 November 2010 20:43:34 dsimcha wrote:
 I was browsing through druntime's lifetime.d for an unrelated reason and I
 noticed that, when an array is copied for the purpose of appending to it,
 the postblits are not called on the new array if it is an array of
 structs.  Is there something I'm missing, or is this a bug that somehow
 noone has noticed up to this point?
Well, if it's copied for the purpose of appending, the elements need to be moved, not copied, don't they? And if they're moved, there's no need to call the postblit constructor.
The appended elements would need to be postblitted. Also, the original elements would need to be postblitted in case the append triggers a reallocation. They cannot be simply moved because there may be slices pointing to the original array. Anyway, in the general case we cannot store structs with dtors/postblits in dynamic arrays (append requires GC and GC cannot destroy structs), which means you will have to use a special container for that purpose.
 - Jonathan M Davis
Nov 24 2010
prev sibling parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Tue, 23 Nov 2010 23:43:34 -0500, dsimcha <dsimcha yahoo.com> wrote:

 I was browsing through druntime's lifetime.d for an unrelated reason and  
 I
 noticed that, when an array is copied for the purpose of appending to  
 it, the
 postblits are not called on the new array if it is an array of structs.   
 Is
 there something I'm missing, or is this a bug that somehow noone has  
 noticed
 up to this point?
Array appending does not respect any of the new copy construction semantics. It basically does a memcpy and that's it. It doesn't even respect shared (shared data is memcpy'd just like any other data), although it does use a lock to protect shared appends. I see there is a postblit function in the typeinfo, so it should be possible to do the right thing. One thing I'm unsure of is how postblit works on const or immutable structs, anyone know? There is no typechecking in the array functions, everything is a void[]. File a bug on it please. -Steve
Nov 24 2010