Question about wchar[]

Steven Schveighoffer schveiguy at yahoo.com
Mon Feb 4 12:09:06 PST 2013


On Mon, 04 Feb 2013 14:45:02 -0500, ollie <ollie at home.net> wrote:

> What is the storage (heap/stack) of "straight assignment" if this were a
> local variable.  Or do you mean that "This is a wchar[]" was already
> created on the heap as an immutable(wchar)[] then assigned to wstr.

The string is actually stored in code, not on the heap.  When you assign,  
you get a pointer right into code (ROM).

On some OSes, you can possibly modify this value, on some you will get a  
memory error.

In D1, where there was no immutable, string literals *were* char[].  Then  
you could have funky stuff like this:

auto str = "hello";
str[0] = 'j';

auto str2 = "hello";
writefln(str2); // writes "jello"

Of course, this only worked on one of the OSes (I think it was windows).   
On the others you would get an error.

Array literals are different, they are allocated on the heap when they are  
used.  so ['h', 'e', 'l', 'l', 'o'] is very different than "hello".

There has in the past been a push to make ALL array literals immutable,  
but it has never gone anywhere.

-Steve


More information about the Digitalmars-d-learn mailing list