www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Quick question

reply Ruby The Roobster <michaeleverestc79 gmail.com> writes:
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.
Jan 29 2021
next sibling parent Ruby The Roobster <michaeleverestc79 gmail.com> writes:
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.
Jan 29 2021
prev sibling next sibling parent reply Adam D. Ruppe <destructionator gmail.com> writes:
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.
Jan 29 2021
parent reply Ruby The Roobster <michaeleverestc79 gmail.com> writes:
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.
Feb 01 2021
parent Adam D. Ruppe <destructionator gmail.com> writes:
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.
Feb 01 2021
prev sibling parent =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
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
Feb 01 2021