how to fix 'Access violation reading location' in this code

1

run this code i got some error like this

' Exception thrown at 0x778D7FCB (ntdll.dll) in Project1.exe: 0xC0000005: Access violation reading location 0x00000014.'

This error occurs in this line

~UnivStudnet() {
delete[]major; // error
    }
#include <iostream>
#include <cstring>
using namespace std;

class Person {
private:
    char * name;
public:
    Person(const char * myname) {
        name = new char[strlen(myname) + 1];
        strcpy_s(name, strlen(name), myname);
    }
    ~Person() {
        delete[]name;
    }

    void WhatYourName() const {
        cout << "My name is " << name << endl;
    }
};

class UnivStudnet : public Person {
private:
    char * major;
public:
    UnivStudnet(const char * myname, const char * const mymajor) :Person(myname) {
        major = new char[strlen(mymajor) + 1];
        strcpy_s(major, strlen(major), mymajor);
    }
    ~UnivStudnet() {
        delete[]major;
    }

    void WhoAreYou() const {
        WhatYourName();
        cout << "My major is " << major << endl;
    }
};

int main(void) {

    UnivStudnet st1("kim", "Mathenatics");
    st1.WhoAreYou();
    UnivStudnet st2("hong", "Physiscs");
    st2.WhoAreYou();
    return 0;
}

How do I fix this error?

c++
destructor
asked on Stack Overflow Mar 28, 2019 by Mr.feel

3 Answers

4

There are bugs on the two strcpy_s lines.

strcpy_s(name, strlen(name), myname);

should be

strcpy_s(name, strlen(myname)+1, myname);

and likewise

strcpy_s(major, strlen(major), mymajor);

should be

strcpy_s(major, strlen(mymajor)+1, mymajor);

Calling strlen on the newly-allocated char arrays name and major which have indeterminate values, causes undefined behavior which is the cause of your crash.

answered on Stack Overflow Mar 28, 2019 by jcarpenter2
1

You could go more C++-way:

You need to declare:

virtual ~Person()

destructor in base class and then:

class UnivStudnet : public Person {
private:
    std::string major;
public:
    UnivStudnet(const char * myname, const char * const mymajor) :Person(myname), major(mymajor) {
    }
    virtual ~UnivStudnet() {
    }
...

This way you will achieve what you need and do not think about memory allocation/deallocation. Remember to #include <string> header.

Same way do it in Person class.

answered on Stack Overflow Mar 28, 2019 by Diodacus
1

Your strcpy_s usage is suspect.

    major = new char[strlen(mymajor) + 1];
    strcpy_s(major, strlen(major), mymajor);

The second parameter to strcpy_s is the allocated size of the buffer specified by the first parameter. (And I just now realized - based on another answer that strlen(major) is undefined before you copy to it!

You are allocating the buffer to be large enough to hold the string, but the subsequent call to strcpy_s is indicating that major isn't big enough to hold the entire string including the null char.

Better:

    size_t len = strlen(mymajor) + 1;
    major = new char[len];
    strcpy_s(major, len, mymajor);

Repeat the above pattern for the base class name parameter as well.

answered on Stack Overflow Mar 28, 2019 by selbie

User contributions licensed under CC BY-SA 3.0