[fpc-pascal] Problem with array of const

Tony Whyman tony.whyman at mccallumwhyman.com
Sun Oct 29 16:26:08 CET 2017


I've fallen into various traps with array of const before. I would 
always recommend that:

1. Test the vType before accessing the argument value and raise an 
exception if it is not what is expected. You are dealing with a free 
union and the compiler may not always do what you expect it to.

2. Values such as VAnsiString are best dealt with as PChars as otherwise 
you can quickly get into problems with reference counts.

For example:

program test_args;

procedure test(name: string; args: array of const);
begin
   writeln(name);

   case args[0].vType of

   vtAnsiString:
     writeln(strpas(PAnsiChar(args[0].VAnsiString)));
   else
     raise Exception.Create('Unexpected vtype');
   end;

end;

begin
   test('name', ['arg']);
end.

Use of "strpas" is probably unnecessary in the above, but it does 
illustrate the point.


On 28/10/17 23:59, Darius Blaszyk wrote:
> Consider the application below. When I run it I do get the following 
> output:
>
> name
> rg��������name�F&{00000000-0000-0000-C000-000000000046}
>
> In other words I lose the first character (a) from the arguments 
> supplied and the string returns with a lot of garbage. What am I doing 
> wrong here?
>
> Rgds, Darius
>
> program test_args;
>
> procedure test(name: string; args: array of const);
> begin
>   writeln(name);
>   writeln(args[0].VString^);
> end;
>
> begin
>   test('name', ['arg']);
> end.
>
>
> _______________________________________________
> fpc-pascal maillist  -  fpc-pascal at lists.freepascal.org
> http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20171029/6e867918/attachment.html>


More information about the fpc-pascal mailing list