Skip to content

Comment on Ask HN: Favorite pointer tricks in C?

Comments

A trick to save memory:

If you have a struct/class with a lot of members that are usually set to zero or some other initial value, you can store them in a "lookaside" structure that is hung off a global hash table with the pointer of the original object as the hashtable key. You can then use a bitfield to keep track of which members actually have interesting data.

So -- accessing the member would look something like this:

  int MyClass::get_foo() {
    if (foo_set_)
      return global_lookaside[this].foo;
    return 0;
  }
AboutSource Built by g1lg1l

Hackerly is an independent reader for Hacker News, built on the public HN API. Not affiliated with Y Combinator.