digitalmars.D.learn - Quick question
- Ruby The Roobster (5/5) Jan 29 2021 I have question here. Is there a difference between .sizeof and
- Ruby The Roobster (4/9) Jan 29 2021 Nevermind. This is junk. I was trying to get something to work,
- Adam D. Ruppe (6/8) Jan 29 2021 for future reference if someone stumbles on this, .sizeof is the
- Ruby The Roobster (3/11) Feb 01 2021 Thanks. However for a char[], .sizeof = .length because a char is
- Adam D. Ruppe (14/16) Feb 01 2021 Nope, char[].sizeof is a platform-specific constant not related
- =?UTF-8?Q?Ali_=c3=87ehreli?= (26/26) Feb 01 2021 While we're on topic, the size of a class type and a class variable both...
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
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
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
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:Thanks. However for a char[], .sizeof = .length because a char is one byte.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.
Feb 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.
Feb 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
Feb 01 2021