Better way to convert short string to pchar?

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Better way to convert short string to pchar?

Ryan Joseph
I would like short strings so I can do concatenation easily but I need to pass pchar to a C function. Is there anyway to do this on the stack instead of allocating a string each time? I feel like it was possible to do some type casting but I don’t remember how.

pname := StrAlloc(Length(name) + 1);
StrPCopy(pname, name);
result := glGetUniformLocation(programID, pname);
StrDispose(pname);

Regards,
        Ryan Joseph

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Better way to convert short string to pchar?

Marco van de Voort
In our previous episode, Ryan Joseph said:
> I would like short strings so I can do concatenation easily but I need to pass pchar to a C function. Is there anyway to do this on the stack instead of allocating a string each time? I feel like it was possible to do some type casting but I don?t remember how.
>
> pname := StrAlloc(Length(name) + 1);
> StrPCopy(pname, name);
> result := glGetUniformLocation(programID, pname);
> StrDispose(pname);

When creating the content of name append #0 (name:=whateverexpression+#0 ); , then do
  result = glGetUniformLocation(programID, @name[1]);

Of course this reduces the maximum length of the shortstring by one. In
Paleo times I've seen TP frameworks that always allocated an extra char
field in records after the shortstring, so that a #0 could always be
appended, even if the string was max.

But the best way is to design out shortstrings.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Better way to convert short string to pchar?

Ryan Joseph


> On Feb 23, 2018, at 6:51 PM, Marco van de Voort <[hidden email]> wrote:
>
> When creating the content of name append #0 (name:=whateverexpression+#0 ); , then do
>  result = glGetUniformLocation(programID, @name[1]);

Thanks now I remember. That was driving me crazy.

Regards,
        Ryan Joseph

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal