1

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;
}
1
  • Thank you every one, i accepted Björn's answer because he was the first one to reply. Commented Nov 27, 2011 at 20:13

5 Answers 5

4

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.

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

Comments

3

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.

Comments

1

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.

Comments

1

You initialized an array of USR*s, but you haven't initialized the single USR* objects.

Comments

0

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];
    }
};

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.