[Gambas-devel] EXTERN Calls

Christopher Brian Jack brian at ...418...
Mon Mar 20 22:12:18 CET 2006


On Mon, 20 Mar 2006, Scott, Vince wrote:

> 1.) Sample 1
>
>
> int GetName(char* pName)
> {
>   sprintf(pName, "%s", "TEST");
>
>   return 0;
> }

If you are using strPtr it is best to also pass the string length and use
snprintf rather than regular printf.  This code is vulnerable to memory
thrashing if the supplied string is too short.

> 2.) Sample 2
>
> char* GetAName()
> {
>   Char* buf = malloc(5);
>
>   sprintf(buf, "%s", "TEST");
>
>   return buf;
>
> }

You are leaking memory by doing it this way becuase your Gambas program
has no way to free the memory consumed by malloc().  I'd stick to passing
the string (and length) by reference.

> 1.) Sample 1
>
> Extern GetName(ptr AS Pointer) AS Integer IN "libtest"
>
> Public Sub Test1()
>
>   Dim ret   AS Ingteger
>   Dim ptr   AS Pointer
>   Dim sTest AS String

Try:
    sTest=STRING(".",5)
    ptr=strPtr(sTest)
In place of:
    ptr = Alloc(sTest, 5)

>   ret = GetName(ptr)
>   Message("String= " & sTest)

Delete this line if using STRING(".",5):
    Free(ptr)

> End

.=================================================.
|  Christopher BRIAN Jack aka "Gau of the Veldt"  |
+================================================='
| brian _AT_ brians-anime _DOT_ com
`=================================================-
Hi Spambots, my email address is sputnik at ...418...




More information about the Devel mailing list