-2

I have two classes that will create a dynamically allocated cstring (null terminated) of n size upon an object of that class being created. In one class, I have a member function overloading the equivalency operator, and in the other class, I have a non-member function overloading the equivalency operator. I'm using strcmp to compare two cstrings of each class type, and no matter what I initialize the array to, it always returns true.

int main() {

    CSTR cstr(5);
    CSTR cstr2(6);

    if (cstr == cstr2)
        cout << "Equal";   //<< Always returns true
    else
        cout << "False";

    cout << "\n\n";

    CSTR2 cstr2_1(5);
    CSTR2 cstr2_2(6);

    if (cstr2_1 == cstr2_2)
        cout << "Equal";  //<< Always returns true
    else
        cout << "False";
    return 0;
}

==============================

class CSTR {
    public:
        CSTR();
        CSTR(unsigned int n);
        ~CSTR();


        bool operator ==(const CSTR & rhs);
    private:
        unsigned int size;
        char *elems;
};

============================

CSTR::CSTR() {

    size = 0;
    elems = new char [0];
}

CSTR::CSTR(unsigned int n) {
    if (n > 0) {
        size = n;
        elems = new char [size];
}
    else {
        size = 0;
        elems = new char [0];
    }
}

CSTR::~CSTR() {

    delete [] elems;
}

bool CSTR::operator ==(const CSTR & rhs) { //<< Always returns true

    return (strcmp(elems, rhs.elems) == 0);
}

===========================================

class CSTR2 {
    public:
        CSTR2();
        CSTR2(unsigned int n);
        ~CSTR2();

        char * getCstrPtr() const;
    private:
        unsigned int size;
        char *elems;
};

bool operator ==(const CSTR2 & CSTR2_1, const CSTR2 & CSTR2_2);

========================================

CSTR2::CSTR2() {

    size = 0;
    elems = new char [0];
}

CSTR2::CSTR2(unsigned int n) {

    if (n > 0) {
        size = n;
        elems = new char [size];
    }
    else {
        size = 0;
        elems = new char [0];
    }
}

CSTR2::~CSTR2() {

    delete [] elems;
}


char * CSTR2::getCstrPtr() const {

    return elems;
}


bool operator ==(const CSTR2 & CSTR2_1, const CSTR2 & CSTR2_2) {  //<< Always returns true

    return (strcmp(CSTR2_1.getCstrPtr(), CSTR2_2.getCstrPtr()) == 0);
}
Victor
  • 57
  • 2
  • 8
  • 4
    What is this? You're comparing a couple of uninitialized arrays and expecting meaningful results? It's funny that you felt the need to handle the `n==0` case separately within the `CSTR` constructor, when it does exactly the same thing as the `n>0` case. Your class doesn't follow the Rule of Three. Use [`std::string`](http://en.cppreference.com/w/cpp/string/basic_string) and stop trying to reinvent the wheel for no reason. – Praetorian Jul 18 '14 at 01:58
  • The behavior you see is what you would expect if the code were correct. However, the comparison operator doesn't check length and the constructor doesn't initialize the raw memory, so the code is not correct. The first point, that you're seeing what you should expect, is in itself enough to make the question unanswerable. – Cheers and hth. - Alf Jul 18 '14 at 02:00
  • Trust me, I am definitely not trying to reinvent the wheel. This is for a HW assignment that we are building on and the only information I have so far is to allocate memory for the arrays and then overload the equivalency operator. I've been racking my brain trying to figure out what the hell kind of equivalency I'm supposed to test for when there is no data established yet. The most miserable part of this class so far has been doing all of this stuff with cstrings when all everyone ever says is that I will never use them. – Victor Jul 18 '14 at 02:05

1 Answers1

1

You haven't initialised the contents of those buffers you newed up. They can contain anything whatsoever. If both of the buffers you allocated both happened to start with '\0', then strcmp would indeed say they are equal (since they are both considered zero-length strings).

Also, I agree with everything in Praetorian's comment, especially about the Rule of Three.

C. K. Young
  • 219,335
  • 46
  • 382
  • 435
  • Thanks Chris! This is for an assignment that we will be building on over the weekend. All I know about it so far is to create what I have and overload the equivalency operator for each class. I was not sure if strcmp should return 0 or not when all I had to go off of was the size of the array. – Victor Jul 18 '14 at 02:02
  • Another possible reason for the behavior seen, would be that the code was correct. We can see that it isn't. But the most likely fix isn't to initialize the array contents, but rather to have equality comparison check lengths, since the lengths are represented explicitly. – Cheers and hth. - Alf Jul 18 '14 at 02:03
  • @Alf: Of course I agree the lengths should be checked first, but then in that case, we're not dealing with "C" strings any more. Comparisons should then use `memcmp`. – C. K. Young Jul 18 '14 at 02:04
  • @ChrisJester-Young: yes. – Cheers and hth. - Alf Jul 18 '14 at 02:05
  • I was originally testing for length, but then, like Christ said, I wouldn't be dealing with cstrings anymore. – Victor Jul 18 '14 at 02:18
  • If you're not ready to do the Rule of Three yet then at least disable the copy-constructor and copy-assignment operator so you get a compiler error instead of bogus behaviour if you inadvertantly do a copy in your code. – M.M Jul 18 '14 at 02:21