Forum > Beginners

PCHAR question

(1/3) > >>

ssawgift:
I suddenly had this question. If I understand correctly, Pascal strings are not null-terminated. When I do a type cast like this:
p := PCHAR(strvar)
What is going on behind the scene? Does the compiler automatically allocate a block of memory and copy the string to it and null-terminate it?

Thaddy:
Pascal strings are actually sometimes (mostly) zero terminated, but Pascal ansistrings are both zero terminated and can contain zero's anywhere in the string. except for shortstrings.
All PChar types behave the same as in e.g. C: They are zero terminated and do not allow zero's anywhere in the middle.
Pchar types are technically arrays with a zero terminator and not Pascal string types. Not Wirth's fault but K&R.
There are many examples on this forum. I wrote at least 10 small examples over the years. But:
--- 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";}};} ---{$H+} // ansi basics on. Shortstring does not need it.var   a:string = 'test me'#0'some more';begin  writeln(PChar(a)); // goofy style ( curly brackets )  writeln( a); // sensible style (Pascal)end.

Martin_fr:
More exact.

Pascal strings are not null terminated.  They can contain nulls, because they have a length field.

But Pascal Strings (at least long/ansi, to which you can apply the mentioned "pchar(s)" cast) do have a terminating #0. Only that is not within the string. It is the next byte after the end of the string. The compiler automatically allocates the extra space for each string, and always puts the #0 in there.

I am not sure if that is
- documented behaviour
- implementation detail. (which means it could change with no notice)

In either case, the "pchar(s)" cast is documented. And it will continue to work.

Mind also that


--- 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";}};} ---var s: ansistrnig;p: pchar;begin  p := pchar(s);  s := s + 'abc'; // at this point p may become a dangling pointer. I.e. p may point to any kind of trash in memory.

Thaddy:

--- Quote from: Martin_fr on November 26, 2022, 05:42:47 pm ---More exact.

Pascal strings are not null terminated.  They can contain nulls, because they have a length field.

--- End quote ---
Is not exact. All supported string types derived from Ansi - AnsiString, UnicodeString etc - are zero terminated. Only exception are shortstrings (where Delphi also terminates these documented with a zero)
See my example.
All of this is documented in the manuals except the zero terminator for shortstring. Which is, as you rightly noted, not necessary but that goes for all the supported Pascal string types.

Thaddy:

--- Quote from: Martin_fr on November 26, 2022, 05:42:47 pm ---More exact.

Pascal strings are not null terminated.  They can contain nulls, because they have a length field.

But Pascal Strings (at least long/ansi, to which you can apply the mentioned "pchar(s)" cast) do have a terminating #0. Only that is not within the string. It is the next byte after the end of the string. The compiler automatically allocates the extra space for each string, and always puts the #0 in there.

I am not sure if that is
- documented behaviour
- implementation detail. (which means it could change with no notice)

In either case, the "pchar(s)" cast is documented. And it will continue to work.

Mind also that


--- 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";}};} ---var s: ansistrnig;p: pchar;begin  p := pchar(s);  s := s + 'abc'; // at this point p may become a dangling pointer. I.e. p may point to any kind of trash in memory.
--- End quote ---
NO. because the compiler treats s as ansistring and concatination is OK.

Navigation

[0] Message Index

[#] Next page

Go to full version