Memory allocation in D (noob question)

Sean Kelly sean at f4.ca
Wed Dec 5 07:49:43 PST 2007


Regan Heath wrote:
> 
> The example shown above is not corrupting any memory.  The 2nd one (not 
> shown above) seems to be and it worries me much more.

The same "copy on write" issue applies to each case, but I agree that 
the behavior of the second is certainly less appealing.  And you're 
right that it can and will corrupt memory if used in this manner.  A 
pointer to the head of the struct is equal to a pointer to the head of 
the array, so right now the runtime is assuming that the entire block 
belongs to the array, which is wrong.  Unfortunately, there is little 
that can be done about this mechanically.  When a slice of the struct 
array is taken, type information is lost, so the compiler doesn't even 
know there's a struct involved, and so would be unable to supply 
additional type information to the runtime.


Sean



More information about the Digitalmars-d mailing list