Recent

Author Topic: PChar To String without memory allocate  (Read 647 times)

Okoba

  • Hero Member
  • *****
  • Posts: 555
PChar To String without memory allocate
« on: November 09, 2024, 03:30:07 pm »
Can I have an string link to a PChar? The PChar is safe and I want to have the string for easy to use work.
SetString copies the data of PChar. Is there any helpers for it or should I write the string memory structure myself?

Fibonacci

  • Hero Member
  • *****
  • Posts: 613
  • Internal Error Hunter
Re: PChar To String without memory allocate
« Reply #1 on: November 09, 2024, 03:44:05 pm »
No. Right before the string content is a structure with refcount, length and others. You cant alloc memory just right before your PAnsiChar to put TAnsiRec there, so reallocation is required anyway.

Okoba

  • Hero Member
  • *****
  • Posts: 555
Re: PChar To String without memory allocate
« Reply #2 on: November 09, 2024, 03:46:35 pm »
The PChar is zero ended, but comes from another lib, and memory is safe. I do not want to allocate a copy, I want to expose it as String for ease of use.

Fibonacci

  • Hero Member
  • *****
  • Posts: 613
  • Internal Error Hunter
Re: PChar To String without memory allocate
« Reply #3 on: November 09, 2024, 03:49:31 pm »
You cant. You must either move the entire PChar by SizeOf(TAnsiChar) losing the last SizeOf(TAnsiChar) and put the string  struct in front, or create a new string from it.

But if the lib wants you to pass a pointer to a buffer, you can of course use String: SetLength -> then pass @str[1]
« Last Edit: November 09, 2024, 03:51:43 pm by Fibonacci »

Thaddy

  • Hero Member
  • *****
  • Posts: 16199
  • Censorship about opinions does not belong here.
Re: PChar To String without memory allocate
« Reply #4 on: November 09, 2024, 03:52:19 pm »
The PChar is zero ended, but comes from another lib, and memory is safe. I do not want to allocate a copy, I want to expose it as String for ease of use.
And ansistrings are also zero ended, but that is not meant to prevent hardcasts, since C-style strings lack a length field and a refcount field at negative offset. So it is either the comfort of Pascal strings and a copy or pchar handling. You have to stick to Pchar handling if you want to avoid a copy.
(Shows why PChars are stupid)
« Last Edit: November 09, 2024, 04:00:01 pm by Thaddy »
If I smell bad code it usually is bad code and that includes my own code.

Okoba

  • Hero Member
  • *****
  • Posts: 555
Re: PChar To String without memory allocate
« Reply #5 on: November 09, 2024, 03:55:30 pm »
Thank you.

Thaddy

  • Hero Member
  • *****
  • Posts: 16199
  • Censorship about opinions does not belong here.
Re: PChar To String without memory allocate
« Reply #6 on: November 09, 2024, 04:10:14 pm »
Okoba,

It also depends on your type of code:
If your code heavily relies on the length of a string then it often more efficient to make a copy to a Pascal string (implied with a cast even) than using the proverbial strlen C-ism which is dead slow because it runs tthe length of the string on every occasion just to determine its length.
Pascal strings read just the length field, so can be much, much faster.
If I smell bad code it usually is bad code and that includes my own code.

Thaddy

  • Hero Member
  • *****
  • Posts: 16199
  • Censorship about opinions does not belong here.
Re: PChar To String without memory allocate
« Reply #7 on: November 09, 2024, 04:12:18 pm »
That does not mean it is sane. And it is not str but setstring that causes the issues..
Whatever you got that misconception from, I am sure that was a mistake:
https://www.freepascal.org/docs-html/rtl/system/str.html

The bottom line is that if you want to avoid copies, you need to stick to C-style handling and there is no way around that.
« Last Edit: November 09, 2024, 04:17:36 pm by Thaddy »
If I smell bad code it usually is bad code and that includes my own code.

Okoba

  • Hero Member
  • *****
  • Posts: 555
Re: PChar To String without memory allocate
« Reply #8 on: November 10, 2024, 11:12:35 am »
Thank you for clarification.

 

TinyPortal © 2005-2018