0

I'm trying to add an object to an object array passed as an argument. Specifically, I have an array of buttons and I am adding a back button to the button array. How can I correctly do this? When I try the code below I get some weird glitches with the buttons passed from the original pointer array.

int createMenu(Button buttons[], int size)
{
    //Stuff here
}

int menu(Button buttons[], int size, bool back)
{
    Button * newButtons[size + 1];
    for (int i = 1; i <= size; i++)
        *newButtons[i] = buttons[i];
    Button back(25, 19, "Back"); //Creates back button object
    *newButtons[0] = back;
    return createMenu(*newButtons, size + 1);
    //Makes menu with the back button
}

Any help is appreciated.

8
  • 4
    Is it necessary to use arrays? In general avoid them and use vectors. Commented Jun 29, 2014 at 5:26
  • 1
    I never looked in to them. Would they be more or less memory-heavy? I am running this on a device with 3MB of RAM. Commented Jun 29, 2014 at 5:28
  • Yes, they would consume more memory than arrays. Probably in your situation it makes sense to use Arrays I guess. Commented Jun 29, 2014 at 5:29
  • Show the declaration of createMenu Commented Jun 29, 2014 at 5:35
  • 2
    BTW arrays must have size known at compile-time. Button *newButtons[size+1]; is illegal (although some compilers offer this as an extension). Also, it is not possible to resize them at runtime. Commented Jun 29, 2014 at 5:38

3 Answers 3

2

In your loop you reference buttons[size], which is ouside of its bounds. You also dereference members of newButtons without initializing them. And you try to pass createMenu an array of pointers, when it expects an array of buttons. Should be something like this:

int menu(Button buttons[], int size, bool back)
{
    Button * newButtons = new Button[size + 1];
    for (int i = 0; i < size; i++)
        newButtons[i + 1] = buttons[i];
    newButtons[0] = Button(25, 19, "Back");
    int result = createMenu(newButtons, size + 1);
    delete [] newButtons;
    return result;
}
Sign up to request clarification or add additional context in comments.

1 Comment

delete newButtons; should be delete [] newButtons;
1

For reference, here's how it would look if you were using vectors:

int menu( std::vector<Button> buttons )
{
    buttons.push_back( Button(25, 19, "Back") );
    return createMenu( buttons );
}

If the button really needs to be pushed at the front then there are various options (e.g. actually push it at the front; or use a deque instead of a vector).

Comments

0

Try this. You are using Button*, so pass address of the Button object.

int menu(Button buttons[], int size, bool back)
{

    Button ** newButtons = new Button*[size + 1];
for (int i = 1; i <= size; i++)
        newButtons[i] = &buttons[i];
    Button * back = new Button(9,11,"fdf"); //Creates back button object
    newButtons[0] = back;

    //Makes menu with the back button
createMenu(*newButtons, size+1);
}

void createMenu(Button buttons[], int size)
{
    (buttons[0]).foo();

    //Stuff here
}

Also, you are using the same variable name "back" both as bool and as object of Button class. Change that.

8 Comments

I still get strange results with this. Maybe it's something related to my compiler.
This stores the address of a local variable
@MattMcNabb, thanks for that! That was unforgivable.
I still don't get the expected results. But it looks like your answer should work.
Are you able to compile now? What is the error you get?
|

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.