Jump to page: 1 2
Thread overview
Classes or stucts :: Newbie
Dec 19, 2010
David Currie
Dec 19, 2010
Denis Koroskin
Dec 19, 2010
bearophile
Dec 19, 2010
Denis Koroskin
Dec 19, 2010
bearophile
Dec 19, 2010
bearophile
Dec 19, 2010
Andrej Mitrovic
Dec 19, 2010
bearophile
Dec 19, 2010
Andrej Mitrovic
Dec 19, 2010
bearophile
Dec 19, 2010
Joost 't Hart
Dec 19, 2010
Nick Voronin
Dec 19, 2010
Jonathan M Davis
Dec 19, 2010
Jonathan M Davis
December 19, 2010
I am new to D (like many have done C++ , Java ).

Can a class be instantiated on the stack ?

eg

class C
{
  private  int _I1;
  private  int _I2;

  public:

  this(int pI) // constructor
  {
    _I1 = pI;
    _I2 = pI + 1;
  }

// ...  other methods etc
}

void f()  // just a function
{

  C myC(3);  // C++ syntax BUT is there a d equivalent

}

It appears that D ASSUMES myC is really a myC*(in C++)

and therefore requires

C myC = new C(3);
// but this ALWAYS requires calling the memory allocator
// this is what Java does (forces your Class instance onto the Heap)

Is there any way in D to instantiate a stack object ?

Will a struct do?

Does a struct have a constructor (as opposed to an opcall?)

I would be very grateful for a response.

David Currie


December 19, 2010
On Mon, 20 Dec 2010 18:00:31 +0300, David Currie <curriedr@iinet.net.au> wrote:

> I am new to D (like many have done C++ , Java ).
>
> Can a class be instantiated on the stack ?
>
> eg
>
> class C
> {
>    private  int _I1;
>    private  int _I2;
>
>    public:
>
>    this(int pI) // constructor
>    {
>      _I1 = pI;
>      _I2 = pI + 1;
>    }
>
> // ...  other methods etc
> }
>
> void f()  // just a function
> {
>
>    C myC(3);  // C++ syntax BUT is there a d equivalent
>
> }
>
> It appears that D ASSUMES myC is really a myC*(in C++)
>
> and therefore requires
>
> C myC = new C(3);
> // but this ALWAYS requires calling the memory allocator
> // this is what Java does (forces your Class instance onto the Heap)
>
> Is there any way in D to instantiate a stack object ?
>
> Will a struct do?
>
> Does a struct have a constructor (as opposed to an opcall?)
>
> I would be very grateful for a response.
>
> David Currie
>
>

Try the following:

scope c = new C(3);

Unfortunately last I've heard it's going to be deprecated in favor of a library solution:

InSitu!(C) c = InSitu!(C)(3); // IIRC not implemented yet
December 19, 2010
On Mon, 20 Dec 2010 18:00:31 +0300, David Currie <curriedr@iinet.net.au> wrote:

> I am new to D (like many have done C++ , Java ).
>
> Can a class be instantiated on the stack ?
>
> eg
>
> class C
> {
>    private  int _I1;
>    private  int _I2;
>
>    public:
>
>    this(int pI) // constructor
>    {
>      _I1 = pI;
>      _I2 = pI + 1;
>    }
>
> // ...  other methods etc
> }
>
> void f()  // just a function
> {
>
>    C myC(3);  // C++ syntax BUT is there a d equivalent
>
> }
>
> It appears that D ASSUMES myC is really a myC*(in C++)
>
> and therefore requires
>
> C myC = new C(3);
> // but this ALWAYS requires calling the memory allocator
> // this is what Java does (forces your Class instance onto the Heap)
>
> Is there any way in D to instantiate a stack object ?
>
> Will a struct do?
>
> Does a struct have a constructor (as opposed to an opcall?)
>
> I would be very grateful for a response.
>
> David Currie
>
>

To your second question, yes, structs would do:

struct C
{
    this(int i) { ... }
}

C c = C(3);
December 19, 2010
David Currie:

> I am new to D (like many have done C++ , Java ).

Welcome to D :-)
What language do you refer to, D1 or D2? The answers here are about the latest versions of D2.


> Can a class be instantiated on the stack ?

There was a way built in the language to do that (using "scope", it works still but it's deprecated), but now you have to use the std library. See the std.conv.emplace, but keep in mind it has issues (more than the issues of "scope"!) like I think not calling the object destructor.


> It appears that D ASSUMES myC is really a myC*(in C++)

Right, in D class instances are always managed by reference, even when you allocate them on the stack using the emplace trick.


> // but this ALWAYS requires calling the memory allocator
> // this is what Java does (forces your Class instance onto the Heap)

A difference is that the Oracle Java Garbage Collector has a Eden memory for the newly allocated objects that's much faster than the current D GC :-)


> Will a struct do?

Sometimes a struct is enough. D structs are managed by value or by pointer, but they don't support inheritance. In D structs and classes are different (like the memory layout of a class instance is decided by the compiler, while struct fields are in memory as you want them, with the alignment you desire) and they are used for different purposes.


> Does a struct have a constructor (as opposed to an opcall?)

In D2 the struct constructor is this() as for classes.

Bye,
bearophile
December 19, 2010
Denis Koroskin:

> Unfortunately last I've heard it's going to be deprecated in favor of a library solution:
> 
> InSitu!(C) c = InSitu!(C)(3); // IIRC not implemented yet

It's named scoped, see about its problems: http://d.puremagic.com/issues/show_bug.cgi?id=5115

Bye,
bearophile
December 19, 2010
> See the std.conv.emplace,

Sorry, see std.typecons.scoped and its problems: http://d.puremagic.com/issues/show_bug.cgi?id=5115

Bye,
bearophile
December 19, 2010
On 12/20/2010 04:00 PM, David Currie wrote:

> I am new to D (like many have done C++ , Java ).

Me too. Let's see what we can figure out together :-)

>
> Can a class be instantiated on the stack ?
>
> eg
>
> class C
> {
> private int _I1;
> private int _I2;
>
> public:
>
> this(int pI) // constructor
> {
> _I1 = pI;
> _I2 = pI + 1;
> }
>
> // ... other methods etc
> }
>
> void f() // just a function
> {
>
> C myC(3); // C++ syntax BUT is there a d equivalent
>
> }
>
> It appears that D ASSUMES myC is really a myC*(in C++)

It is not so much a *, but reference semantics (like T& param in C++).

>
> and therefore requires
>
> C myC = new C(3);
> // but this ALWAYS requires calling the memory allocator
> // this is what Java does (forces your Class instance onto the Heap)
>
> Is there any way in D to instantiate a stack object ?

Not a class object. Only values are on the stack. The reference semantics requires a "living" entity to refer to.

>
> Will a struct do?

Yes, if you so wish, but new also works. Structs have value semantics.

>
> Does a struct have a constructor (as opposed to an opcall?)

You can define one, but you cannot redefine the default this() constructor. Because structs have value semantics, D wants to make sure that T.init member values are slotted in by default. Note that T.init for a class type is the null reference.

>
> I would be very grateful for a response.

Hope this helps a bit.
Cheers,
Joost.

>
> David Currie
>
>

December 19, 2010
There's also scoped() in std.typecons, but I think this will still
allocate on the heap. Not sure..

On 12/19/10, bearophile <bearophileHUGS@lycos.com> wrote:
> David Currie:
>
>> I am new to D (like many have done C++ , Java ).
>
> Welcome to D :-)
> What language do you refer to, D1 or D2? The answers here are about the
> latest versions of D2.
>
>
>> Can a class be instantiated on the stack ?
>
> There was a way built in the language to do that (using "scope", it works still but it's deprecated), but now you have to use the std library. See the std.conv.emplace, but keep in mind it has issues (more than the issues of "scope"!) like I think not calling the object destructor.
>
>
>> It appears that D ASSUMES myC is really a myC*(in C++)
>
> Right, in D class instances are always managed by reference, even when you allocate them on the stack using the emplace trick.
>
>
>> // but this ALWAYS requires calling the memory allocator
>> // this is what Java does (forces your Class instance onto the Heap)
>
> A difference is that the Oracle Java Garbage Collector has a Eden memory for the newly allocated objects that's much faster than the current D GC :-)
>
>
>> Will a struct do?
>
> Sometimes a struct is enough. D structs are managed by value or by pointer, but they don't support inheritance. In D structs and classes are different (like the memory layout of a class instance is decided by the compiler, while struct fields are in memory as you want them, with the alignment you desire) and they are used for different purposes.
>
>
>> Does a struct have a constructor (as opposed to an opcall?)
>
> In D2 the struct constructor is this() as for classes.
>
> Bye,
> bearophile
>
December 19, 2010
On 12/19/10, bearophile <bearophileHUGS@lycos.com> wrote:
>> See the std.conv.emplace,
>
> Sorry, see std.typecons.scoped and its problems: http://d.puremagic.com/issues/show_bug.cgi?id=5115
>
> Bye,
> bearophile
>

Is this another bug?:

import std.stdio;
import std.typecons;

class A
{
    ~this()
    {
        writeln("dtor");
    }
}

void main()
{
    {
        A a1 = scoped!A();       // doesn't call dtor after the scope
        auto a2 = scoped!A();  // calls dtor after the scope
    }
}
December 19, 2010
Andrej Mitrovic:

> There's also scoped() in std.typecons, but I think this will still
> allocate on the heap. Not sure..

It allocates on the stack.

Bye,
bearophile
« First   ‹ Prev
1 2