Sign in Join

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

Mailing lists / fpc-pascal / 2011-01

AnsiString

3 messages · 3 people · –

Reply by email Subscribe to this list Your subscription

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

Luis Fernando Del Aguila Mejía

The documentation
(http://www.freepascal.org/docs-html/prog/progsu146.html#x189-1990008.2.7) says:
-8 Longint current string with size.
-4 Longint with reference count.
But, when I want access to that structure, I have to do it backwards. -8 Longint with reference count.
-4 Longint current string with size.

{$codepage UTF8}
Var
  cad1:AnsiString;
  aux1:AnsiString;
  p:pointer;
Begin
  SetLength(cad1,8);
  p:=pointer(cad1);
  Writeln('memory address : ',longint(p));
  p:=p-4;
  Write('memory address : ',longint(p),'=');
//Must show reference count, but shows size
  Writeln(longint(p^));
  aux1:=cad1;
  p:=pointer(cad1);
  p:=p-8;
  Write('memory address : ',longint(p),'=');
//Must show Size, but shows reference count
  Writeln(longint(p^));
End.

Do these positions are different, depending on microprocessor being used ?

Thanks

David Emerson
to Luis Fernando Del Aguila Mejía
Luis Fernando Del Aguila Mejía wrote 10 lines
The documentation (http://www.freepascal.org/docs-html/prog/progsu146.html#x189-1990008.2.7) says: -8 Longint current string with size. -4 Longint with reference count. But, when I want access to that structure, I have to do it backwards. -8 Longint with reference count. -4 Longint current string with size. Do these positions are different, depending on microprocessor being used ?

I'm fairly certain that is an error in the documentation, and that it SHOULD always be as you discovered it to actually be.

As an aside, a tip for doing pointer arithmetic...

var
  s : ansistring;
  p : ptruint;
p := pointer(s);
dec(p);  // equivalent to p:=p-4 because p is a typed pointer.

Using typed pointers in this way will likely make it easier to adapt your code to a 64-bit conversion in the future.

Also note that, in this case, dec(p,2) would do p:=p-8, as (hopefully) expected

I use these types of constructs frequently, and it makes it much easier to change types, if that is ever necessary, or adapt constructs to new types.

Cheers,
David

Jürgen Hestermann
to Luis Fernando Del Aguila Mejía: The documentation (http://www.freepascal.org/docs-html/prog/progsu146.html#x189-1990008.2.…
p:=pointer(cad1); p:=p-8; Write('memory address : ',longint(p),'='); //Must show Size, but

shows reference count

Writeln(longint(p^));

I fail to see why you need to mess with the internals of is the reason to access size and reference count this way? At least for the size there is Length(cad1) which would be much clearer.

Although pointer arithmetic is possible in Pascal it looks like C. And you loose any help from the compiler to check types.