1

I am encountering a segmentation fault and I don't understand how this is possible. Within a class, I have two methods like this:

Declaration s in header:

int do_something() const;
void do_something_helper(const T &a, const T &b, const double new_param) const;

In myclass.cpp file:

int MyClass::do_something() const {
   double new_param = {calculation}
   ...
   std::cout << "Value of new param is: " << new_param << '\n';

   do_something_helper(a,b,c,new_param);
   ...
   return something;
}

void MyClass::do_something_helper(const T &a, const T &b, const double new_param) {
  const double another_value = {calculation};
  if(new_param < another_value)
  ...
}

The output statement in do_something works fine. A value is output without incident. If I remove any use of new_param in do_something_helper, there is no incident. It's only when I attempt to compare, output, or do anything with it that there is an instant segfault.

How is this even possible? I don't understand how I can have a segmentation fault on a primitive that has been passed by value. Any ideas?

new_param is a very recent addition. No runtime errors without it. If I take it out, it all goes smoothly. (But obviously I need it to do something new.)

Even more mysterious is that if I hardcode a value, like

do_something_helper(a,b,c,5.0);

I still get the seg fault.

JDizzle
  • 11
  • 2
  • 1
    Please read [how to create a minimal example](http://stackoverflow.com/help/mcve) – OldProgrammer Mar 05 '15 at 02:36
  • I can't see anything suspicious. Your do_something_helper definitions are inconsistent, but I assume that's a typo. The bug is probably elsewhere but just shows up in that variable. – Neil Kirk Mar 05 '15 at 02:41
  • Somewhere missing the [Rule of Three](http://stackoverflow.com/questions/4172722/what-is-the-rule-of-three) probably. – πάντα ῥεῖ Mar 05 '15 at 02:46
  • By adding and removing code, you're just moving the bug around to another part of your code. Just stick with the version that fails and fix that version, otherwise you'll go nuts discovering that when you add/remove irrelevant lines of code, strange things happen. Given that, you need to post a full function and driver code that shows how you use the function. – PaulMcKenzie Mar 05 '15 at 02:56
  • post the full code...if you want somebody to help you, – Tejendra Mar 05 '15 at 04:24

0 Answers0