0

I overloaded operator << for my dynamic array Tvector container and i want to cout areas of squares.

std::ostream & operator<<(std::ostream &os, TVector &s) {
    for(int i = 0; i < s.length(); ++i){
        os << "[" << s[i].Area() << " ";
    }
    os << "]";
    return os;
}

Function area

double Square::Area() {
    double len_a = a.dist(b);
    double len_b = b.dist(c);
    return len_a * len_b;
}

If i just

cout << a.Area();

everything works fine. But if i want to cout dynamic array, compilator throws segmentation fault error. main function fragment

    Square a;
    std::cin >> a;
    Square a1;
    std::cin >> a1;
    TVector v;
    v.push_back(a);
    v.push_back(a1);
    std::cout << v;

Segmentation fault

P.S Code for TVector

TVector::TVector()
    : _array(nullptr)
    , _size(0)
{

int TVector::length() {
    return _size;
}

Square &TVector::operator[](int idx) {
    return _array[idx];
}

std::ostream & operator<<(std::ostream &os, TVector &s) {
    for(int i = 0; i < s.length(); ++i){
        os << "[" << s[i].Area() << " ";
    }
    os << "]";
    return os;
}

std::istream& operator>>(std::istream& is, TVector& s) {
    Square a;
    is >> a;
    s.push_back(a);
    return is;
}

TVector::~TVector()
{
    clear();
}
Yan
  • 29
  • 5
  • And what is `TVector`? – Evg Nov 18 '21 at 12:59
  • 2
    Your code snippets most likely don't contain the source of the bug. Please read how to make a [mre]. –  Nov 18 '21 at 13:04
  • @Evg added TVector code on pastebin – Yan Nov 18 '21 at 13:06
  • loaded TVector functions which is needed to solve this problem, i think – Yan Nov 18 '21 at 13:12
  • The `TVector` class doesn't seem to honor the Rule of Three, so I'm guessing the `Square` class doesn't either. If you're also doing nasty C-like dynamic memory stuff in that class, there's a reasonable chance that copy-assignment is causing this issue. Or, if `Square` is a non-trivial type, there could be problems with `malloc` because you're not doing any placement-new/delete management. There's also the issue that `realloc` can move the memory so if that will break anything inside `Square` weird things can happen. You should use array-new/delete at the very least. – paddy Nov 18 '21 at 13:12
  • @paddy but why when i `cout << Square.Area()` it doesn't throw segmentation fault? – Yan Nov 18 '21 at 13:15
  • Why are you assuming that's even related to the issue? – paddy Nov 18 '21 at 13:16
  • Undefined behavior means anything can happen. It doesn't mean the code has to crash. – drescherjm Nov 18 '21 at 13:16
  • 2
    Can you just give a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). – Jason Nov 18 '21 at 13:18
  • Temporarily comment out the `clear();` if this fixes the problem the bug is likely the rule of 3 violation. Related: [https://stackoverflow.com/questions/4172722/what-is-the-rule-of-three](https://stackoverflow.com/questions/4172722/what-is-the-rule-of-three) – drescherjm Nov 18 '21 at 13:18

0 Answers0