Quote:Original post by visitor
How well would be that work in a multithreading program? (It seems to me that the entire C pattern of using globals and statics is a bit problematic.)
It's fine for that (constant, read-only), but globals can be problematic due to linking, especially once DLLs come into play. It's an extra thing to worry about, and potentially a timed bomb.
Quote:For the special case, return NULL.
For the "normal, no data" case, return a buffer that is 1 DWORD long, where that DWORD's value is 0.
This would IMHO be the sanest approach if designing from scratch.
But considering this seems to be existing codebase, there is probably existing code using the function. By changing the contents of return value, but not its semantics, everything will still compile just fine, even though the returned data may now be used incorrectly.
I'd prefer API breaking change that would cause caller to fail to compile vs. stealthy change in contents.
Or, the return value should be something just barely safer, if C++:
struct Result { size_t count; DWORD * data; // or whatever way the implicit conversion is actually declared operator DWORD*() const { return data; }; };
This should preserve the logic while (perhaps) not breaking existing code, and allow for special case, perhaps even via some additional error states indicated by Result itself.
[Edited by - Antheus on April 27, 2009 10:45:53 AM]