Okay this program is really pissing me off! After line 95, the member variable is suddenly getting changed (That is, the 2nd string). Even after looking at it for 3 hours, I've failed to crack it :(
Please help me out guys!

# include <iostream>
# include <cstring>
using namespace std;

class strings
{
    private :
        char *strng;

    public  :
        strings(const char * str)
        {
            int length = strlen(str);
            strng = new char[length];

            int i;

            for(i = 0; i < length; i++)
            {
                strng[i] = str[i];
            }

            strng[length] = '\0';
        }

        ~strings()
        {
            delete(strng);
            strng = NULL;
        }

        void string_show()
        {
            cout << strng;
        }

        int string_length()
        {
            return strlen(strng);
        }

        int operator==(strings obj)
        {
            if(strlen(strng) != strlen(obj.strng))
            {
                cout << "\n\n\t* INSIDE FUNCTION!";
                cout << "\n\n\t" << strng;
                cout << "\n\n\t" << obj.strng;
                return 0;
            }

            int i;
            for(i = 0; i < strlen(strng); i++)
            {
                if(strng[i] != obj.strng[i])
                {
                    cout << "\n\n\t** INSIDE FUNCTION!";
                    cout << "\n\n\t" << strng;
                    cout << "\n\n\t" << obj.strng;
                    return 0;
                }
            }

            cout << "\n\n\t*** INSIDE FUNCTION!";
            cout << "\n\n\t" << strng;
            cout << "\n\n\t" << obj.strng;

            return 1;
            
            //  **UPTO HERE, THE STRING IS NOT CHANGED!!**
        }

};

int main()
{
    strings obj_1("Hello");
    strings obj_2("Hello");

    cout << "\n\n\t1st String : ";
    obj_1.string_show();
    cout << "\n\n\t2nd String : ";
    obj_2.string_show();

    cout << "\n\n\tLength Of 1st String : " << obj_1.string_length();
    cout << "\n\n\tLength Of 2nd String : " << obj_2.string_length();

    cout << "\n\n\t1st String ~ Length : " << obj_1.string_length() << "\n\n\t";
    obj_1.string_show();
    cout << "\n\n\t2nd String ~ Length : " << obj_2.string_length() << "\n\n\t";
    obj_2.string_show();
    
    //  **UPTO THIS, THE STRING IS NOT CHANGED**

    if(obj_1 == obj_2)  //  **HERE, INSIDE THE FUNCTION, THE STRING IS NOT CHANGED**
        cout << "\n\n\t\tEqual Strings!";
    else
        cout << "\n\n\t\tUnequal Strings!";
        
    //  **THE STRING IS NOW CHANGED!**

    cout << "\n\n\t1st String ~ Length : " << obj_1.string_length() << "\n\n\t";
    obj_1.string_show();
    cout << "\n\n\t2nd String ~ Length : " << obj_2.string_length() << "\n\n\t";
    obj_2.string_show();

    return 0;
}

Recommended Answers

All 6 Replies

for starters line 14 does not allocate memory for the string's null terminating character. So line 23 writes beyond the end of the character array.

line 28 should be delete[] string; line 42: pass strings class by reference int operator==(strings& obj)

As AD had hinted, you seem to be forgetting that array indexes start at zero (0) and end at (size-1) (a.k.a. length-1). Line 23 is using "length" as an index. The element "strng[length]" does not exist, making that an invalid assignment.

You could use the strcpy() function to acquire this functionality:

int i;

            for(i = 0; i < length; i++)
            {
                strng[i] = str[i];
            }

The element "strng[length]" does not exist

Aww man! I generally don't make these mistakes. Dammit!

This is how it should look...

strings(const char * str)
        {
            int length = strlen(str);
            strng = new char[length + 1];

            int i;

            for(i = 0; i < length; i++)
            {
                strng[i] = str[i];
            }

            strng[length] = '\0';
        }

Thanks Ancient. It worked but why? What did I do wrong?

@dusktreader
I'm trying to make my own functions...

This is a perfect example of why watching array boundaries is so important.

It is likely that the two arrays were stored directly adjacent to each other in memory. If that happens, and you overrun the boundaries of the first array, the second array gets unintentionally modified as well.

If that happens, and you overrun the boundaries of the first array, the second array gets unintentionally modified as well.

The thing is, i didn't even modify the first array, then how come the second array is modified?

int operator==(strings &obj)
        {
            if(strlen(strng) != strlen(obj.strng))  //  NO MODIFICATION
            {                                       //  ONLY READING
                return 0;
            }

            int i;
            
            for(i = 0; i < strlen(strng); i++)
            {
                if(strng[i] != obj.strng[i])        //  NO MODIFICATION HERE ALSO
                {
                    return 0;
                }
            }
                                                    //  NO MODIFICATION TILL HERE
                                                    //  WHEN I PRINTED THE STRINGS
                                                    //  HERE, THEY WERE FINE
            return 1;
        }

But inside main(), just after the function call, the string passed as parameter gets changed. So how the hell is this happening?
And why is the program correcting itself when I'm passing a reference of the object to the function?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.