[GiNaC-devel] Why does this program crash?

Alexey Sheplyakov asheplyakov at yandex.ru
Sun May 15 20:21:35 CEST 2016


Hello,

> I condensed my previous post on subclassing symbol into this little
> program, which crashes at the last line. Any idea why? Is it a bug or do

>
>      #include <iostream>
>      #include <ginac/ginac.h>
>      using namespace std;
>      using namespace GiNaC;
>
>      int main()
>      {
>         symbol x("x");
>         x = realsymbol("y");

Assigning an instance of a subclass is in general a bad idea, see https://en.wikipedia.org/wiki/Object_slicing
In this particular case GiNaC::basic assignment operator resets status_flags::evaluated

Thus when an ex gets constructed from the symbol here

>         cout << x << endl;

ex::construct_from_basic enters an infinite recursion in

 const ex tmpex & = other.eval();

since symbol::eval() is essentially a nop. Sooner or later the program exhausts the stack and segfaults.
So this is sort of GiNaC bug, but it's certainly useful for it lets you know that your code is wrong.

#include <iostream>
#include <ginac/ginac.h>
using namespace std;
using namespace GiNaC;

int main() {
      symbol x("x");
      x.dbgprinttree();
      x = realsymbol("y");
      x.dbgprinttree();
      return 0;
}

x (symbol) @0x7ffc08327bc0, serial=1, hash=0x1, flags=0x6, domain=0
y (symbol) @0x7ffc08327bc0, serial=2, hash=0x1, flags=0x0, domain=0

So x remains complex (domain == 0) after assigning y. This is natural from C++ point of the view,
however this is most likely not what you mean. Without the bug which triggers an infinite recursion
the original problem (assigning an instance of a subclass) will be more difficult to find.


Hope this helps,
       Alexey


More information about the GiNaC-devel mailing list