Copying member function pointer arrays

I am trying to create an array of function pointers, with the ability to add functions. So I have to expand my array with each new function. My only problem is that every time I add a function only the last function I add executes, the other ones just crash, which my guess is that it did not correctly assign the pointer value. Here is my function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
void MyClass::AddCommand(String command,FUNCPTR function)
{
    //Get original size
    int commandListSize = m_commandList.GetCount();

    //First make a copy, should have [commandListSize+1]
    FUNCPTR* m_commandFunctionsCopy = m_commandFunctions;

    delete m_commandFunctions;

    //not make sure we make a new array with the correct size
    //Size has to add one to old expected size, so [commandListSize+2]
    m_commandFunctions = new FUNCPTR[commandListSize+2];

    //Add a command to the list
    m_commandList.Add(command);

    //Fill all the slots with the correct functions
    for(int x=0;x < commandListSize;x++)
    {
        m_commandFunctions[x] = m_commandFunctionsCopy[x];
        //memcpy((char*)&m_commandFunctions[x],(char*)&m_commandFunctionsCopy[x],sizeof(m_commandFunctionsCopy[x]));
    }

    //fill it with the new function and a NULL ending
    m_commandFunctions[commandListSize] = function;
    m_commandFunctions[commandListSize+1] = '\0';
}


Thanks
Line 7 only copies the pointer, not the array the pointer points to.

Why don't you just use a vector? Or perhaps a map if you need to link a command name to a function pointer.
Last edited on
You are also freeing the memory held by the previous array on line 9 and then you are trying to copy the elements out of that array on line 21.

Though I agree with helios' solution.
Topic archived. No new replies allowed.