1

This is part of my program. parameters.path is a string that contains a path to the file I will be working with, but that is not in this code.

typedef struct directory {

   char *name;
   char *path;

} directory;

void insertDir(directory*** array , char * path, char* name, int* length) {

    directory *p = malloc(sizeof(directory));

    p->path = malloc(strlen(path)+ 1);
    strcpy(p->path, path);

    p->name = malloc(strlen(name)+ 1);
    strcpy(p->name, name);

    *array = (directory**) realloc( *array , (*length) * (sizeof(directory*)));
    *array[(*length)] = p;
    (*length)++;

}

int main(int argc , char** argv) {

    directory** array = NULL;

    int lenght = 0;

    while(true) {
        insertDir(&array, parameters.path, name , &lenght);
    }

    return 0;
}

It fails on the third realloc with segmentation fault. Can you help me please?

1
  • 3
    You should never type cast the return from malloc/realloc/calloc in C. Get in the habit early! Commented May 8, 2015 at 21:06

2 Answers 2

6

When doing the realloc(), you need to add 1 to the length, because you haven't incremented it yet.

*array = realloc( *array , (*length + 1) * (sizeof(directory*)));

You also need to change:

*array[(*length)] = p;

to:

(*array)[*length] = p;

because the subscripting operator has higher precedence than the dereference operator. See the C operator precedence table here. There's also no need for parentheses inside [].

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

8 Comments

thanks, that was a mistake but its interesting that it still works the same way :/
It's not really working, you're causing undefined behavior. Anything can happen when you do that, including "seeming" to work for a while.
it fails even after correction... maybe the mistake isnt here
You're calling insertDir, but the function is named insetrDir. You should be getting an error about an undefined function.
oops, i didnt copy-paste that part, so it was just here... but thanks a lot for help.. i am going to continue analyzing my whole code :D
|
3

In addition to the (*array)[*length] indexing issue, you leave yourself wide open for several allocation failures with both malloc and realloc. First, and generally, always validate that your allocation succeeds. Enough said.

Next, with realloc, it is always better to reallocate using a temporary variable instead of your array. If there is a failure on reallocation, realloc returns NULL causing you to lose the address to array completely. This will cause the loss of all existing data. Instead, if you use a tmp pointer for reallocation, you have the ability to handle the failure in a graceful manner, and the ability to free the block of memory originally assigned to array.

Here is a bit more robust way to handle the allocations/reallocations. Note: you are left to code the response to a reallocation failure:

void insertDir (directory*** array, char *path, char *name, int *length)
{

    directory *p = malloc (sizeof *p);
    if (!p) {
        fprintf (stderr, "error: virtual memory exhausted.\n");
        exit (EXIT_FAILURE);
    }

    p->path = malloc (strlen (path) + 1);
    strcpy (p->path, path);

    p->name = malloc (strlen (name) + 1);
    strcpy(p->name, name);

    directory **tmp = realloc (*array, (*length + 1) * sizeof *tmp);
    if (!tmp) {
        fprintf (stderr, "error: struct reallocation failure.\n");
        exit (EXIT_FAILURE);
    }
    *array = tmp;
    (*array)[*length] = p;
    (*length)++;

}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.