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

2023-07-17 Thread Ernesto Castellotti via Digitalmars-d-learn
On Monday, 17 July 2023 at 16:52:00 UTC, Alain De Vos wrote: This works also, ``` class C { int * pa; int [] a; // Constructor this() {writefln("Called constructor"); pa=cast(int *)malloc(1000*int.sizeof); a=pa[0..1000]; } } void

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

2023-07-17 Thread Alain De Vos via Digitalmars-d-learn
This works also, ``` class C { int * pa; int [] a; // Constructor this() {writefln("Called constructor"); pa=cast(int *)malloc(1000*int.sizeof); a=pa[0..1000]; } } void dofun() { scope x=new C; x.a[3]=5; writefln("%12x",); }

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

2023-07-17 Thread Nick Treleaven via Digitalmars-d-learn
On Sunday, 16 July 2023 at 18:18:08 UTC, Alain De Vos wrote: 12 │ ~this(){ 13 │ writeln("Free heap"); 14 │ import object: destroy; 15 │ import core.memory: GC; 16 │ i=null; // But How to force GC free ? Firstly, be careful with class

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

2023-07-17 Thread Steven Schveighoffer via Digitalmars-d-learn
On 7/16/23 11:58 PM, Alain De Vos wrote: 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;

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

2023-07-17 Thread Steven Schveighoffer via Digitalmars-d-learn
On 7/16/23 11:41 PM, Alain De Vos wrote: 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

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

2023-07-17 Thread Alain De Vos via Digitalmars-d-learn
Here is i use new, ``` import std.stdio:writefln; import object: destroy; import core.memory: GC; import core.stdc.stdlib: malloc,free; import std.typecons; class C { int * pa; int [] a; // Constructor this() {writefln("Called constructor"); pa=cast(int

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

2023-07-17 Thread drug007 via Digitalmars-d-learn
17.07.2023 15:45, Alain De Vos пишет: This works also: [snipped] Despite this time you use new you still allocate your class on stack using scope and its scope still is `dofun`. But I just want to inform you. Your solutions work. Don't get me wrong. N.B. I would say if you do not have

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

2023-07-17 Thread Alain De Vos via Digitalmars-d-learn
This works also: ``` import std.stdio:writefln; import object: destroy; import core.memory: GC; import core.stdc.stdlib: malloc,free; import std.typecons; class C { int * pa; int [] a; // Constructor this() {writefln("Called constructor"); pa=cast(int

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

2023-07-17 Thread drug007 via Digitalmars-d-learn
17.07.2023 13:17, Alain De Vos пишет: The following code works: ``` import std.stdio:writefln; import object: destroy; import core.memory: GC; import core.stdc.stdlib: malloc,free; import std.typecons; class C { int * pa; int [] a;     // Constructor     this()

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

2023-07-17 Thread Alain De Vos via Digitalmars-d-learn
The following code works: ``` import std.stdio:writefln; import object: destroy; import core.memory: GC; import core.stdc.stdlib: malloc,free; import std.typecons; class C { int * pa; int [] a; // Constructor this() {writefln("Called constructor");