user2233125
user2233125

Reputation:

Segmentation fault when declaring variable inside struct

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!

Upvotes: 1

Views: 1445

Answers (1)

NPE
NPE

Reputation: 500357

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.

Upvotes: 2

Related Questions