String Literals (D 2.0)

Xinok xnknet at gmail.com
Tue Jun 19 15:01:50 PDT 2007


Vladimir Panteleev wrote:
>> Yet if you write:
>> int[] str = [10, 20, 30];
>> It gives no error.
> 
> I'm not sure whether integer literals are also placed in the same data segment for read-only memory. If it is, then it's a bug - all array literals should be immutable.

I wrote a simple test to figure out what exactly the compiler does with 
array literals:


void test1(int count = 3){
	if(count > 0) test1(count-1);
	int[] arr = [10, 20, 30];
	writefln(&arr[0]);
}

void test2(int count = 3){
	if(count > 0) test2(count-1);
	invariant int[] arr = [10, 20, 30];
	writefln(&arr[0]);
}

void main(){
	test1(); test2();
}


The results:
880FE0
880FD0
880FC0
880FB0
415080
415080
415080
415080

For mutable types, the array literal is dup'd.
For invariant types, the array literal is referenced.


I prefer it this way, and I hope the same will be done for string literals.



More information about the Digitalmars-d mailing list