www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How can I get the total memory size of a Jagged Array

reply seany <seany uni-bonn.de> writes:
Hello


Let's assume, that we have a string array : string[] sa;

I fill up the array with random strings with random lengths.

I want to know how much space it occupies in memory.

I could go over the string array using a for loop, then another 
loop for each character, and then sum up char.sizeof. But this 
would be very slow and inefficient.

Is there a better way? I want a method that works for every 
variable type including classes and objects.

Thank you
Nov 13 2023
next sibling parent Alexandru Ermicioi <alexandru.ermicioi gmail.com> writes:
On Monday, 13 November 2023 at 14:15:31 UTC, seany wrote:
 Is there a better way? I want a method that works for every 
 variable type including classes and objects.

 Thank you
Perhaps, use length property of string. It should tell you length of a string and then you multiply it by size of char.
Nov 14 2023
prev sibling parent novice2 <sorryno em.ail> writes:
may be std.string.representation() may help?
https://dlang.org/phobos/std_string.html#.representation

byteSize = representation(myString).length;
Nov 14 2023