0xC0000005: Access violation reading location 0x00000000. issues with overloaded == operator

-1

Hi there I'm currently working on a program for a data structures course that I am taking and I'm working on a part of an overloaded extraction operator. I am currently receiving the error Access violation reading location 0x00000000. when I attempt to compare two My String Objects with one another. A MyString object is essentially a c String, here is the class definition

    class MyString {
    private:
        char* str;
    public:
        MyString();
        MyString(const char*);
        MyString(const MyString&);
        ~MyString();

        int length() const;
        void read(istream&, char);
        static const int MAX_INPUT_SIZE = 127;

        MyString& operator=(const MyString&);
        MyString& operator +=(const MyString&);
        friend MyString operator +(const MyString&, const MyString&);


        char operator[](int location)const;
        char& operator[](int location);

        friend ostream& operator<<(ostream&, const MyString&);
        friend istream& operator>>(istream&, MyString&);

        friend bool operator <(const MyString& left, const MyString& right);
        friend bool operator <=(const MyString& left, const MyString& right);
        friend bool operator >(const MyString& left, const MyString& right);
        friend bool operator >=(const MyString& left, const MyString& right);
        friend bool operator ==(const MyString& left, const MyString& right);
        friend bool operator !=(const MyString& left, const MyString& right);

    };
}
#endif

this is the overloaded == operator throwing the exception

bool operator ==(const MyString& left, const MyString& right) {
        return strcmp(left.str, right.str) == 0;
    }

this is the context in which i am making the comparison, assume that temp is a valid MyString object.

for (int i = 0; i < sizeof(cs_measure::Measure::unitStrings); i++) {
            if (cs_measure::Measure::unitStrings[i] == temp) {
                readMe.unit = i;
                in >> readMe.unit;
            }
        }

this is the array that is being referenced in the for loop

const MyString Measure::unitStrings[] =
    { "dram", "tsp", "tbsp", "oz", "cup", "pint",
     "qt", "gal", "peck", "bushel", "barrel", "acre_ft" };

This is my first time posting to stack overflow so I have left out any crucial information that may be useful for solving this issue please let me know and I would be happy to help.

c++
arrays
extraction
asked on Stack Overflow Jan 18, 2020 by Chris Camano

1 Answer

0

As mentioned in the comments, sizeof(cs_measure::Measure::unitStrings) is not the number of items in the array cs_measure::Measure::unitStrings. It is the number of bytes the array occupies in memories.

Since the size in bytes is almost surely larger than the number of elements, you will access the array out-of-bounds in the loop, causing undefined behavior.

You can get the number of items in a built-in array with

std::size(cs_measure::Measure::unitStrings)

since C++17 (may require #include<iterator> if you have not included any container library header).

Or if you can't use C++17, you can define your own version of it, though the C++17 standard version is a bit more powerful. (from cppreference.com):

template <class T, std::size_t N>
constexpr std::size_t size(const T (&array)[N]) noexcept
{
    return N;
}
answered on Stack Overflow Jan 18, 2020 by walnut • edited Jan 18, 2020 by walnut

User contributions licensed under CC BY-SA 3.0