0

i try to implement a split function to split a string with a caracter like in java. But my function need to stock the splited string in tab with 2 dimensions (the tab is in parameters). And my function need to return the numbers of arg.

So i pass an address of char ** at my function and i want to realloc this char ** But it doesn't work.

int split(char*** parsedCommand,const char* splitCaracter,const char* myString) {
    char* splittedPart = "";
    char* copyOfmyString = NULL;
    int argc = 0;

    copyOfmyString = strdup(myString);
    splittedPart = strtok(copyOfmyString, splitCaracter);

    while(splittedPart != NULL)
    {
        *parsedCommand = (char**) realloc(*parsedCommand, sizeof(char*)*(argc+1));
        if (*parsedCommand == NULL)
        {
            printf("fatalError ");
            exit(-1);
        }
        (*parsedCommand)[argc] = strdup(splittedPart);

        if ((*parsedCommand)[argc] == NULL)
        {
            printf("fatalError ");
            exit(-1);
        }

        splittedPart = strtok (NULL, splitCaracter);

        argc++;
    }

    free(copyOfmyString);
    return argc;
}

I have an Segmentation fault when i try to split a string with 2 arg like "ls | ls", the first ls was stock well in the tab, but for the second one we gat an segmentation falt at strdup(splittedPart).

I hope you understand my English, i'm not really good at it. Thx.

EDIT : i forgot i call the function with :

char ** cmd_membres = NULL;
split(&cmd_membres, "|", chaine);
Alexandre C.
  • 55,948
  • 11
  • 128
  • 197
Timo
  • 497
  • 10
  • 21
  • `malloc(sizeof(myString))` - you're *sure* you want the size of a *pointer* there? Methinks not. `strlen(myString)+1` seems a more appropriate fit. Or just do as you did further below: use `strdup()` – WhozCraig Jan 10 '15 at 00:01
  • My bad thx. But my problem still the same ^^ – Timo Jan 10 '15 at 00:39
  • Note that the pattern `variable = realloc(variable, new_size);` where the same variable is assigned the result, is a very bad idea. The trouble is that if you get `NULL` returned from `realloc()`, you've lost access to the original space -- a memory leak (and when it is a pointer to an array of pointers to allocated memory that's lost, then that's a lot of memory leaked). Always use the pattern `new_space = realloc(old_space, new_size); if (new_space == 0) { ...handle error... } old_space = new_space;`. – Jonathan Leffler Jan 10 '15 at 01:14
  • I agree with you. But in my case i don't think i need a second pointer. Because if the allocation failed i need to stop my program so i can use for instance `exit(-1)` in my test ? – Timo Jan 10 '15 at 09:55

1 Answers1

2

You're running into a precedence error; *parsedCommand[argc] is being interpreted as *(parsedCommand[argc]) instead of what it looks like you want, (*parsedCommand)[argc].

Scott Hunter
  • 48,888
  • 12
  • 60
  • 101
  • If i do like you said `(*parsedCommand)[argc] strdup(splittedPart);` It's the same error. And like i said i don't understand why it's work for the first value... When i lauchn my debug tool i can see `*parsedCommand[0] = ls` but [1] impossible even after realloc... – Timo Jan 10 '15 at 00:35
  • 1
    When `argc` is 0, `(*parsedCommand)[argc]` and `*(parsedCommand[argc])` are the same thing. – Scott Hunter Jan 10 '15 at 01:51
  • Your right. Thx that works fine !! I will edit my post with the correction. – Timo Jan 10 '15 at 09:56