Jump to page: 1 2
Thread overview
Class Field Size/Offsets
Mar 02, 2013
Maxime Chevalier
Mar 02, 2013
bearophile
Mar 02, 2013
Andrej Mitrovic
Mar 02, 2013
Maxime Chevalier
Mar 02, 2013
Maxime Chevalier
Mar 02, 2013
Walter Bright
Mar 02, 2013
Maxime Chevalier
Mar 02, 2013
Peter Alexander
Mar 02, 2013
Walter Bright
Mar 02, 2013
Andrej Mitrovic
Mar 03, 2013
Maxime Chevalier
March 02, 2013
To access class fields in machine code generated by my JIT compiler, I need to be able to know the offset and size of the said fields. It seems that D requires a pointer to an instance of the class to make this work. This seems rather problematic for my usage.

Is it safe to circumvent this by using a null pointer or could I potentially get an exception/segfault by doing this? E.g.:

MyClass ptr = null;

auto fSize = ptr.theField.sizeof;
auto fOffs = ptr.theField.offsetof;

Is it safe to assume that the offset is the same in every instance? I would assume so (why would the offsets change?) but I'd like to know if the language actually guarantees this.
March 02, 2013
Maxime Chevalier:

> It seems that D requires a pointer to an instance of the class to make this work.

I have related ER since a lot of time that has not received an answer in about three years:

http://d.puremagic.com/issues/show_bug.cgi?id=3939

Bye,
bearophile
March 02, 2013
On 3/2/13, Maxime Chevalier <maximechevalierb@gmail.com> wrote:
> To access class fields in machine code generated by my JIT compiler, I need to be able to know the offset and size of the said fields. It seems that D requires a pointer to an instance of the class to make this work.

Shouldn't the following work for you?

class C { int a; int[4] b; }

void main()
{
    pragma(msg, C.a.sizeof);
    pragma(msg, C.a.offsetof);
    pragma(msg, C.b.sizeof);
    pragma(msg, C.b.offsetof);
}
March 02, 2013
Your example works but my use case doesn't and I have no idea why:

class C { int a; }

struct Foo
{
    void foo(string className, string fieldName)()
    {
        mixin("auto sz = " ~ className ~ "." ~ fieldName ~ ".sizeof;");

        writefln("sz: %s", sz);
    }
}

static this()
{
    Foo f;

    f.foo!("C", "a");
}

Produces:

Error: this for a needs to be type C not type Foo
Error: template instance Foo.foo!("C", "a") error instantiating
March 02, 2013
The problem persists without the mixin and the template:

class C { int a; }

struct Foo
{
    void foo()
    {
        auto sz = C.a.sizeof;
        writefln("sz: %s", sz);
    }
}

static this()
{
    Foo f;
    f.foo();
}

Error: this for a needs to be type C not type Foo
March 02, 2013
On 3/2/2013 2:10 PM, Maxime Chevalier wrote:
> The problem persists without the mixin and the template:

It compiles without complaint for this code:
--------------------------------------------
import std.stdio;

class C { int a; }

struct Foo
{
    void foo()
    {
        auto sz = C.a.sizeof;
        writefln("sz: %s", sz);
    }
}

static this()
{
    Foo f;
    f.foo();
}

void main() { }
-----------------------------------------
March 02, 2013
I'm guessing you use a bleeding edge version in which the bug has been fixed then. It doesn't work on DMD64 v2.062.
March 02, 2013
On Saturday, 2 March 2013 at 22:25:24 UTC, Maxime Chevalier wrote:
> I'm guessing you use a bleeding edge version in which the bug has been fixed then. It doesn't work on DMD64 v2.062.

Ditto: http://dpaste.dzfl.pl/8bf3cc73
March 02, 2013
On 3/2/2013 2:25 PM, Maxime Chevalier wrote:
> I'm guessing you use a bleeding edge version in which the bug has been fixed
> then. It doesn't work on DMD64 v2.062.

You're correct.
March 02, 2013
On Saturday, 2 March 2013 at 22:10:57 UTC, Maxime Chevalier wrote:
> The problem persists without the mixin and the template:
>
> class C { int a; }
>
> struct Foo
> {
>     void foo()
>     {
>         auto sz = C.a.sizeof;
>         writefln("sz: %s", sz);
>     }
> }
>
> static this()
> {
>     Foo f;
>     f.foo();
> }
>
> Error: this for a needs to be type C not type Foo

For 2.062 you're going to have to move the .sizeof expression
outside of any methods which require 'this'. Make 'foo' static or
wrap the .sizeof expression inside of a template and it will
work. E.g.:

template getSizeOf(alias symb)
{
     enum getSizeOf = symb.sizeof;
}

struct Foo
{
     void foo()
     {
         auto sz = getSizeOf!(C.a);
         writefln("sz: %s", sz);
     }
}

It's just a bug which will be fixed in the next version.
« First   ‹ Prev
1 2