Extern?

BlitzMax Forums/BlitzMax Programming/Extern?

sswift(Posted 2006) [#1]

Extern
Function puts( str$z )
Function my_puts( str$z )="puts"
End Extern

puts "Using clib's put string!"
my_puts "Also using clib's put string!"




What does extern do, and more importantly what the heck is with that variable name? Why is there a $ in the middle?


N(Posted 2006) [#2]
Extern is used to specify functions that are either in an imported library, non-BMax source file (Extern "C"), or specific to the OS API (Extern "Win32").

As far as I know, the part of Extern denoting 'source', sortof, is the calling convention used. (Win32 is __stdcall, by the way)

$z denotes a CString (you can't specify :CString though -- it's wierd).


sswift(Posted 2006) [#3]
"Crow Leone" sounds familiar. Something french I think. Where have I heard that before? It might be a single french word which has a similar sound. And yes, I know that is an anagram. :-)

$z denotes a C string? Well, I guess if you're going to make something nonsensical and against all the other conventions you might as well go all the way. $c would have made much too much sense! :-)

[edit]
Maybe I am thinking of "o sole mio" which is italian.
crow sole leone?
[/edit]


N(Posted 2006) [#4]
Maybe I am thinking of "o sole mio" which is italian.
crow sole leone?


No idea. I just ran it through [a http://www.wordsmith.org/anagram/anagram.cgi?anagram=noel+cower&include=&exclude=&d=2&n=4&m=5&source=adv&a=n&l=n&q=n&language=english-obscure&where=God+himself]http://www.wordsmith.org/anagram/[/a]


rdodson41(Posted 2006) [#5]
sswift the reason it is $z is that c style strings are terminated with a null character, which is ascii 0. hint 'z' for zero.


sswift(Posted 2006) [#6]
Sushi:
See these are the kind of things that should be in the help file. Now I might actually remember it. :-)

Where does the help file list this $z thing anyway?


Dreamora(Posted 2006) [#7]
Think in the advanced topic section of the language description where it talks about extern usage in general


gman(Posted 2006) [#8]
when you do a .ToCString() essentially there is memory allocated that must be freed. if you pass in the direct result of ToCString() to an extern function you have lost the ability to free it causing a memory leak. $z handles this for you. so essentially these are equivelent (sp?):
Extern 
	Function test1(c_str:Byte Ptr)
	Function test2(c_str$z)
EndExtern

Local mystr:String="the really cool test string"
Local strptr:Byte Ptr=mystr.ToCString()

test1(strptr)
test2(mystr) ' $z does the byte ptr conversion for you and cleans up afterwards
test1(mystr.ToCString()) ' this will cause a leak

MemFree(strptr) ' free up the memory ToCString() created