1

Lets say I have a class player, which has the member variables num_houses and a pointer temp1 of type house

class player{
private:
    int num_houses;
    house* temp1;
public:
    player(){
    num_houses = 0;
    temp1 = new house[num_houses];
}

My question lies with my code when I try to resize or basically add/remove an element of type house to my array of houses. In my code I happen to be getting a segmentation fault. I'm confused and I am a student so any advice or criticism will help me dearly!

void player::add_house(house tkn){
    house* temp = new house[num_houses];
    for(int i = 0; i < num_houses; i++){
        temp[i] = temp1[i];
    } 
    num_houses++;
    if(temp1 != NULL){
        delete [] temp1;
    }
    temp1 = new house[num_houses];
    for(int i = 0; i < num_houses-1; i++){
        temp1[i] = temp[i];
    }
    temp1[num_houses-1] = tkn;
    delete [] temp;
}
8
  • Since this can't possible compile in C, why did you add that tag? Please don't do that. Only add the language tag of the language you're actually programming in. Commented May 9, 2018 at 5:47
  • 4
    As for your problem, whenever you think "dynamic array" you next thought should always be std::vector. Commented May 9, 2018 at 5:47
  • 1
    I am restricted from using std::vector Commented May 9, 2018 at 5:48
  • This code doesn't work for me because temp2 isn't defined. Commented May 9, 2018 at 5:48
  • 1
    Think about it: if you want to grow the array, you only need to allocate a new array once. Not twice. Commented May 9, 2018 at 5:49

2 Answers 2

1
temp1 = new house[num_houses];

is a problem when num_houses is 0.

Change the default constructor to:

player() : num_houses(0), temp1(nullptr) {}

You can simplify the member function add_house so it requires one less pair of new/delete.

void player::add_house(house tkn){

    num_houses++;
    house* temp = new house[num_houses];
    for(int i = 0; i < num_houses-1; i++){
        temp[i] = temp1[i];
    } 

    temp[num_houses-1] = tkn;

    if(temp1 != nullptr){
        delete [] temp1;
    }

    temp1 = temp;
}
Sign up to request clarification or add additional context in comments.

7 Comments

Thanks for the advice, I would also like to add that my copy constructor, destructor and assignment operator overload are all created for my house class. Do you think that there could be a issue with shallow vs deep copy in this situation? I have been getting a "pointer being freed was not allocated" error in my program.
@RobertYelas, most likely. Make sure to follow The Rule of Three.
small remark, I think that the check if(temp1 != nullptr) we are not needed. We can safely call delete with the value of null_ptr .
@AlexceiShmakov, The C++11 standard is vague about what happens when the argument to delete is a nullptr. See stackoverflow.com/questions/25329576/….
@RSahu, thank you for the intresting link. I did not know about it.
|
1

For increasing array size rewrite your algo to perform these steps:

  1. Allocate new temp array with size of current+1.
  2. Copy everything from current array to temp array.
  3. Increment houses amount.
  4. delete current array.
  5. Save the pointer to temp array to the current array pointer.
  6. Add new house to the end of current array.

Of course you can reorder this list if you understand how (4 is always after 1, 6 is always after 1, 5 is always after 4 etc.).

P.S. you don't have to check if pointer is not null before deleting it - deleting nullptr won't fail.

UPD: added code

void player::add_house(house tkn {
    house* temp = new house[num_houses + 1];
    for(int i = 0; i < num_houses; i++) {
        temp[i] = temp1[i];
    }
    num_houses++;
    delete [] temp1;
    temp1 = temp;
    temp1[num_houses-1] = tkn;
}

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.