简体   繁体   中英

Segmentation fault in std::string::assign(std::string const&)

i have the indicated problem with the following code and i have no idea what it might be causing it. I searched before posting the problem, and i learned that it might be something going out of the scope like a reference to a freed memory location but i could not find it on my own. Thank you for helping me.

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

class USR{
private:
    string name;
public:
    void setName(string name){  
        this->name = name;
    }
    string getName(){
        return name;
    }

};

class A{
private:
    USR* * a;
public:
    A(int size){
        a = new USR*[size];
    }   
    USR* getUser(){
        return a[0];
    }
};

int main(){
    A test = A(5);
    USR* u = test.getUser(); 
    (*u).setName("test");
    USR* u2 = test.getUser(); 
    cout << (*u2).getName() << endl;
    cout << (*u).getName() << endl;
}

The problem is that you allocated the array of pointers, but you never allocated anything for the pointers themselves.

This gives you the array of pointers:

a = new USR*[size];

But you never allocated anything for each of the pointers.

Therefore, it's crashing here:

(*u).setName("test");

because *u is not initialized.


There are two ways to fix this:

  1. Allocate (and initialize) something for each USR pointer.
  2. Don't use the double pointers. Just use a simple array of USR objects.

I'd prefer the latter since what you have is probably more complicated than it needs to be.

Something like this will probably do what you want:

class A{
private:
    USR *a;
public:
    A(int size){
        a = new USR[size];
    }   
    USR* getUser(){
        return &a[0];
    }
};

Don't forget that you'll want a destructor as well.

Your method getUser returns an uninitialized pointer (the constructor of A creates an array of uninitialized pointers). The error you see is the result of dereferencing the uninitialized pointer that method returned.

You are only creating a new USR* array instead of an array of USR objects. Accessing the pointer in

USR* u = test.getUser();

will give you an unitialized pointer. Calling

(*u).setName("test");

will therefore segfault.

您初始化了USR*的数组,但尚未初始化单个USR*对象。

As you have declared a 2D array of USR, so i think you have requirement of Array of Array of user names, although it looks weird to me why can't you simply use USR *userArray;

Anyway if you want the array of array of user names to work then you need to modify your class A as below:

class A{
private:
    USR* * a;
public:
    A(int size){
        a = new USR*[size];
        int iter = 0;
        for(; iter < size; iter++)
        {
           a[iter] = new USR[size_of_user_names_for_each_user_array];
        }
    }   
    USR* getUser(){
        return a[0];
    }
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM