Gyan
Gyan

Reputation: 12410

Confusing segmentation fault

EDIT: I basically revamped the whole question so I could provide an executable example...

I have been getting a segmentation fault that I can't figure out. Here is a compacted version of my code. I maintained the original class hierarchy even though some of the classes have no relevant methods since I think that it has something to do with my issue, especially after some of the comments I've been getting.

#import <vector>
using namespace std;

template<class Data = float>
  class Vector
  {
    // All pure virtual functions.
  };

template<class Data>
  class TranslationVector : public virtual Vector<Data>
  {
    // All pure virtual functions.
  };

template<class Data>
  class SimpleVector4 : public virtual Vector<Data>
  {
    public:
      SimpleVector4(const Data d0, const Data d1, const Data d2, const Data d3)
      {
        fData = new vector<Data> ;

        fData->push_back(d0);
        fData->push_back(d1);
        fData->push_back(d2);
        fData->push_back(d3);
      }

      vector<Data>*
      getData()
      {
        return (fData);
      }

    private:
      vector<Data>* fData;
  };

template<class Data>
  class SimpleTranslationVector4 : public SimpleVector4<Data> , public TranslationVector<Data>
  {
    public:
      SimpleTranslationVector4(const Data x, const Data y, const Data z, const Data w) :
        SimpleVector4<Data> (x, y, z, w)
      {
      }
  };

template<class Data = float>
  class Matrix
  {
    // All pure virtual functions.
  };

template<class Data>
  class TransformationMatrix : public virtual Matrix<Data>
  {
    // All pure virtual functions.

    virtual void
    translate(TranslationVector<Data>* const translation) = 0;
  };

template<class Data>
  class SimpleMatrix44 : public virtual Matrix<Data>
  {
    public:
      SimpleMatrix44()
      {
        fData = new vector<Data> (CELLS_IN_MATRIX, 0);

        setIdentity();
      }

      vector<Data>*
      getData()
      {
        return (fData);
      }

      void
      setIdentity()
      {
        fData->at(0) = 1;
        fData->at(1) = 0;
        fData->at(2) = 0;
        fData->at(3) = 0;

        fData->at(4) = 0;
        fData->at(5) = 1;
        fData->at(6) = 0;
        fData->at(7) = 0;

        fData->at(8) = 0;
        fData->at(9) = 0;
        fData->at(10) = 1;
        fData->at(11) = 0;

        fData->at(12) = 0;
        fData->at(13) = 0;
        fData->at(14) = 0;
        fData->at(15) = 1;
      }

    private:
      static const int CELLS_IN_MATRIX = 16;

      vector<Data>* fData;
  };

template<class Data>
  class SimpleTransformationMatrix44 : public SimpleMatrix44<Data> , public TransformationMatrix<Data>
  {
    public:
      SimpleTransformationMatrix44() :
        SimpleMatrix44<Data> ()
      {
      }

      void
      translate(TranslationVector<Data>* translation)
      {
        vector<Data> *data = SimpleMatrix44<Data>::getData();
        vector<Data> *transData = ((SimpleVector4<Data>*) translation)->getData();

        // The error occurs on this line:
        data->at(12) += data->at(0) * transData->at(0) + data->at(4) * transData->at(1) + data->at(8) * transData->at(2);
        data->at(13) += data->at(1) * transData->at(0) + data->at(5) * transData->at(1) + data->at(9) * transData->at(2);
        data->at(14) += data->at(2) * transData->at(0) + data->at(6) * transData->at(1) + data->at(10) * transData->at(2);
        data->at(15) += data->at(3) * transData->at(0) + data->at(7) * transData->at(1) + data->at(11) * transData->at(2);
      }
  };

int
main(int argc, char** argv)
{
  SimpleTransformationMatrix44<float> matrix1;
  matrix1.translate(new SimpleTranslationVector4<float> (0.0f, 10.0f, 0.0f, 1.0f));

  return 0;
}

I have commented in the code where the error occurs. From debugging I can see that it actually occurs in the size() function of vector and that transData has not been initialized. I can't for the life of me figure out why transData has not been initialized! Any ideas?

Thanks,

Gaz.

Upvotes: 1

Views: 312

Answers (2)

eugene_che
eugene_che

Reputation: 1997

Cast of non-inherited classes seems to be your error.

Let's see your code. There is a conversion of SimpleTranslationVector4<float>* to TranslationVector<float>* while invoking the translate function. Then the converted value is converted again into SimpleVector4<float>*. But the SimpleVector4<float> does not inherit TranslationVector<float>.

This code also results in error.

template<class Data>
class SimpleVector4 {
public:
    int a;
};

template<class Data>
class TranslationVector {
};

template<class Data>
class SimpleTranslationVector4 : public SimpleVector4<Data>,
public TranslationVector<Data> {
};

int main() {
    SimpleTranslationVector4<float> A;
    SimpleVector4<float>* b = (SimpleVector4<float>*)&A;
    TranslationVector<float>* c = (TranslationVector<float>*)&A;
    SimpleVector4<float>* d = (SimpleVector4<float>*)c;
    b->a = 1; // ok
    d->a = 1; // error
}

Upvotes: 3

bshields
bshields

Reputation: 3593

You're doing a C-style cast between unrelated types. This is not safe. The fact that you need to do this at all probably indicates a problem in your design, but try replacing this:

vector<Data>* transData = ((SimpleVector4<Data>*) translation)->SimpleVector4<Data>::getData();

with this:

vector<Data>* transData = dynamic_cast<SimpleVector4<Data>*>(translation)->getData();

Upvotes: 1

Related Questions