Debugging results in an error as access violation.

Asked 2 years ago, Updated 2 years ago, 160 views

The compilation works well, but the access violation appears when debugging. What did I do wrong? Did I put the pointer wrong?

#include <iostream>
#include <string>

using namespace std;

class Complex {
private:
    float real, imaginary;
public:
    Complex(float _real, float _imaginary) : real(_real), imaginary(_imaginary) {};
    auto operator==(Complex rhs) {
        return (real == rhs.real) && (imaginary == rhs.imaginary);
    }
};

class MyString {
private:
    const char *str;
public:
    MyString():str(""){}
    MyString( const char *_str) : str(_str) {}
    auto operator==(MyString rhs) {
        return (!strcmp(str, rhs.str));
    }
};

template <class T, int size>
class List {
    T *elem;
    int size;
    int currentSize;
public:
    List() : currentSize(0) {}
    List(int _size) : size(_size), currentSize(0) { elem = new T[_size]; }
    List(const List& another) : size(another.size), currentSize(another.currentSize) {
        elem = new T[size];
        for (int i = 0; i < currentSize; i++) {
            elem[i] = another.elem[i];
        }
    }
    int add(const T &anElem) {
        elem[currentSize] = anElem;
        return currentSize++;
    }


    void find(const T &anElem) const {
        for (int i = 0; i < currentSize; i++) {
            cout << typeid(elem[i]).name() << endl;
            if (elem[i] ==anElem) {
                cout << "exist" << endl;
            }
            else {
                cout << "not exist" << endl;
            }
        }
    }
    void remove(const T &anElem) const {
        for (int i = 0; i < currentSize; i++) {
            if (typeid(elem[i]) == typeid(anElem)) {
                for (int j = i; j < (currentSize - 1); j++) {
                    elem[j] = elem[j + 1];
                }
            }
            else {
                cout << "not exist" << endl;
            }
        }
    }
    void remove(const int location) const {
        for (int j = location; j < (currentSize - 1); j++) {
            elem[j] = elem[j + 1];
        }
    }

    List operator = (const List& L){
        for (int i = 0; i < currentSize; i++) {
            L.elem[i] = elem[i];
        }
        return L;
    }

    ~List() { delete [] elem; }
};


int main() {
    List<Complex, 100> cList;
    List<MyString, 200> sList;

    int i1 = cList.add(Complex(0, 0));
    cList.add(Complex(1, 1));
    int i2 = sList.add("abc");
    sList.add("def");
    cList.find(Complex(1, 0));
    sList.find("def");
    cList.remove(i1);
    sList.remove("abc");

    List<MyString, 200> s2List(sList);
    List<MyString, 200> s3List;
    s3List.add("123");
    s3List = s2List;
    s3List.remove("def");
}

debugging access

2022-09-22 14:45

1 Answers

I turned it on with a debugger.

int i1 = cList.add(Complex(0, 0));

Ride in here, and in the add method below,

int add(const T &anElem) {
        // // elem = 0xCCCCCCCC, currentSize = 0
        elem[currentSize] = anElem;  // <<< access violation

        return currentSize++;
    }
The exceptions are raised

approach.

cList was created as the List() constructor, and memory was not allocated to lem. (Please check the break point with a debugger.)


2022-09-22 14:45

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.