You, sir, name? wrote: Yakk wrote:
tastelikecoke wrote:I got lost there. Do you mean importing C++ code into C or is that supposed to be C source file? I don't know if I can use the standard library that liberally on our assignment.
Well, I was talking about solving the problem, not solving your assignment.
I was saying that a nice way to handle needing a stable sort in C would be to use C++ std::stable_sort, and exploit the ability for C++ to export a C-compatible library.
If solving the problem is the object, C89 and later has solved it for you:
- Code: Select all
/* in stdlib.h */
void qsort(void *base, size_t nmemb, size_t size, int(*compar)(const void *, const void *));
qsort is not stable.
So now you have to repack the array with indexes (or initial pointer values) for each element, sort that, then strip out the index information, and return that.
Yakk wrote:You, sir, name? -- yes, RAII lambda destructors rock.
I'd be tempted to call the Resource class "call on exit scope". Less abstract, but more self documenting.
I copied it from a codebase where the name actually makes sense.
But yeah, that, along with the below has added a whole new level of awesome to my C++ code.
[SNIP event-listener implantation]
Bad programmer, no cookie.
At 100 listeners installed per frame, 60 frames per second, that fails after 8 and a bit days. In a less ridiculous situation, it still fails after a few weeks/months/years.
Use a 64 bit index, and put off your crash for eons.
Or, make it a std::set< std::function<void(T...)>* >, and use new to get unique keys. Or write your own pseudo-heap of unique keys that deals with recycled keys (store recycled keys somewhere, and use them first...)
Another problem is that resource destruction in response to a callback could cause problems in the for loop. This one is serious: fixing it requires some work on the part of invoke.
(I mention these because I've implemented isomorphic code to yours, and these issues actually came up...)