Re: Variables & kind of memory

2022-04-23 Thread Salih Dincer via Digitalmars-d-learn

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:14eb4a10:AAA
.LV:ptr :14eb4a077000:heap
.LF:ptr :7ffeb7c3f220:stack
.SLV:ptr:  4a5498:BBB
.SLF:ptr:14eb4a10:AAA
```

SDB@79


Re: Variables & kind of memory

2022-04-23 Thread Alain De Vos via Digitalmars-d-learn
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.  and  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 ?


Re: Variables & kind of memory

2022-04-23 Thread Stanislav Blinov via Digitalmars-d-learn

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.  and  in `main`.


Re: Variables & kind of memory

2022-04-23 Thread Mike Parker via Digitalmars-d-learn

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.


Re: Variables & kind of memory

2022-04-23 Thread Mike Parker via Digitalmars-d-learn

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.




Re: Variables & kind of memory

2022-04-22 Thread Alain De Vos via Digitalmars-d-learn

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 ?


Re: Variables & kind of memory

2022-04-22 Thread Salih Dincer via Digitalmars-d-learn

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", [0], [$-1]);
}
```

SDB@79


Re: Variables & kind of memory

2022-04-22 Thread Alain De Vos via Digitalmars-d-learn

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 ?


Variables & kind of memory

2022-04-22 Thread Alain De Vos via Digitalmars-d-learn

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",);
writefln("GLF:address :%12x:AAA",);
writefln("GSLV:address:%12x:AAA",);
writefln("GSLF:address:%12x:AAA",);
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",);
writefln(".LF:address :%12x:stack",);
writefln(".SLV:address:%12x:AAA",);
writefln(".SLF:address:%12x:AAA",);
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 :7fffdfd0:stack
.LF:address :7fffdfc8:stack
.SLV:address:   8002d6150:AAA
.SLF:address:   8002d6160:AAA
.LV:ptr :   80100:heap
.LF:ptr :7fffdfc8: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.