A mind less ordinary

10Jul/081

In-place array uniq in C

I've been developing Insight even though the uni project has come to an end, because it's fun! I also want to make it more stable and eventually release it under an open-source licence of some kind. There will be an update coming soon, I promise! I now have Internet, so I can write up some things... Anyway, one of the interesting things I wanted to do for Insight was an in-place form of uniq for an array, ideally without any additional memory allocation. It seems that this is something nobody else has done yet! So I set about doing it myself... For those of you who are unfamiliar with the Linux/UNIX command uniq, it takes a sorted list and removes any duplicates. This is almost exactly what I'm trying to do, with one caveat: I need to keep the "discarded" duplicates. What happens is that I have an array containing a number of strings, and these have all been dynamically allocated via malloc() or calloc(). If I just remove or overwrite their pointers, they'll vanish and cause a memory leak. While I've now fixed a large number of leaks thanks to Valgrind, I'm trying to actively avoid any possibility of adding them. Read on for the details...