www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - dynamic array during appending to array of static array?

reply "Namespace" <rswhite4 googlemail.com> writes:
----
void main() {
	float[3][] arr;
	arr ~= [1, 2, 3];
}
----
It seems [1, 2, 3] is in this case erroneously a dynamic array 
(according to objconv). So is it also erroneously allocated on 
the heap?
Dec 02 2013
parent reply "bearophile" <bearophileHUGS lycos.com> writes:
Namespace:

 void main() {
 	float[3][] arr;
 	arr ~= [1, 2, 3];
 }
 ----
 It seems [1, 2, 3] is in this case erroneously a dynamic array 
 (according to objconv). So is it also erroneously allocated on 
 the heap?
Generally array literals allocate on the heap. Recently some of their cases have being optimized, but even more recently part of those optimizations were reverted. So I don't know what's happening in your case and why. With DMD your code generates this (optimized build!), so it's far from well optimized: __Dmain: sub ESP,020h mov EAX,offset FLAT:_D12TypeInfo_G3f6__initZ push EBX push ESI push EDI mov dword ptr 010h[ESP],0 mov dword ptr 014h[ESP],0 push 3 push EAX call near ptr __d_arrayliteralTX mov EBX,EAX fld float ptr FLAT:_DATA[00h] mov ESI,EBX lea EDI,028h[ESP] fstp float ptr [EBX] fld float ptr FLAT:_DATA[04h] fstp float ptr 4[EBX] fld float ptr FLAT:_DATA[08h] fstp float ptr 8[EBX] movsd movsd movsd mov EDX,offset FLAT:_D13TypeInfo_AG3f6__initZ push 1 lea ECX,01Ch[ESP] push ECX push EDX call near ptr __d_arrayappendcTX lea ESI,034h[ESP] mov 02Ch[ESP],EAX mov EDI,02Ch[ESP] lea EDI,[EDI*2][EDI] mov 030h[ESP],EDX mov EAX,030h[ESP] lea EDI,-0Ch[EDI*4][EAX] movsd movsd movsd add ESP,014h xor EAX,EAX pop EDI pop ESI pop EBX add ESP,020h ret With ldc2 V.0.12.1 the situation is better: __Dmain: pushl %ebp movl %esp, %ebp andl $-8, %esp subl $24, %esp movl $0, 16(%esp) movl $0, 20(%esp) leal 16(%esp), %eax movl %eax, 4(%esp) movl $1, 8(%esp) movl $__D13TypeInfo_AG3f6__initZ, (%esp) calll __d_arrayappendcTX movl 20(%esp), %eax movl $1065353216, (%eax) movl $1073741824, 4(%eax) movl $1077936128, 8(%eax) xorl %eax, %eax movl %ebp, %esp popl %ebp ret Bye, bearophile
Dec 02 2013
parent "Namespace" <rswhite4 googlemail.com> writes:
So it would be unfortunately better to use a struct with a 
internal static array. :/
Dec 02 2013