Question about wchar[]

Jonathan M Davis jmdavisProg at gmx.com
Mon Feb 4 11:54:14 PST 2013


On Monday, February 04, 2013 19:45:02 ollie wrote:
> > Right, because you are duplicating the string onto the heap, and making
> > it mutable.
> 
> I thought druntime always created dynamic arrays on the heap and returned
> a slice.

It does, but duping or iduping an array or string literal would allocate 
_again_.

Also, in at least some cases (e.g. Linux) string literals end up in ROM such 
that they're shared (both across threads and across uses - e.g. multiple uses 
of the string literal "hello world" will potentially end up being exactly the 
same string in memory). As such, they're actually allocated as part of the 
program itself rather than when they're used (unlike with normal array 
literals). So, doing

auto str = "hello world";

won't necessarily allocate anything. But I believe that that's implementation-
dependent (e.g. I'm don't think that that happens on Windows).

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list