1

this is a part of an assignment for my programming class. the teacher wanted us to create a couple of functions, one of which would add elements to an existing dynamic array of structures, and this is what I have troubles with.

here's my understanding of how the function should work, based on different posts I found online:

  1. create a new array, bigger than the one already existing

  2. copy the content of the old array to the new array

  3. add the new element to the new array

  4. destroy the old array

however, something is wrong, and the program crashes - I think the problem lies in the way I'm trying to do points 3 and 4. Can someone take a look? I'd really appreciate any kind of help.

edit: forgot to mention, the teacher wants the functions set to void, they are supposed to not return anything.

Here is the code:

const int size = 2;

struct Player {
    string name;
    string kind;
};

void addplayer(Player * plarr, int size) {

    cout << "Adding a new element to the array" << endl << endl;

    //creating a new, bigger array:
    Player * temp = NULL;
    temp = new Player[size+1];

    //copying the content of the old array
    for (int i=0;i<size;i++) {
        temp[i].name = plarr[i].name;
        temp[i].kind = plarr[i].kind;   
    }

    //adding the new element:
    string name, kind;
    cout << "Choose the name for the new player: " << endl;
    cin >> name;
    cout << "Choose the class for the new player: " << endl;
    cin >> kind;

    temp[size+1].name = name;
    temp[size+1].kind = kind;

    //deleting the old array, replacing it with the new one
    delete[] plarr;     
    plarr = temp; 

}
6
  • 3
    Why not use std::vector - hard work done for you! Commented Nov 13, 2016 at 15:41
  • @EdHeal the teacher prohibited it, sadly Commented Nov 13, 2016 at 15:48
  • @EdHeal: Wouldn't be terribly instructive in how dynamic allocation works under the hood, would it? Commented Nov 13, 2016 at 15:57
  • Not your problem, but things are easier in the long term if you use size_t rather than int for array sizes. It's much more compatible with other bits of C++. Commented Nov 13, 2016 at 16:03
  • 1
    Also look at RAII. In this case you could build a Player_array class to manage and delete the array automatically on destruction. Commented Nov 13, 2016 at 17:10

1 Answer 1

4
void addplayer(Player * plarr, int size) {

The plarr parameter is passed to this function by value.

This function appears to allocate a new array and copy over the contents correctly, except for one error:

temp[size+1].name = name;
temp[size+1].kind = kind;

The index should be size, here. But the biggest error is that the function concludes with:

    delete[] plarr;     
    plarr = temp; 
}

Unfortunately, since plarr was passed by value, all this does is set the plarr parameter to this function to the new pointer, and returns.

Which accomplishes absolutely nothing, since the caller to this function still has its original pointer. Which is now destroyed.

You should change this function to return the new pointer, which the caller will need to save, instead of the original pointer.

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

4 Comments

sadly, the teacher wants us to write functions that return nothing. in other words, they should all have void type - sorry, I probably should have mentioned this in my question :( Thank you so much for your help anyway!
If the OP returns the value of the new pointer, the call will have to become plarr = newPlayer(plarr, size++);. Personally, I would pass both the pointer and the size by reference, increment the size in the function, so the declaration is void newPlayer( Player*&plarr, size_t &size).
If your incompetent teacher wants you to write bad code, your only option is to pass the parameter by reference, instead of value.
yes, passing by reference worked, thanks so much everyone!

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.