How to write template-like data structure in pure C


write type-independent data structure (say, red-black tree, dynamic array etc.) in pure C (which means you cannot use templates and any other features)


First of all, lets create an interface of our “generic” (or “template“) type. We can interpret void* as this unknown type and build some interface functions. It definitely has to have a constructor and destructor. Also, it can have a comparator and kind of print function.

So, we can define it like this:

typedef void* (*ConstructorFunc)(const void*);
typedef void (*DestructorFunc)(void*);
typedef int (*CompareFunc)(const void*, const void*);
typedef void (*PrintFunc)(const void*);

struct FuncFactory_struct
   ConstructorFunc constructor;
   CompareFunc compareFunc;
   DestructorFunc destructorFunc;
   PrintFunc printFunc;

#define FuncFactory struct FuncFactory_struct

And about implementation details..

Continue reading How to write template-like data structure in pure C

Use Emacs for remote compilation and catch output

Just a small tip. I have souced and makefile on remote¬† machine and I’m trying to compile it on my local machine, but I don’t have the compiler and environment. So when I run “M-x compile“, I use a script with following contents

ssh my_remote_machine "source my_environment_file; cd my_build_directory; make my_target"

Remember, that ssh command are executed not in your common environment, so you have to import it before execution of main commands (I use source command with some script to configure environment)

Then Emacs *compilation* buffer would receive stdout from remote make and you even would be able to navigate through compiler errors in your local code (if you have correct paths)

Firefox hangs and eats 25% cpu! (with fix)

This thing happened suddenly and I wasn’t able to explain it. A moment ago Firefox worked without problems, but now it hanged and Task Manager showed 25% CPU usage. Firefox was “Not responding” and nothing helped. In safe mode it worked well, so addons were the problems. But which one? I have a dozen..

Such behavior was still even at home at OpenSUSE with FF (and with same strange 25%!!), so I started Firefox with addons disabled. Everything went smoothly.¬† So there was a problems in addons. After playing with them, I’ve found that Xmarks conflicts with Firefox Sync: both are produthing thousands of bookmarks in infinite loop.

(I’ve created a bugreport in the Mozilla Bugzilla)

So disabling Xmarks solved the issue!

UPD: Bugzilla guys say “it’s not their fault” and redirect to Xmarks bugzilla, so I reported an issue in FF addons window for Xmarks

Emacs hangs on large file with DOS-encoded end of lines

I’ve had a quite big C++ source file, copied from Windows with ^M symbol after each line. After any scrolling downside Emacs 24.2 hanged up and the only option was to kill it.

Due to the fact that my .emacs configuration is quite big, I started with binary search, commenting out parts of lisp code. After few minutes I’ve figured out, that the problem lies in .gnu-emacs file, so I moved with binary search to that file.

Within 5 minutes, source of the problem was found:

(setq-default inhibit-eol-conversion t)

After commenting this line, Emacs was resurrected!