Forum > Beginners

how to evaluate memorysize ocupied by string?

(1/3) > >>

alexraynepe196:
Hallow!
i need acces to string as bytes memory. i can trivial get a pointer to head - @s[1].
but is there portable way evaluate memory ocupied by string?have FPC RTL function on it?

Gustavo 'Gus' Carreno:
Hey Alex,

Well, it depends on what your strings contains: Simple ASCII, UTF8 or UTF16.

And it's not that trivial in any of the UTF(X) cuz under UTF8 not all chars are 2 bytes.

I think the unit LazUTF8 or LazUTF8Utils, something like that, has utilities to calculate these values.

But in the end, I think the usual Length(AString) will give you a value you can then use.

But again, you'll have to make sure there is no Unicode magic influencing that count.

Cheers,
Gus

howardpc:
This function for ansistrings comes from the LazDbgLog unit:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---function MemSizeString(const s: string): PtrUInt;begin  Result:=length(s);  if s<>'' then    inc(Result,SizeOf(Pointer)*4);end;

marcov:

--- Quote from: alexraynepe196 on November 13, 2021, 10:02:37 pm ---Hallow!
i need acces to string as bytes memory. i can trivial get a pointer to head - @s[1].
but is there portable way evaluate memory ocupied by string?have FPC RTL function on it?

--- End quote ---


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---result:=length(s);if result<>0 then  result:=result*sizeof(s[1]); // in case the string is wide- or unicode- 
That is just the payload. Howard goes more in the direction of the total allocation size, descriptor inclusive.

BobDog:

Would this not do?

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---{$mode delphi}{$T+}function size<T>(s:T):int64;beginexit((@s[length(s)])-(@s[1]));end;   vars:ansistring;begins:='abcdefghijklmnopqrstuvwxyz';writeln(size<ansistring>(s));writeln(size<unicodestring>('Σὲ γνωρίζω ἀπὸ τὴν κόψη'));writeln('Press enter to end . . .');readln;end.      

Navigation

[0] Message Index

[#] Next page

Go to full version