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?

3
  • @hsalimi even there is better to do in general here that has no importance because no delete from a base class pointer Commented Mar 28, 2019 at 6:17
  • Implement copy/move constructor/assignment as well, otherwise you will delete the raw pointer twice if you do copy operations. Commented Mar 28, 2019 at 6:18
  • Are you sure the code you give us produce the invalid access ? Why do you not use std::string ? Commented Mar 28, 2019 at 6:19

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

I think the OP went by the documentation which states that the second parameter is the size of the destination string.
Which here is strlen(myname) + 1 since it's newed that size on the line just above. strlen(name) does not give the allocated length of name.
Yes, you are right. I just wanted to point why the OP might have made this error (apart from not adding 1). Upvoted the answer by the way. :)
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.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.