Usage of $z and $w

BlitzMax Forums/BlitzMax Programming/Usage of $z and $w

Htbaa(Posted 2012) [#1]
So I knew $z and $w existed and that they were somehow used when interfacing with C/C++ code. I find them quite convenient actually, as I don't have to do the work of converting a string to a CString and free up the used memory after wards.

I also remember that as of a couple of version of BlitzMax these 2 types are discouraged. But I can't find anything about it anymore on the forums. If they are deprecated, why so?


Yasha(Posted 2012) [#2]
One would assume you can't use them to store arbitrary binary data thanks to the null-terminator.

Also, are they real objects? Because the documentation for the ToCString and ToWString methods indicates that the result of those - which I would assume are of the same type - has to be freed manually.


ProfJake(Posted 2012) [#3]
Every call to ToCString/ToWString allocates a 16-bit aligned block of memory on the heap, large enough to store the String's data. That memory is unmanaged - meaning the GC will not take care of it automatically.
As a result that memory must be free'd manually by the programmer or else it will remain until the program ends.

When using $z or $w BlitzMax automatically inserts calls to ToCString/ToWString before and MemFree after the actual C/C++ function.
That way you don't have to worry about forgetting to free the memory.
On the downside you will have two additional function calls, one of which is a memory allocation. That is often not optional neither in terms of speed nor memory management.

I have not found anything about them being deprecated in a short search, could you add a link to that?

[EDIT] No the returned values are not "real objects", those functions simply return a pointer to a NULL-terminated string.

Last edited 2012


Htbaa(Posted 2012) [#4]
I don't have a link to it, as I can't find it anymore on the forums. Might've been something else instead. So I guess they're still OK to use.

Another neat thing I found out about is that if you have a C function that expects an int pointer to alter it's value later on, you can use Int Var in the BlitzMax function name.

For example, zmq_version(major:Int Var, minor:Int Var, patch:Int Var) for the zeromq library. Which I was surprised about would actually work :-).


Yan(Posted 2012) [#5]
AFAIR, at one point the compiler allowed them to be used with BMax functions which was unintended behaviour and was fixed in an update. Perhaps you're thinking of this?


Also, have a look at this...

http://blitzbasic.com/Community/posts.php?topic=58703#657218

I only hope you don't have as much difficulty comprehending plain english as the some others seemed to. ;o)


Htbaa(Posted 2012) [#6]
I think that sounds about right. Familiar at least.

I think I've got a clear view about when to use $z or $w, and when not. Basically if a C function returns or expects a const char* you can use it and you don't have the hassle of having to convert a normal string yourself, and then freeing it afterwards.


ProfJake(Posted 2012) [#7]
Yes.

Although technically, when a C/C++ function returns a char * string FromCString is called automatically.
Which returns a GC-managed BBString* without freeing the pointed to null-terminated string.
At least that is what I think happens after reading the assembly output of the bcc.