Recent

Author Topic: how to evaluate memorysize ocupied by string?  (Read 2042 times)

alexraynepe196

  • New Member
  • *
  • Posts: 21
how to evaluate memorysize ocupied by string?
« 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?

Gustavo 'Gus' Carreno

  • Hero Member
  • *****
  • Posts: 838
  • Professional amateur ;-P
Re: how to evaluate memorysize ocupied by string?
« Reply #1 on: November 13, 2021, 10:57:12 pm »
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
Lazarus 2.3.0(trunk) FPC 3.3.1(trunk) Ubuntu 21.10 64b Dark Theme
Lazarus 2.0.12(stable) FPC 3.2.2(stable) Ubuntu 21.10 64b Dark Theme
http://github.com/gcarreno

howardpc

  • Hero Member
  • *****
  • Posts: 3860
Re: how to evaluate memorysize ocupied by string?
« Reply #2 on: November 13, 2021, 11:17:31 pm »
This function for ansistrings comes from the LazDbgLog unit:
Code: Pascal  [Select][+][-]
  1. function MemSizeString(const s: string): PtrUInt;
  2. begin
  3.   Result:=length(s);
  4.   if s<>'' then
  5.     inc(Result,SizeOf(Pointer)*4);
  6. end;

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 9778
  • FPC developer.
Re: how to evaluate memorysize ocupied by string?
« Reply #3 on: November 13, 2021, 11:19:08 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?

Code: Pascal  [Select][+][-]
  1. result:=length(s);
  2. if result<>0 then
  3.   result:=result*sizeof(s[1]); // in case the string is wide- or unicode-
  4.  

That is just the payload. Howard goes more in the direction of the total allocation size, descriptor inclusive.

BobDog

  • Full Member
  • ***
  • Posts: 183
Re: how to evaluate memorysize ocupied by string?
« Reply #4 on: November 14, 2021, 01:24:12 am »

Would this not do?
Code: Pascal  [Select][+][-]
  1. {$mode delphi}
  2. {$T+}
  3. function size<T>(s:T):int64;
  4. begin
  5. exit((@s[length(s)])-(@s[1]));
  6. end;
  7.  
  8.  
  9.  
  10. var
  11. s:ansistring;
  12. begin
  13. s:='abcdefghijklmnopqrstuvwxyz';
  14. writeln(size<ansistring>(s));
  15. writeln(size<unicodestring>('Σὲ γνωρίζω ἀπὸ τὴν κόψη'));
  16. writeln('Press enter to end . . .');
  17. readln;
  18. end.    
  19.  
  20.  

winni

  • Hero Member
  • *****
  • Posts: 2862
Re: how to evaluate memorysize ocupied by string?
« Reply #5 on: November 14, 2021, 10:35:32 am »
Hi!

Even if you got only 1 GB RAM on your computer a string of 100 bytes occupies

0.00001%

And:

"Σὲ γνωρίζω ἀπὸ τὴν κόψη"

national anthems are not wellcome - even if it is the greek.

Winni


BobDog

  • Full Member
  • ***
  • Posts: 183
Re: how to evaluate memorysize ocupied by string?
« Reply #6 on: November 14, 2021, 10:57:44 am »

I couldn't translate that Greek sentence, I tried a few sites.
But it was on the net as a unicode sample.
I still have no clue to what it is in English.
I hope no Greek or other members are offended, and it is written in a code string after all, not the open forum.

Thaddy

  • Hero Member
  • *****
  • Posts: 11149
Re: how to evaluate memorysize ocupied by string?
« Reply #7 on: November 14, 2021, 01:19:07 pm »
https://youtu.be/aYdIzTnjRnY

Ok it is not old Greek which is much easier to me.
You have morons, I am one, and you have morons squared.

af0815

  • Hero Member
  • *****
  • Posts: 845
Re: how to evaluate memorysize ocupied by string?
« Reply #8 on: November 14, 2021, 04:24:26 pm »
If "η κόψη" is 'an edge', i understand it.
regards
Andreas

winni

  • Hero Member
  • *****
  • Posts: 2862
Re: how to evaluate memorysize ocupied by string?
« Reply #9 on: November 14, 2021, 05:34:40 pm »
Hi

As Thaddy and 08/15 are curious:
Here is the english translation of the greek national anthem.
158 stanzas ......


https://greekreporter.com/2021/10/28/greek-national-anthem-greece-hymn-to-liberty/


Winni

BobDog

  • Full Member
  • ***
  • Posts: 183
Re: how to evaluate memorysize ocupied by string?
« Reply #10 on: November 14, 2021, 06:44:58 pm »

In that case, winni, my unicode sentence was completely benign, it just happened to be the first line of the anthem. Don't pull me up again for nothing.


af0815

  • Hero Member
  • *****
  • Posts: 845
Re: how to evaluate memorysize ocupied by string?
« Reply #11 on: November 14, 2021, 07:52:07 pm »
In the past i use for UTF-8 the function UTF8Length, see https://forum.lazarus.freepascal.org/index.php?topic=42515.0 for length and UTF8length.
But if the source string is UTF-16 (like Delphi often use it) or UTF-32 you get not the correct result. So first you must know in what kind of format the sourcestring is. I have in the past problems with libraries, i got a wrong result, because the lib was build with Delphi using UTF-16.
regards
Andreas

alexraynepe196

  • New Member
  • *
  • Posts: 21
Re: how to evaluate memorysize ocupied by string?
« Reply #12 on: November 14, 2021, 07:58:22 pm »

Would this not do?
Code: Pascal  [Select][+][-]
  1. {$mode delphi}
  2. {$T+}
  3. function size<T>(s:T):int64;
  4. begin
  5. exit((@s[length(s)])-(@s[1]));
  6. end;
  7. ...
  8.  

this looks fine. Do you use it?

winni

  • Hero Member
  • *****
  • Posts: 2862
Re: how to evaluate memorysize ocupied by string?
« Reply #13 on: November 14, 2021, 08:14:48 pm »

In that case, winni, my unicode sentence was completely benign, it just happened to be the first line of the anthem. Don't pull me up again for nothing.

Dont't  copy text where you did not even try to understand.

That can be dangerous.

 

TinyPortal © 2005-2018