Re: How to free memory ater use of "new" to allocate it.

2023-07-16 Thread Richard (Rikki) Andrew Cattermole via Digitalmars-d-learn
Steven is right, was quite late when I said that so it'll work but not 
for the reasons I thought it would.


Re: Print debug data

2023-07-16 Thread Chris Katko via Digitalmars-d-learn

On Monday, 17 July 2023 at 03:43:04 UTC, Alain De Vos wrote:

Is it possible to print runtime memory usage of:
-The stack
-The heap
-The garbage collector ?


there's gc.stats for part of it:

https://dlang.org/library/core/memory/gc.stats.html



Re: How to free memory ater use of "new" to allocate it.

2023-07-16 Thread Alain De Vos via Digitalmars-d-learn
Maybe code above works when you enforce an Garbage-collection-run 
?


Code below works fine. So you cannot use "new" but must use 
malloc?


```

import std.stdio:writefln;
import object: destroy;
import core.memory: GC;
import core.stdc.stdlib: malloc,free;


void dofun(){
   auto pa=cast(int *)malloc(1000*int.sizeof);
   writefln("%12x",pa);
   auto a=pa[0..1000];
   free(a.ptr);
}

int main(){
dofun();
   auto pb=cast(int *)malloc(1000*int.sizeof);
   writefln("%12x",pb);
   auto b=pb[0..1000];
   free(b.ptr);
   return 0;

}

```


Print debug data

2023-07-16 Thread Alain De Vos via Digitalmars-d-learn

Is it possible to print runtime memory usage of:
-The stack
-The heap
-The garbage collector ?


Re: How to free memory ater use of "new" to allocate it.

2023-07-16 Thread Alain De Vos via Digitalmars-d-learn

The following program prints two different addresses.
Meaning the new allocates memory until the program dies.
So the means memory leak by default ?


```

import std.stdio:writefln;
import object: destroy;
import core.memory: GC;

void dofun(){
auto a=new int[1000];
writefln("%12x",);
destroy(a);
GC.free(a.ptr);
}

int main(){
dofun();
auto b=new int[1000];
writefln("%12x",);
return 0;
}

```



Re: How to free memory ater use of "new" to allocate it.

2023-07-16 Thread Steven Schveighoffer via Digitalmars-d-learn

On 7/16/23 2:41 PM, Alain De Vos wrote:

Is this ok ?
```
void main(){
 int[] i=new int[1];
     import object: destroy;
 destroy(i);
     import core.memory: GC;
 GC.free(GC.addrOf(cast(void *)(i.ptr)));
}
```


No, that won't work. Check out `i` value after you call `destroy` on it:

```d
destroy(i); // basically sets i = null
assert(i.ptr is null); // yep
GC.free(i.ptr); // basically free(null) which is a no-op
```

Also note that `destroy` is *shallow*. It does not dig into pointers or 
arrays. So even if your array was of elements with a destructor, 
destroying the array doesn't destroy the elements.


In this case, all you need to do is:

```d
GC.free(GC.addrOf(i.ptr));
```
You don't need the cast here. You shouldn't need the addrOf, but this is 
still open: https://issues.dlang.org/show_bug.cgi?id=13558


-Steve


Re: How to free memory ater use of "new" to allocate it.

2023-07-16 Thread Richard (Rikki) Andrew Cattermole via Digitalmars-d-learn

Yes.

For basic types like int's, you don't need to destroy the array.

As long as you don't slice the array and store that in i, you don't need 
to call addrOf too.


Re: How to free memory ater use of "new" to allocate it.

2023-07-16 Thread Alain De Vos via Digitalmars-d-learn

Is this ok ?
```
void main(){
int[] i=new int[1];
import object: destroy;
destroy(i);
import core.memory: GC;
GC.free(GC.addrOf(cast(void *)(i.ptr)));
}
```


Re: How to free memory ater use of "new" to allocate it.

2023-07-16 Thread Richard (Rikki) Andrew Cattermole via Digitalmars-d-learn

You can do it with ``GC.free``.

But you really shouldn't need to (it won't automatically release it back 
to OS).


https://dlang.org/phobos/core_memory.html#.GC.free


Re: Initializing an associative array into a variable when it is created

2023-07-16 Thread Alexander Zhirov via Digitalmars-d-learn

On Sunday, 16 July 2023 at 11:16:55 UTC, Danilo wrote:

Would a static constructor be okay? This way the static data
is not initialized at every `new` and object creation is faster.


Alternatively, i can think about your proposal. At the moment, I 
have solved my problem using the following method:


```d
@property string[][string] arr() {
return [
"one": ["abc", "def"],
"two": ["ghi", "jkl"],
"three": ["mno", "pqr"]
];
}
```

I don't know how true it will be considered. After all, the 
function creates an array every time it is called, which is not 
correct.


Re: Initializing an associative array into a variable when it is created

2023-07-16 Thread Danilo via Digitalmars-d-learn
But is there really no other way to immediately point a static 
array to a variable?


Looks like this is not implemented yet:

- https://dlang.org/spec/hash-map.html#static_initialization

Would a static constructor be okay? This way the static data
is not initialized at every `new` and object creation is faster.

- https://dlang.org/spec/class.html#static-constructor

```d
import std.stdio;

class C {
static string[][string] arr;

static this() { // static constructor
arr = [
"one": ["abc", "def"],
"two": ["ghi", "jkl"],
"three": ["mno", "pqr"]
];
}

this() { // object constructor
}

void show(string value) {
writeln(
value,
": ",
arr[value],
": ",
arr[value][0],
", ",
arr[value][1]
);
}
}

void main() {
auto var = new C;
var.show("one");
var.show("two");
var.show("three");
}
```



Re: Initializing an associative array into a variable when it is created

2023-07-16 Thread Alexander Zhirov via Digitalmars-d-learn

On Saturday, 15 July 2023 at 23:34:22 UTC, Danilo wrote:

Works fine, if you add a semicolon at the end.


I'm sorry. I didn't put the question quite correctly. Yes, this 
is how the array is initialized. I'm trying to describe it all in 
a class. I.e. I need to create a variable in the class that will 
be initialized when creating an object. I understand that it 
needs to be described in the constructor. But is there really no 
other way to immediately point a static array to a variable?