Search code examples
c++cstructcodebase

Adding a field to a structure without breaking existing code


So I'm working with this huge repository of code and have realized that one of the structs lack an important field. I looked at the code (which uses the struct) as closely as I could and concluded that adding an extra field isn't going to break it.

Any ideas on where I could've screwed up?

Also: design advice is welcome - what's the best way I can accomplish this?

E.g. (if I wasn't clear):

typedef struct foo
{
  int a;
  int b;
}
foo;

Now it's :

typedef struct foo
{
  int a;
  int b;
  int c;
}
foo;

Solution

  • From what you've written above I can't see anything wrong. Two things I can think of:

    1. Whenever you change code and recompile you introduce the ability to find "hidden" bugs. That is, uninitialized pointers which your new data structure could be just big enough to be corrupted.
    2. Are you making sure you initialize c before it gets used?

    Follow Up:

    Since you haven't found the error yet I'd stop looking at your struct. Someone once wrote look for horses first, zebras second. That is, the error is probably not an exotic one. How much coverage do you have in your unit tests? I'm assuming this is legacy code which almost invariably means 0% or at least that's been my experience. Is this accurate?