Hi, Alan, On Dec 18, 2010, at 10:57 AM, Alan W. Irwin wrote:
> Another model for plgnver, plgndev, and plgnfnam is one similar to > strlcpy that is discussed by Todd C. Miller and Theo de Raadt at > http://www.gratisoft.us/todd/papers/strlcpy.html. That is a most > interesting paper to read. I am not necessarily advocating something > similar to this model API. I leave that to those more knowledgable in > C than I am. However, I am mentioning this paper for further > discussion. It seems like strlcpy is a more convenient alternative to strncpy, but I don't see how this would solve the allocation problem. Wouldn't the user still need to ensure that a large enough buffer has been allocated? If so, the problem of determining how large is large enough still remains. Am I missing something? > <aside to Dave> Can you remember the name of the function you were using as > the model > for your API suggestion for plgnver, plgndev, and plgnfnam? If so, > that will give us a chance to dig into the official documention of > that function like we can currently do for getcwd and strlcpy. </aside to > Dave> OK, digging way back into my mental archives (and googling), I think I first came across this approach in a previous life when I did Windows programming. An example of this approach can be seen in the Windows GetShortPathName function (and probably others as well)... http://msdn.microsoft.com/en-us/library/aa364989(v=vs.85).aspx I mis-remembered the exact details of the return code. The WIN32 functions seem to return the total size needed iff the buffer pointer is NULL and the size is given as 0. This shouldn't really change the fundamentals of what I wrote earlier, however. > On 2010-12-18 13:59-0000 Andrew Ross wrote: > >> Down side for us [of automatic allocation] is that allocated memory >> in C may be difficult to use in other languages. I think automatic allocation by the library function, while not a big drawback, has a few potential points against it. One it that memory allocation is one of the (potentially) more expensive operations, so minimizing it is desirable. I don't think the use cases in question will result in this being a significant factor. The other issue is that once the library has allocated and populated the buffer, language bindings sometimes (typically?) require that the contents be copied into a suitably sized buffer that the scripting language manages. In this case the result is a double allocation (one by the library function, one by the scripting language), a extra strcpy from library buffer to scripting language buffer, and a free of the buffer returned by the library function. Getting the size first, allocating a buffer using he scripting language memory management routines, then passing the pointer to the library routine is, IMHO, more cleanly amenable to scripting language bindings (though I write mine "manually" so I don't know whether this would apply to SWIG generated bindings). Again, I don't think these factors will be significant for the functions under discussion, so I'll be happy with whatever consensus emerges. Dave ------------------------------------------------------------------------------ Lotusphere 2011 Register now for Lotusphere 2011 and learn how to connect the dots, take your collaborative environment to the next level, and enter the era of Social Business. http://p.sf.net/sfu/lotusphere-d2d _______________________________________________ Plplot-devel mailing list Plplot-devel@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/plplot-devel