use-revolution digest, Vol 1 #1506 - 8 msgs

Yennie at aol.com Yennie at aol.com
Thu Jun 19 15:05:00 EDT 2003


> 1) String constants are copied before returning, e.g.
> *retstring = istrdup("atan2: must pass 2 arguments");
> Why is this? It seems to work just as well todo
> *retstring = "here is the return value";
> 
This is because you want to explicity allocate memory for the string which 
MetaCard can use and dispose of. A string constant is allocated locally in your 
external and could potentially make bad things happen when MetaCard tries to 
read from that spot in memory (or dispose of the memory).

It may be the case that the memory is dealt with so quickly that the risk is 
small with such a small return value, but you could potentially get into 
trouble by way of bad luck, platform differences, and/or the size of the return 
value.

> 2) To indicate a successful call to a handler, it is doing
> *retstring = (char *)calloc(1, 1);
> What is the significance of the 1 byte? It seems returning NULL works
> just as well and it's one less function call to make.
> 
Most likely MetaCard checks for NULL, in which case this is ok- but if 
Metacard is expecting a pointer, it's more "correct" to give it one. If the engine 
were to try to read from that pointer after you set it to NULL, it could crash 
your app.

All this is doing is creating an empty string: strings are terminated with a 
NULL byte, so the 1 byte is just the "end-of-string" marker with no string 
data before it.

HTH






------------------------------
Brian Yennie
Chief Technology Officer
QLD Learning, LLC
www.QLDLearning.com

PH: (904)-997-0212
EMAIL: Yennie at aol.com
-------------------------------
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.runrev.com/pipermail/use-livecode/attachments/20030619/98c19c93/attachment.html>


More information about the use-livecode mailing list