www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - array literals and the read only segment

reply Shachar Shemesh <shachar weka.io> writes:
Hello everyone,

Please consider the following snippet:
immutable ubyte[] array1 = [ 1, 2, 3, 4 ];
immutable ubyte[] array2 = cast(immutable ubyte[]) x"01 02 03 04";

Examining the resulting code, it is obvious that the literals 
initializing array2 are stored in the read only segment (.rodata), as 
they should be. It is equally obvious that array1 is stored in the data 
segment.

This is bad for a variety of reasons.

Is this a know issue?

Shachar
Dec 11 2016
next sibling parent reply Johan Engelen <j j.nl> writes:
On Sunday, 11 December 2016 at 12:48:17 UTC, Shachar Shemesh 
wrote:
 Is this a know issue?
Which compiler(s) did you test? -Johan
Dec 11 2016
parent reply Shachar Shemesh <shachar weka.io> writes:
On 11/12/16 21:03, Johan Engelen wrote:
 On Sunday, 11 December 2016 at 12:48:17 UTC, Shachar Shemesh wrote:
 Is this a know issue?
Which compiler(s) did you test? -Johan
DMD 2.072.1 and ldc 2.070.2 It's easy to verify. Just create a large array (1M) and check the segment sizes of the result. Shachar
Dec 11 2016
parent Johan Engelen <j j.nl> writes:
On Monday, 12 December 2016 at 06:28:09 UTC, Shachar Shemesh 
wrote:
 
 DMD 2.072.1 and ldc 2.070.2

 It's easy to verify. Just create a large array (1M) and check 
 the segment sizes of the result.
For LDC, it depends whether the variables are defined inside a function or not. ``` immutable ubyte[] array1 = [ 1, 2, 3, 4 ]; // ends up in data section immutable ubyte[] array2 = cast(immutable ubyte[]) x"01 02 03 04"; // not in data section void foo() { // Both initializers are _not_ put in the data section. immutable ubyte[] array1 = [ 1, 2, 3, 4 ]; immutable ubyte[] array2 = cast(immutable ubyte[]) x"01 02 03 04"; } ``` The module-scope variable's initializer ending up in the data section is a bug I think, but I have not thought it through enough. -Johan
Dec 12 2016
prev sibling parent Kagamin <spam here.lot> writes:
On Sunday, 11 December 2016 at 12:48:17 UTC, Shachar Shemesh 
wrote:
 Is this a know issue?
What's the issue? That array1 is in data section? That array2 is in rodata section? Or that they are in different sections? I suppose string literal goes to rodata because all string literals go there, and everything else goes to data.
Dec 12 2016