Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2021-06

Remove last character from pchar

5 messages · 3 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Darius Blaszyk

Hi,

I am a bit struggling with what should be fairly simple. I have a pchar where I am adding and removing characters. What I did so far is to allocate new memory every time and copying the source pchar over to a new one to append or delete a character. This required two helper variables in order to allow for the swap. This does not seem to be very efficient to me and I was wondering if I am missing anything?

What also confused me in the beginning that assigning a string to a pchar automatically assigns memory for that pchar. Is there a way to control this? e.g. to switch this off and generate a compiler error or runtime error when this happens? I'd like to prevent runtime errors or memory leaks instead.

Thank you very much!

Rgds, Darius

Reply by email

James Richters
to Darius Blaszyk

Pchars are just a pain, I do not use any Pchars.. instead I use AnsiStrings that I can manipulate in a way that is easy and then I just do Pchar(myvariable) whenever I need to use a function that needs a Pchar… like this:

Var
   MessageBoxText:Ansistring;

..
..
windows.messagebox(0,pchar(MessageBoxtext)…..bla bla bla);

There is probably a better way but for me conversion to Pchar is a small price to pay for easy string manipulation. AnsiStrings take care of their own memory allocation and the Pchar conversion doesn’t require an actual Pchar variable to bother with managing.

James

Reply by email

James Richters
to James Richters

Also StrPCopy will take the contents of an existing PChar and put it in an AnsiString… And sometimes I have regular pascal strings I want to pass to something as a Pchar, so then I do: Pchar(AnsiString(mystring));
But lately I’ve been just making all strings AnsiStrings anyway.

James

Reply by email

Jean SUZINEAU
to Darius Blaszyk: I am a bit struggling with what should be fairly simple. I have a pchar where I am adding…

I think you could just manage the end of your string with a #0 char.

Just allocate enough space for the maximum size of your string plus the terminal #0 char.

For example

program Project1;
{$mode objfpc}{$H+}
uses
   SysUtils, Classes;
var
    s: array[0..30] of char;
    p: PChar;
begin
      p:= @s;
      StrPLCopy( p, 'Test', 30);
      Writeln( p);
      s[3]:= #0;
      Writeln( p);
end.

produces

$ ./project1
Test
Tes
$

Reply by email

Jean SUZINEAU
to Jean SUZINEAU

Note: if your string is UTF8 encoded, the last "character" can be encoded, can span over 1 to 4 "Char", you need to know the length of your character.

For example:

program Project1;
{$mode objfpc}{$H+}
uses
   SysUtils, Classes;
var
    s: array[0..30] of char;
    p: PChar;
    i: Integer;
begin
      p:= @s;
      StrPLCopy( p, 'Test€a', 30);
      Writeln( p);
      for i:= 0 to 7
      do
        Write( ord(s[i]),':',s[i],' ');
      Writeln;
      s[5]:= #0;
      Writeln( p);
      for i:= 0 to 7
      do
        Write( ord(s[i]),':',s[i],' ');
      Writeln;
end.

gives

$ ./project1
Test€a
84:T 101:e 115:s 116:t 226:� 130:� 172:� 97:a Test�
84:T 101:e 115:s 116:t 226:� 0: 172:� 97:a
$

(the € character which spans over 3 chars is broken and so the end of the string becomes unreadable)

Reply by email