Signed word lengths and indexes

bearophile bearophileHUGS at lycos.com
Fri Jun 18 15:59:38 PDT 2010


Adam Ruppe:

> I don't think a destructor can free the mem
> of its own object.

I see and I'd like to know! :-)

By the way, this program shows your code is not a replacement of the operator overloading of the variable length struct itself I was talking about, because D structs can't have length zero (plus 3 bytes of padding, here):

import std.stdio: writeln, write;

struct TailArray(T) {
    T opIndex(size_t idx) {
        T* tmp = cast(T*)(&this) + idx;
        return *tmp;
    }

    T opIndexAssign(T value, size_t idx) {
        T* tmp = cast(T*)(&this) + idx;
        *tmp = value;
        return value;
    }
}

struct MyString1 {
    size_t size;
    TailArray!char data; // not the same as char data[0]; in C
}

struct MyString2 {
    size_t size;
    char[0] data;
}

void main() {
    writeln(MyString1.sizeof); // 8
    writeln(MyString2.sizeof); // 4
}

Bye,
bearophile


More information about the Digitalmars-d mailing list