Thread overview
Quick question
Jan 30, 2021
Ruby The Roobster
Jan 30, 2021
Ruby The Roobster
Jan 30, 2021
Adam D. Ruppe
Feb 01, 2021
Ruby The Roobster
Feb 01, 2021
Adam D. Ruppe
Feb 01, 2021
Ali Çehreli
January 30, 2021
I have  question here. Is there a difference between .sizeof and .length(of a char[])? For example, let's say you have the following array: char[2][] members.
Is it possible for members[0].sizeof == members[1].sizeof but members[0].length != members[1].length? Thanks in advance.
January 30, 2021
On Saturday, 30 January 2021 at 00:58:09 UTC, Ruby The Roobster wrote:
> I have  question here. Is there a difference between .sizeof and .length(of a char[])? For example, let's say you have the following array: char[2][] members.
> Is it possible for members[0].sizeof == members[1].sizeof but members[0].length != members[1].length? Thanks in advance.

Nevermind. This is junk. I was trying to get something to work, but then I realized doing that was impossible. Ignore this now.
January 30, 2021
On Saturday, 30 January 2021 at 00:58:09 UTC, Ruby The Roobster wrote:
> I have  question here. Is there a difference between .sizeof and .length(of a char[])?

for future reference if someone stumbles on this, .sizeof is the static size of the reference, .length is the actual length of the array

you pretty rarely want array.sizeof in D.
February 01, 2021
On Saturday, 30 January 2021 at 01:57:53 UTC, Adam D. Ruppe wrote:
> On Saturday, 30 January 2021 at 00:58:09 UTC, Ruby The Roobster wrote:
>> I have  question here. Is there a difference between .sizeof and .length(of a char[])?
>
> for future reference if someone stumbles on this, .sizeof is the static size of the reference, .length is the actual length of the array
>
> you pretty rarely want array.sizeof in D.

Thanks. However for a char[], .sizeof = .length because a char is one byte.
February 01, 2021
On Monday, 1 February 2021 at 16:19:13 UTC, Ruby The Roobster wrote:
> Thanks. However for a char[], .sizeof = .length because a char is one byte.

Nope, char[].sizeof is a platform-specific constant not related to the length at all.

void main() {
        import std.stdio;
        char[] a = "test".dup;
        writeln(a.sizeof); // 8 on 32 bit, 16 on 64 bit independent of content
        writeln(a.length); // 4 because of the content "test"
}


With a static array sizeof and length would happen to match but a dynamic array is different. sizeof is the size of the length and pointer, not the content.
February 01, 2021
While we're on topic, the size of a class type and a class variable both are constant on a platform, e.g. 8 bytes on 64 bit systems. To get the size of actual instances (objects) of this type, one needs to use the classInstanceSize trait:

class C {
  int i;
}

void main() {
  auto a = new C();
  static assert (a.sizeof == C.sizeof);

  pragma(msg, "Size of the reference: ", a.sizeof);
  pragma(msg, "Size of the object: ", __traits(classInstanceSize, C));
}

Prints the following during compilation my system:

Size of the reference: 8LU
Size of the object: 20LU

20 is the sum of two hidden variables (the vtbl pointer and the monitor) and the member 'i'.

As I learned recently but never used yet, extern(C++) classes do not have the monitor member. So, the instances of the following type would be 12 on a 64 bit system:

extern (C++)
class C {
  int i;
}

Ali