1

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!

2
  • 4
    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. Commented Apr 1, 2013 at 18:09
  • 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. Commented Aug 26, 2016 at 12:10

1 Answer 1

2

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.
Sign up to request clarification or add additional context in comments.

2 Comments

In addition to valgrind, there's Electric Fence and (Windows-only) App Verifier.
Thanks for your help everyone, but I figured it out. Turns out I allocated a large array composed of Notes statically inside another class. I should have done this dynamically, since the program's stack isn't big enough to hold so many instances of Note. Which is quite ironic, since adding a variable to the struct caused the stack to overflow. Just should have looked at the website title, lol.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.