1
votes

I've recently come across a very strange segfault while developing my application. Basically, if I add another variable to one of my structs, a segfault is caused upon execution, for no apparent reason. Removing this variable immediately solves the problem. The struct is as follows:

typedef struct Note {
    char cNote;
    unsigned int uiDuration;
    unsigned int uiVelocity;
};

As soon as I add a

long lStartTime;

variable anywhere in the struct, the code compiles as usual but will throw a segmentation fault. GDB's backtrace is lost somewhere in some obscure WIN methods that I don't even use.

Any ideas?

Thanks!

1
You have undefined behaviour somewhere. Adding this member is just making it more apparent. Do you do anything related to Notes that involves manual memory management or accessing arrays? Start looking there. - Joseph Mansfield
To add a bit to Joseph's comment - it is quite possible some code is doing something untoward with memory management or array access, but no relationship whatsoever to Note. One of the joys of undefined behaviour is that symptoms can change when totally unrelated code is edited. - Peter

1 Answers

2
votes

I see several possible explanations:

  1. Something somewhere assumes that the struct is of a certain size. Changing the size break things.
  2. You might have a memory bug of some sort, which is brought to the surface by you changing the layout of things in memory. Try a tool like valgrind or Purify.
  3. You are changing the struct in a header file, but are failing to rebuild all source files that use the struct.