2 ms·
can you elaborate on 'namespacing' in C? also, which way you use to do 'destructors' in C?
by synergy20 3y ago
can you elaborate on 'namespacing' in C?
also, which way you use to do 'destructors' in C?
- gavinhoward 3y agoNamespacing was mentioned by another comment, but I do something like this: libname_module_function(); So for my bc [1], I do this: bc_parse_if(...); That parses an `if` statement. For destructors, I want them to be passed and stored as first-class entities, so I defined them like this: typedef void (*BcDestructor)(void* item); Then things like my resizable arrays store the destructor for the items in the array, and when items are removed (popped, whatever), the destructor is run on the item if the destructor is not NULL. Works like a charm. Edit: There is one danger to the destructors: you can pass the wrong type of pointer. But if this happens, you'll probably get a lot of Valgrind errors and a spectacular crash.
- __d 3y ago"namespacing" in C means prefixes on public symbols. Typically, they're two-part. First you have the project/company prefix, and then the ADT (abstract data type) prefix. So, eg. "foo_hash_" is a prefix from company/project "foo" and is a part of the "hash" ADT interface. As a consequence, C people like abbreviations. Which is fine in moderation. Private functions don't need the prefixes, of course. Public variables are a little problematic if you ever port to Windows (DLLs), so try to avoid them.