Jump to page: 1 25  
Page
Thread overview
Classes on stack
Sep 01, 2022
Redwan
Sep 01, 2022
Dennis
Sep 01, 2022
Redwan
Sep 01, 2022
Dennis
Sep 01, 2022
Redwan
Sep 01, 2022
rikki cattermole
Sep 01, 2022
Redwan
Sep 01, 2022
bauss
Sep 01, 2022
Redwan
Sep 01, 2022
bauss
Sep 01, 2022
Redwan
Sep 01, 2022
Guillaume Piolat
Sep 01, 2022
bauss
Sep 01, 2022
Commander Zot
Sep 01, 2022
Redwan
Sep 01, 2022
rikki cattermole
Sep 01, 2022
Ali Çehreli
Sep 01, 2022
Paul Backus
Sep 02, 2022
Walter Bright
Sep 01, 2022
Redwan
Sep 01, 2022
rikki cattermole
Sep 01, 2022
Redwan
Sep 01, 2022
rikki cattermole
Sep 01, 2022
bauss
Sep 01, 2022
Johan
Sep 02, 2022
Walter Bright
Sep 02, 2022
Walter Bright
Sep 05, 2022
IGotD-
Sep 01, 2022
IGotD-
Sep 01, 2022
Redwan
Sep 01, 2022
Ali Çehreli
Sep 01, 2022
Redwan
Sep 01, 2022
mw
Sep 01, 2022
Paul Backus
Sep 01, 2022
IGotD-
Sep 01, 2022
Ali Çehreli
Sep 03, 2022
Nick Treleaven
Sep 01, 2022
IGotD-
Sep 01, 2022
dd
September 01, 2022

Hi all
I'm new to D from c, cpp and Rust.

this language made me surprised because of its features and syntax and etc.

but I shocked when I found that classes must be created only on stack! Is this nightmare real? why???
I want declare classes and create instance of them on stack and nicely use them. why I must put them on heap??
structs does not support inheritance and I don't want to use them. please tell me that is not true or give me some other ways. I don't want define objects always on heap. don't let me go back on cpp :( tnx!

September 01, 2022

On Thursday, 1 September 2022 at 09:13:56 UTC, Redwan wrote:

>

I want declare classes and create instance of them on stack and nicely use them.

You can use that using the scope storage class on a local variable with a class type:

https://dlang.org/spec/attribute.html#scope-class-var

void main() @nogc
{
    scope o = new Object();
}

The @nogc attribute ensures no allocations are made with the Garbage Collector.

September 01, 2022

On Thursday, 1 September 2022 at 09:13:56 UTC, Redwan wrote:

>

Hi all
I'm new to D from c, cpp and Rust.

this language made me surprised because of its features and syntax and etc.

but I shocked when I found that classes must be created only on stack! Is this nightmare real? why???
I want declare classes and create instance of them on stack and nicely use them. why I must put them on heap??
structs does not support inheritance and I don't want to use them. please tell me that is not true or give me some other ways. I don't want define objects always on heap. don't let me go back on cpp :( tnx!

This comes from the 90s old classes are reference type and structs are value types (which D shares with C#). I think this have been discussed previously in this forum and I have seen kind of vague answer about it. However, you can allocate a class on the stack using the scoped template. One more serious flaw to this is that classes are not expanded as members in the host class, which means that memory allocation and fragmentation is much worse than people think. It is possible to expand member classes into host classes using some kind of hack though.

For "multiple" inheritance, use composition with mixins which I assume you are after.

Basically, I don't buy why why classes must be reference types and must be allocated on the heap. I have not seen a good explanation to this, or I have not fully understood it.

September 01, 2022

On Thursday, 1 September 2022 at 09:40:06 UTC, Dennis wrote:

>

On Thursday, 1 September 2022 at 09:13:56 UTC, Redwan wrote:

>

I want declare classes and create instance of them on stack and nicely use them.

You can use that using the scope storage class on a local variable with a class type:

https://dlang.org/spec/attribute.html#scope-class-var

void main() @nogc
{
    scope o = new Object();
}

The @nogc attribute ensures no allocations are made with the Garbage Collector.

oh, thank you. with this, no allocation??

September 01, 2022

On Thursday, 1 September 2022 at 09:43:55 UTC, Redwan wrote:

>

oh, thank you. with this, no allocation??

No heap allocation. The @nogc attribute is enforced by the compiler, so you will get an error if you use new without scope here.

September 01, 2022

On Thursday, 1 September 2022 at 09:53:08 UTC, Dennis wrote:

>

On Thursday, 1 September 2022 at 09:43:55 UTC, Redwan wrote:

>

oh, thank you. with this, no allocation??

No heap allocation. The @nogc attribute is enforced by the compiler, so you will get an error if you use new without scope here.

OK tnx. another problem that probably is related to this issue.
I compiled this worthless code:

void main() {}

also this one:

@nogc void main() {}

the valgrind result:

...
total heap usage: 143 alloc, 141 frees, 13,236 bytes allocated
...

!!!
what's this means??
from where comes this allocations and also leaks???

if it's for GC, how to completely disable gc? I don't Wan use it at all. I searched for a compiler command line disabling for gc but not found. GC.disable() is ugly way I think.

September 01, 2022
On 01/09/2022 10:13 PM, Redwan wrote:
> if it's for GC, how to completely disable gc? I don't Wan use it at all. I searched for a compiler command line disabling for gc but not found. GC.disable() is ugly way I think.

As long as you want to use things like classes and Phobos, you're stuck with the GC being linked in and active at some point in the lifetime of your process.

D's runtime includes a lot of infrastructure such as threading that is initialized before your main ever executes.

You can limit the GC activity by things like GC.disable, but its still linked in and in use at some point in time.

D's GC is your friend, if you don't allocate it won't attempt to collect unless you want it to.
September 01, 2022

On Thursday, 1 September 2022 at 10:13:17 UTC, Redwan wrote:

>

On Thursday, 1 September 2022 at 09:53:08 UTC, Dennis wrote:

>

On Thursday, 1 September 2022 at 09:43:55 UTC, Redwan wrote:

>

oh, thank you. with this, no allocation??

No heap allocation. The @nogc attribute is enforced by the compiler, so you will get an error if you use new without scope here.

OK tnx. another problem that probably is related to this issue.
I compiled this worthless code:

void main() {}

also this one:

@nogc void main() {}

the valgrind result:

...
total heap usage: 143 alloc, 141 frees, 13,236 bytes allocated
...

!!!
what's this means??
from where comes this allocations and also leaks???

if it's for GC, how to completely disable gc? I don't Wan use it at all. I searched for a compiler command line disabling for gc but not found. GC.disable() is ugly way I think.

Use betterC if you want to avoid the D runtime.

September 01, 2022
On Thursday, 1 September 2022 at 10:21:32 UTC, rikki cattermole wrote:
>
> On 01/09/2022 10:13 PM, Redwan wrote:
>> if it's for GC, how to completely disable gc? I don't Wan use it at all. I searched for a compiler command line disabling for gc but not found. GC.disable() is ugly way I think.
>
> As long as you want to use things like classes and Phobos, you're stuck with the GC being linked in and active at some point in the lifetime of your process.
>
> D's runtime includes a lot of infrastructure such as threading that is initialized before your main ever executes.
>
> You can limit the GC activity by things like GC.disable, but its still linked in and in use at some point in time.
>
> D's GC is your friend, if you don't allocate it won't attempt to collect unless you want it to.

well I know it's my friend, but we're just friend :) I don't want to use it because it's uses alot of heap and I don't like it. my case is special and all of heap usages needs to be done by me. so I want to do something to avoid this dynamic and runtime linkages and etc and be pure and in my hand. definitely it must be some flags or configurations to disable this stuff for a system language. am I right?
September 01, 2022
On Thursday, 1 September 2022 at 10:51:44 UTC, Redwan wrote:
> On Thursday, 1 September 2022 at 10:21:32 UTC, rikki cattermole wrote:
>>
>> On 01/09/2022 10:13 PM, Redwan wrote:
>>> if it's for GC, how to completely disable gc? I don't Wan use it at all. I searched for a compiler command line disabling for gc but not found. GC.disable() is ugly way I think.
>>
>> As long as you want to use things like classes and Phobos, you're stuck with the GC being linked in and active at some point in the lifetime of your process.
>>
>> D's runtime includes a lot of infrastructure such as threading that is initialized before your main ever executes.
>>
>> You can limit the GC activity by things like GC.disable, but its still linked in and in use at some point in time.
>>
>> D's GC is your friend, if you don't allocate it won't attempt to collect unless you want it to.
>
> well I know it's my friend, but we're just friend :) I don't want to use it because it's uses alot of heap and I don't like it. my case is special and all of heap usages needs to be done by me. so I want to do something to avoid this dynamic and runtime linkages and etc and be pure and in my hand. definitely it must be some flags or configurations to disable this stuff for a system language. am I right?

I already gave you the solution one message above; betterC (A subset of D) is what you want.
« First   ‹ Prev
1 2 3 4 5