0

I'm trying to pass a pointer to an array of structs to a function. Then have the function create its own array of structures, populate it with data and then overwright the the old array with the new array.

I'm pretty sure the problem is with occurring when I try to overwrite the memory. I think that I could be either using the wrong method to overwrite the memory(should I be using the memory functions?) or I might be trying to overwrite the wrong thing. I'm not quite sure what I'm doing wrong. If someone could point me in the right direction I would be extremely thankful; I've been pulling the hair out of my head for like three hours now.

Struct:

typedef struct
{
        char command;
        int argc;
        char* argv[];
}cmd;

Code:

int main(int argc, char *argv[])
{
    [... irrelevant code]
    cmd cmdv[count];
    cmd* cmdv_ptr = &cmdv[0];
    dissectCmd(cmdstr, cmdv_ptr);
    printf("%i", cmdv[0].argc);
    return 0;
}

void dissectCmd(char* str, cmd* cmdv)
{
    [... irrelevant code]
    cmd cmds[count];
    int i = 0;
    for(i = 0; i < count; i++)
    {
        cmd next;
        next.command = 'u';
        next.argc = 100;
        cmds[i] = next;
    }
    cmdv = cmds;
}
1
  • cmd cmds[count] is a local VLA array, isn't it? even if you pass cmd **cmdv and *cmdv = cmds, it won't work. Commented Jan 6, 2011 at 3:37

2 Answers 2

3

You're not overwriting the memory -- the statement cmdv = cmds just copies the pointer (making cmdv point at cmds.) If you want to actually copy the memory, you need memcpy(cmdv, cmds, count * sizeof(cmd));

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

2 Comments

... or you could just do cmdv[i] = next; in the loop and do away with cmds entirely.
@caf, could work, but only if there will never be any premature returns or exceptions inside that loop. Otherwise it could result in a partially overwritten array which isn't always desired.
-1

i'm not sure about this, but try declaring

void dissectCmd(char* str, cmd* cmdv)

as

void dissectCmd(char* str, cmd** cmdv)

changing

cmdv = cmds;

to

(*cmdv) = &cmds;

and changing current call from main which is now

dissectCmd(cmdstr, cmdv_ptr);

to

dissectCmd(cmdstr, &cmdv_ptr);

also, when you do this, you loose the address to the old array completely, creating a memory leak. unless you already free that memory in your second [... irrelevant code] passage :)

3 Comments

right, this won't work.. you should look at chris dodd's solution. my version might work though if you change printf("%i", cmdv[0].argc); to printf("%i", (*cmdv_ptr)[0].argc); D:
This will leave cmdv_ptr in main dangling, pointing at the memory for the local cmds in dissectCmd's stack frame which was freed when dissectCmd returned
true! i missed the fact that the cmd structs inside dissectCmd wasn't new:ed :(

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.