0

I created a small program to experiment with linked lists. When I ran it however, I got a "Access violation writing location" at jerry->age = 45. I'm not sure what I'm doing wrong.

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

struct Person {
    string name;
    int age;
    char gender; 
    struct Person* contact;
};

int main() {
    struct Person* jerry = (struct Person*) malloc(sizeof(struct Person));
    jerry->name = "Jerry";
    jerry->age = 45;
    jerry->gender = 'M';
    jerry->contact = (struct Person*)malloc(sizeof(struct Person));;
    printf("Hi! My name is %s.\n I am %d years old.\n I am ");
    printf((jerry->gender == 'M') ? " a man.\n" : " a woman.\n", jerry->gender);
    printf("I happen to know ");
}

EDIT:

My new code is as follows:

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

class Person {
public:
    Person(const string& name, int age, char gender, const Person* contact) : _name(name), _age(age), _gender(gender), _contact(contact) {}
    
public:
    string getName() {
        return _name;
    }
    int getAge() {
        return _age;
    }
    char getGender() {
        return _gender;
    }

private:
    string _name;
    int _age;
    char _gender;
    const Person* _contact;
    /*Person* getPerson() {
        return _contact;
    }*/
};

int main() {
    Person jerry("Jerry", 45, 'M', nullptr);
    Person simon("simon", 58, 'M', nullptr);

    printf("Hi! My name is %s.\n I am %d years old.\n I am", jerry.getName(), jerry.getAge());
    printf((jerry.getGender() == 'M') ? " a man.\n" : " a woman.\n", jerry.getGender());
    printf("I happen to know ");
}

How would I access the pointer contact in the Person class?

  • Which OS and platform? – user107511 Aug 10 '21 at 06:00
  • If it's c++ why are you using malloc ? – John3136 Aug 10 '21 at 06:02
  • 2
    Can't reproduce with gcc. As to problems you have not described, he first `printf()` call has undefined behaviour because it contains two format specifiers, but no corresponding arguments. – Peter Aug 10 '21 at 06:02
  • My guess is that the malloc fails because of the string inside the struct. Most probably, string uses `new` under the hood, and mixing `malloc` and `new` is bad. And it crashes on my machine/online compiler. – Hafnernuss Aug 10 '21 at 06:05

1 Answers1

0

When you are using malloc to allocate your struct, you don't run the ctor for std::string.

You should create a ctor for your struct where you initialize correctly the string, and create the object using the ctor and not malloc (use new and delete when creating a pointer).

a code sample for defining the class:

class Person {
public:
    Person(const string& name, int age, char gender, const Person* contact) : _name(name), _age(age), _gender(gender), _contact(contact) {}

private:
    string _name;
    int _age;
    char _gender;
    const Person* _contact;
};

a code sample for using the class:

Person jerry("Jerry", 45, 'M', nullptr);
Person jerry2("Jerry2", 45, 'M', &jerry);

Person* jerry3 = new Person("Jerry3", 45, 'M', &jerry);
delete jerry3;

The usage of pointer for contact may be unsafe, and should be changed.

user107511
  • 772
  • 3
  • 23