Recent

Author Topic: Appending a Char onto a PChar  (Read 2581 times)

syntonica

  • Full Member
  • ***
  • Posts: 120
Re: Appending a Char onto a PChar
« Reply #15 on: December 24, 2019, 07:01:59 pm »
X should be null-terminated.
No, that is basically to prevent accidents by C programmers.
There is really no other reason to have a terminating zero in Pascal.
Pascal string types can even contain #0's in the middle of a string.
It is just that C programmers and API's do not understand that.
Code: Pascal  [Select][+][-]
  1. {$H+}
  2. var s: string = 'Merry'#0', Christmas';
  3. begin
  4.  writeln(PChar(s));
  5.  writeln(s);
  6. end.
Merry Christmas.... 8-)
Which is all fun and party balloons until you pass s to a C-function. Merry.

syntonica

  • Full Member
  • ***
  • Posts: 120
Re: Appending a Char onto a PChar
« Reply #16 on: December 24, 2019, 07:09:50 pm »
Please note that due to compatibility reasons Pascal generates runtime errors. If you want to have better named errors you need to include the SysUtils unit to your uses clause so that the exception handling mechanism is hooked. In that case you'll then get an EAccessViolation:
Thanks you! I couldn't figure out why my plug in was exiting at all, and taking the host with it, but O so silently! Tracing errors step by step just isn't fun, although I'm becoming quite adept at lldb sans the Xcode front end.


I'll have to check for SysUtils. I thought my main class invoked it, but it might be a different class in a different source...

syntonica

  • Full Member
  • ***
  • Posts: 120
Re: Appending a Char onto a PChar
« Reply #17 on: December 24, 2019, 07:12:45 pm »
My plugin kept politely exiting with a code 217, which apparently means Bye! and No, I'm not giving you an error report! I think it's right up there with Java's "Object is not an instance of an Object." Can't anyone just say that it's a nil pointer?  ::)
Not always, access to uninitialized memory area will generate the same RTE. Compile with -gl to get stacktrace when that access violation happens.
Okay, maybe not a nil pointer, but still uninitialized. Thank you for the tip!

PascalDragon

  • Hero Member
  • *****
  • Posts: 2578
  • Compiler Developer
Re: Appending a Char onto a PChar
« Reply #18 on: December 24, 2019, 07:51:25 pm »
X should be null-terminated.
No, that is basically to prevent accidents by C programmers.
There is really no other reason to have a terminating zero in Pascal.
Pascal string types can even contain #0's in the middle of a string.
It is just that C programmers and API's do not understand that.
Code: Pascal  [Select][+][-]
  1. {$H+}
  2. var s: string = 'Merry'#0', Christmas';
  3. begin
  4.  writeln(PChar(s));
  5.  writeln(s);
  6. end.
Merry Christmas.... 8-)
Which is all fun and party balloons until you pass s to a C-function. Merry.
As long as the C-function does not modify it, it will work. Otherwise you can use StrNew(PChar(YourStr)) to retrieve a copied PChar of your string.

I'll have to check for SysUtils. I thought my main class invoked it, but it might be a different class in a different source...
It is enough if at least one unit uses the SysUtils unit.

syntonica

  • Full Member
  • ***
  • Posts: 120
Re: Appending a Char onto a PChar
« Reply #19 on: December 24, 2019, 08:35:24 pm »
As long as the C-function does not modify it, it will work. Otherwise you can use StrNew(PChar(YourStr)) to retrieve a copied PChar of your string.
Ah. My favorite scourge of the computer world, where somebody thinks that they know better than the end-user and try to be "helpful." If it does work, then either it's mutating my string behind my back by changing the #0 to an unprintable char, or it's making a copy of the string, leaving out the forbidden chars, where I'm trying to avoid unnecessary copies. Either way is bad behavior. Explicit casting should be required here to do these things. I understand that at its core, it's a string backed by a fat pointer with count, which I'm all for. Usually. But I have a speed-critical use case where I must manipulate C-strings directly.

Anyway, I've got my string situation sorted out for now, for the most part. Optimization must wait until I get everything working properly. My next battle is with inheritance and pointers that end up pointing to the parent and not the child class... I've got ugly, explicit pointer casts littered all over for now plus unexpected nil pointers. The VST framework has many warts.

valdir.marcos

  • Hero Member
  • *****
  • Posts: 1018
Re: Appending a Char onto a PChar
« Reply #20 on: December 24, 2019, 09:32:21 pm »
As long as the C-function does not modify it, it will work. Otherwise you can use StrNew(PChar(YourStr)) to retrieve a copied PChar of your string.
Ah. My favorite scourge of the computer world, where somebody thinks that they know better than the end-user and try to be "helpful." If it does work, then either it's mutating my string behind my back by changing the #0 to an unprintable char, or it's making a copy of the string, leaving out the forbidden chars, where I'm trying to avoid unnecessary copies. Either way is bad behavior. Explicit casting should be required here to do these things. I understand that at its core, it's a string backed by a fat pointer with count, which I'm all for. Usually. But I have a speed-critical use case where I must manipulate C-strings directly.

Anyway, I've got my string situation sorted out for now, for the most part. Optimization must wait until I get everything working properly. My next battle is with inheritance and pointers that end up pointing to the parent and not the child class... I've got ugly, explicit pointer casts littered all over for now plus unexpected nil pointers. The VST framework has many warts.
Why don't you just use a simple Pointer to an Array of Char just like C and first implementations of Pascal did in the 1970's?
https://wiki.freepascal.org/Character_and_string_types

syntonica

  • Full Member
  • ***
  • Posts: 120
Re: Appending a Char onto a PChar
« Reply #21 on: December 24, 2019, 10:29:55 pm »
Why don't you just use a simple Pointer to an Array of Char just like C and first implementations of Pascal did in the 1970's?
https://wiki.freepascal.org/Character_and_string_types
I was just trying to avoid reinventing the wheel here.  Null-terminated strings are responsible for a vast majority of evil in the computing world. C should've gone with Pascal strings.

winni

  • Hero Member
  • *****
  • Posts: 2113
Re: Appending a Char onto a PChar
« Reply #22 on: December 25, 2019, 01:02:17 am »
Hi!

And runtime error 217 just means that the exceptions were  (not yet) loaded. So place sysutils always very early in the uses. And don't forget it.

And why not invent the wheel again? The Aztek would have been happy about that ...

Winni


PascalDragon

  • Hero Member
  • *****
  • Posts: 2578
  • Compiler Developer
Re: Appending a Char onto a PChar
« Reply #23 on: December 25, 2019, 12:07:10 pm »
And runtime error 217 just means that the exceptions were  (not yet) loaded. So place sysutils always very early in the uses. And don't forget it.
Please note that any runtime error means that exceptions aren't hooked up.
Also as long as no exception is happening in one of the initialization sections it doesn't matter where the SysUtils unit is loaded.

 

TinyPortal © 2005-2018