0

I'm trying to implement a dynamic array of strings. However I encountered a slight problem. I don't allocate the memory properly, but I have no idea what am doing wrong.

My structure for the dynamic array looks like:

typedef struct Array {
    char **ids;
    int size;
} dynamicArray;

I initialize it with:

void initArray(dynamicArray *array) {
    array = malloc(sizeof(dynamicArray));
    array->ids = NULL;
    array->size = 0;
}

Deallocate by:

void freeArray(dynamicArray *array) {
    if (array->size != 0) {
        for (int i = 0; i < array->size; i++) {
            free(array->ids[i]);
        }
        array->size = 0;
        array->ids = NULL;
    }
}

But now the real problem for me is inserting:

void insertArray(dynamicArray *array, char *name) {
    if (array == NULL) {
        return;
    }
    int length = strlen(name) + 1;
    array = realloc(array, (??));
    strcpy(array->ids[array->size++], name);
}

The program fails on the reallocation with: Exception has occurred.. I'm really not sure, what am I doing wrong. I know I should be also allocating the array of string, but have no idea how to put it in there. Could you guys please send me any hints??

9
  • 2
    initArray() is not returning the pointer to its caller. Commented Nov 30, 2020 at 22:59
  • Show how you're calling initArray(). Commented Nov 30, 2020 at 23:02
  • If initArray() is supposed to allocate the dynamicArray structure, see stackoverflow.com/questions/13431108/…. If you're calling it with the address of an existing struct, then it shouldn't call malloc(). Commented Nov 30, 2020 at 23:03
  • I have dynamicArray array; followed by initArray(&array); Commented Nov 30, 2020 at 23:07
  • And that variable is declared dynamicArray array;? Then you don't need to call malloc(), the memory is already in the variable. Commented Nov 30, 2020 at 23:09

1 Answer 1

2

The pointer that you need to reallocate is array->ids, not array. When you insert into the array you increase its size. Then the new element points to a copy of the name string.

void insertArray(dynamicArray *array, char *name) {
    if (array == NULL) {
        return;
    }
    int length = strlen(name) + 1;
    char *copy = malloc(length);
    if (copy == NULL) { // malloc failed
        return;
    }
    strcpy(copy, name);
    char **new_ids = realloc(array->ids, (array->size+1) * sizeof(char *));
    if (new_ids == NULL) { // realloc failed
        return;
    }
    array->ids = new_ids;
    array->ids[array->size++] = copy;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much, it works just fine now.

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.