www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Variables & kind of memory

reply Alain De Vos <devosalain ymail.com> writes:
I wrote a simple test program:
```
import std.stdio:writefln;

int [] GLV=[1,2];
int [2] GLF=[1,2];
static int [] GSLV=[1,2];
static int [2] GSLF=[1,2];

void main(){

writefln("-------------");
writefln("GLV:address :%12x:AAA",&GLV);
writefln("GLF:address :%12x:AAA",&GLF);
writefln("GSLV:address:%12x:AAA",&GSLV);
writefln("GSLF:address:%12x:AAA",&GSLF);
writefln("GLV:ptr     :%12x:BBB",GLV.ptr);
writefln("GLF:ptr     :%12x:AAA",GLF.ptr);
writefln("GSLV:ptr    :%12x:BBB",GSLV.ptr);
writefln("GSLF:ptr    :%12x:AAA",GSLF.ptr);

int [] LV=[1,2];
int [2] LF=[1,2];
static int [] SLV=[1,2];
static int [2] SLF=[1,2];

writefln("------------");
writefln(".LV:address :%12x:stack",&LV);
writefln(".LF:address :%12x:stack",&LF);
writefln(".SLV:address:%12x:AAA",&SLV);
writefln(".SLF:address:%12x:AAA",&SLF);
writefln(".LV:ptr     :%12x:heap",LV.ptr);
writefln(".LF:ptr     :%12x:stack",LF.ptr);
writefln(".SLV:ptr    :%12x:BBB",SLV.ptr);
writefln(".SLF:ptr    :%12x:AAA",SLF.ptr);
```
Which outputs:
```
-------------
GLV:address :   8002d6120:AAA
GLF:address :   8002d6130:AAA
GSLV:address:   8002d6138:AAA
GSLF:address:   8002d6148:AAA
GLV:ptr     :      298808:BBB
GLF:ptr     :   8002d6130:AAA
GSLV:ptr    :      298810:BBB
GSLF:ptr    :   8002d6148:AAA
------------
.LV:address :7fffffffdfd0:stack
.LF:address :7fffffffdfc8:stack
.SLV:address:   8002d6150:AAA
.SLF:address:   8002d6160:AAA
.LV:ptr     :   801000000:heap
.LF:ptr     :7fffffffdfc8:stack
.SLV:ptr    :      298818:BBB
.SLF:ptr    :   8002d6160:AAA
```

The output has clearly 4 different kind of memory regions.
I have stack and heap, but don't know what AAA & BBB regions are.
Feel free to elaborate.
Apr 22 2022
next sibling parent reply Alain De Vos <devosalain ymail.com> writes:
On Saturday, 23 April 2022 at 03:41:17 UTC, Alain De Vos wrote:
 I wrote a simple test program:
 ```
 import std.stdio:writefln;

 [...]
BBB: is probably the data-segment. Remains AAA ?
Apr 22 2022
parent reply Alain De Vos <devosalain ymail.com> writes:
On Saturday, 23 April 2022 at 04:52:39 UTC, Alain De Vos wrote:
 On Saturday, 23 April 2022 at 03:41:17 UTC, Alain De Vos wrote:
 I wrote a simple test program:
 ```
 import std.stdio:writefln;

 [...]
BBB: is probably the data-segment. Remains AAA ?
Could AAA be the bss-segment ?
Apr 22 2022
parent Mike Parker <aldacron gmail.com> writes:
On Saturday, 23 April 2022 at 05:01:51 UTC, Alain De Vos wrote:
 On Saturday, 23 April 2022 at 04:52:39 UTC, Alain De Vos wrote:
 On Saturday, 23 April 2022 at 03:41:17 UTC, Alain De Vos wrote:
 I wrote a simple test program:
 ```
 import std.stdio:writefln;

 [...]
BBB: is probably the data-segment. Remains AAA ?
Could AAA be the bss-segment ?
Run a dump tool on the generated binary and you can see what goes where. And there are probably differences between the three D compilers.
Apr 22 2022
prev sibling next sibling parent Salih Dincer <salihdb hotmail.com> writes:
On Saturday, 23 April 2022 at 03:41:17 UTC, Alain De Vos wrote:
 I wrote a simple test program:
 ```d
 import std.stdio:writefln;

 int [] GLV=[1,2];
 int [2] GLF=[1,2];
 
 // paste it ->>
 
 static int [] GSLV=[1,2];
 static int [2] GSLF=[1,2];

 void main()
 {
 
 }
 ```
First of all, there is no such thing as a global region in D. Although the addresses are different, we have 2 regions: * **AAA:** Stack * **BBB:** Heap Yes, there are a lot of expressions: ```static, const, immutable, shared etc.``` But the expressions matters to D compiler. Two concepts are important to us: the inter-module region and the ```main()``` function where the code starts to run. Please paste the following into the inter-module region. Turn it on/off with the toggle (//) sign and see the difference. ```d //auto data = iota(1024).array; /* int[] data; static this() { data = new int[1024]; } //*/ void main() { writefln("%12x, %12x", &data[0], &data[$-1]); } ``` SDB 79
Apr 22 2022
prev sibling next sibling parent Mike Parker <aldacron gmail.com> writes:
On Saturday, 23 April 2022 at 03:41:17 UTC, Alain De Vos wrote:
 int [] GLV=[1,2];
 int [2] GLF=[1,2];
 static int [] GSLV=[1,2];
 static int [2] GSLF=[1,2];
FYI, `static` has no effect at module scope.
Apr 22 2022
prev sibling parent reply Stanislav Blinov <stanislav.blinov gmail.com> writes:
On Saturday, 23 April 2022 at 03:41:17 UTC, Alain De Vos wrote:

 Feel free to elaborate.
Variables declared at module scope, and static variables in function/aggregate scopes, unless also annotated as `shared` or `__gshared`, are thread-local, therefore are placed in thread-local storage. That's why you see difference in addresses between e.g. &LV and &SLV in `main`.
Apr 23 2022
parent reply Alain De Vos <devosalain ymail.com> writes:
On Saturday, 23 April 2022 at 10:46:42 UTC, Stanislav Blinov 
wrote:
 On Saturday, 23 April 2022 at 03:41:17 UTC, Alain De Vos wrote:

 Feel free to elaborate.
Variables declared at module scope, and static variables in function/aggregate scopes, unless also annotated as `shared` or `__gshared`, are thread-local, therefore are placed in thread-local storage. That's why you see difference in addresses between e.g. &LV and &SLV in `main`.
Maybe i can use gdb to see more info. This is not a gdb forum but if someone knows the commands to verify the segments/heap/stack ?
Apr 23 2022
parent Salih Dincer <salihdb hotmail.com> writes:
On Saturday, 23 April 2022 at 17:14:22 UTC, Alain De Vos wrote:
 Maybe i can use gdb to see more info.
 This is not a gdb forum but if someone knows the commands to 
 verify the segments/heap/stack ?
Maybe it'll be helpful. The following output is from another compiler: ``` GLV:address :14eb4a177730:AAA GLF:address :14eb4a177740:AAA GSLV:address:14eb4a177748:AAA GSLF:address:14eb4a177758:AAA GLV:ptr : 4a5488:BBB GLF:ptr :14eb4a177740:AAA GSLV:ptr : 4a5490:BBB GSLF:ptr :14eb4a177758:AAA ------------ .LV:address :7ffeb7c3f228:stack .LF:address :7ffeb7c3f220:stack .SLV:address:14eb4a177760:AAA .SLF:address:14eb4a177770:AAA .LV:ptr :14eb4a077000:heap .LF:ptr :7ffeb7c3f220:stack .SLV:ptr : 4a5498:BBB .SLF:ptr :14eb4a177770:AAA ``` SDB 79
Apr 23 2022