0

I have a dynamic array of strings, for example "123", "231" etc. Next I need to delete a particular array string. For the deletion I used the normal technique used for static arrays, that is to move all the elements to the left by one position, in order to overwrite the deleted element. But since the array is dynamic, how do I fix the memory? That is, to decrement the memory this array is using? Here is my code: PART OF CODE TO INSERT A STRING:

char **received_messages;

if(array_size == 0){
    debug_level == 2 && printf("First message received..\n");
    received_messages = malloc(sizeof(char*));
    received_messages[0] = malloc((1024+1) * sizeof(char));
    array_size = array_size + 1;
    strcpy(received_messages[0],id);
}
else{

for(int i=0; i<array_size; i++){
    debug_level == 2 && printf("Compare between %s and %s\n", received_messages[i],id);
    if( (strcmp(received_messages[i],id)) == 0){
        id_found= 1;
    }
}

if(id_found== 1){
    debug_level == 2 && printf("ID found.\n");
}else{
    debug_level == 2 && printf("ID not found.\n");
    received_messages[array_size] = malloc((1024+1) * sizeof(char));
    strcpy(received_messages[array_size],id);
    array_size = array_size + 1;
}

}

PART OF CODE TO DELETE A STRING:

    for(int j=0; j<array_size;j++){
        printf("I'm searching the SN to delete. Compare %s to %s\n", received_messages[j], id_to_delete);
        if(strstr(received_messages[j], id_to_delete) != NULL){
        printf("Sequence number found. It is %s\n", id_to_delete);
        for(int k=j; k<array_size; k++){
            received_messages[k] = received_messages[k+1];
            array_size = array_size -1;
        }
    } 
    }
    printf("Now, the array has %d elements. Actually valid sequence number are: \n", array_size);
    if(array_size > 0){
    for(int j=0; j<array_size; j++){
        printf("%d) %s\n", j+1, received_messages[j]);
    }
    }else{
        printf("Actually no valid sequence number.\n");
    }
5
  • When you delete an element, you never free the memory have allocated for it. Commented Sep 9, 2021 at 7:53
  • You also have a much worse problem than the memory leak from not freeing memory: You never reallocate the array received_messages itself when you increase its size. That means for any index except 0 you will go out of bounds and have undefined behavior. Commented Sep 9, 2021 at 7:54
  • Unfortunately I know, in fact I would like to know how to do it. I don't know how to free up the memory for the item that was deleted. Commented Sep 9, 2021 at 7:55
  • If you don't need the strings to be stored in contiguous memory and you're looking at adding or removing a lot of strings, may I suggest that you use a linked list? That way it would ease your memory allocation/deallocation as you add and remove strings from the list. Commented Sep 9, 2021 at 7:56
  • Before moving the contents of the received_messages array, call free(received_messages[j]) Commented Sep 9, 2021 at 7:58

1 Answer 1

1

For starters this code snippet is incorrect if the memory for the array pointed to by the pointer received_messages was not reallocated

}else{
    debug_level == 2 && printf("ID not found.\n");
    received_messages[array_size] = malloc((1024+1) * sizeof(char));
    strcpy(received_messages[array_size],id);
    array_size = array_size + 1;
}

If previously there were allocated array_size elements then using the index array_size results in accessing memory beyond the allocated array.

In this code snippet

for(int j=0; j<array_size;j++){
    printf("I'm searching the SN to delete. Compare %s to %s\n", received_messages[j], id_to_delete);
    if(strstr(received_messages[j], id_to_delete) != NULL){
    printf("Sequence number found. It is %s\n", id_to_delete);
    for(int k=j; k<array_size; k++){
        received_messages[k] = received_messages[k+1];
        array_size = array_size -1;
    }
} 

it is unclear why you are using the function strstr instead of the function strcmp.

The inner for loop

    for(int k=j; k<array_size; k++){
        received_messages[k] = received_messages[k+1];
        array_size = array_size -1;
    }

has to be moved from the outer for loop. Moreover this statement

array_size = array_size -1;

has to be moved from this inner loop. Within the loop it does not make a sense because for each iteration of the loop the variable array_size is decremented.

This statement

received_messages[k] = received_messages[k+1];

invokes undefined behavior due to the index expression k + 1 when k is equal to array_size - 1 because in this case the index expression is equal to array_size that is not a valid index for the array containing array_size elements.

Instead of the loop you could use the function memmove and then the function realloc to decrease the allocated memory due to removing an element from the array.

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

Comments

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.