1

I have created a dynamic list:

The struct is:

typedef struct{
    char *id;
    char *name;
    char *surname;
    int age;
    char gender;
    char *username;
    char *password;
    char *description;
    char *hobbies;
}User;

And after I create User **headMan I add users via:

void newMan(User **headMan, int *size, char *id, char *name, char *surname,
            int age, char gender, char *username,
            char *password, char *description, char *hobbies){
    if(*size == 0){
        *headMan = (User*)malloc(sizeof(User));
        if(*headMan == NULL){
            printf("Allocation of (*headMan) failed\n");
            exit(1);
        }
        (*headMan) -> id = (char*)malloc(ID_LENGTH*sizeof(char));
        if ((*headMan) -> id == NULL){
            printf("Allocation of (*headMan) -> id failed\n");
            exit(1);
        }
        strcpy((*headMan) -> id,id);
        (*headMan) -> name = (char*)malloc(NAME_LENGTH*sizeof(char));
        if ((*headMan) -> name == NULL){
            printf("Allocation of (*headMan) -> name failed\n");
            exit(1);
        }
        strcpy((*headMan) -> name,name);
        (*headMan) -> surname = (char*)malloc(NAME_LENGTH*sizeof(char));
        if ((*headMan) -> surname == NULL){
            printf("Allocation of (*headMan) -> surname failed\n");
            exit(1);
        }
        strcpy((*headMan) -> surname, surname);
        (*headMan) -> age = age;
        (*headMan) -> gender = gender;

        (*headMan) -> username = (char*)malloc(MAX*sizeof(char));
        if ((*headMan) -> username == NULL){
            printf("Allocation of (*headMan) -> username failed\n");
            exit(1);
        }
        strcpy((*headMan) -> username, username);

        (*headMan) -> password = (char*)malloc(NAME_LENGTH*sizeof(char));
        if ((*headMan) -> password == NULL){
            printf("Allocation of (*headMan) -> password failed\n");
            exit(1);
        }
        strcpy((*headMan) -> password, password);

        (*headMan) -> description = (char*)malloc(DESCRIPTION*sizeof(char));
        if ((*headMan) -> description == NULL){
            printf("Allocation of (*headMan) -> description failed\n");
            exit(1);
        }
        strcpy((*headMan) -> description, description);

        (*headMan) -> hobbies = (char*)malloc(NAME_LENGTH*sizeof(char));
        if ((*headMan) -> hobbies == NULL){
            printf("Allocation of (*headMan) -> hobbies  failed\n");
            exit(1);
        }
        strcpy((*headMan) -> hobbies, hobbies);
        (*size)++;
    }
    else{
        headMan[*size] = (User*)malloc(sizeof(User));
        if(headMan[*size] == NULL){
            printf("Allocation of headMan[*size] failed\n");
            exit(1);
        }
        headMan[*size] -> id = (char*)malloc(ID_LENGTH*sizeof(char));
        if (headMan[*size] -> id == NULL){
            printf("Allocation of headMan[*size] -> id failed\n");
            exit(1);
        }
        strcpy(headMan[*size] -> id,id);
        headMan[*size] -> name = (char*)malloc(NAME_LENGTH*sizeof(char));
        if (headMan[*size] -> name == NULL){
            printf("Allocation of headMan[*size] -> name failed\n");
            exit(1);
        }
        strcpy(headMan[*size] -> name,name);
        headMan[*size] -> surname = (char*)malloc(NAME_LENGTH*sizeof(char));
        if (headMan[*size] -> surname == NULL){
            printf("Allocation of headMan[*size] -> surname failed\n");
            exit(1);
        }
        strcpy(headMan[*size] -> surname, surname);
        headMan[*size] -> age = age;
        headMan[*size] -> gender = gender;

        headMan[*size] -> username = (char*)malloc(MAX*sizeof(char));
        if (headMan[*size] -> username == NULL){
            printf("Allocation of headMan[*size] -> username failed\n");
            exit(1);
        }
        strcpy(headMan[*size] -> username, username);

        headMan[*size] -> password = (char*)malloc(NAME_LENGTH*sizeof(char));
        if (headMan[*size] -> password == NULL){
            printf("Allocation of headMan[*size] -> password failed\n");
            exit(1);
        }
        strcpy(headMan[*size] -> password, password);

        headMan[*size] -> description = (char*)malloc(DESCRIPTION*sizeof(char));
        if (headMan[*size] -> description == NULL){
            printf("Allocation of headMan[*size] -> description failed\n");
            exit(1);
        }
        strcpy(headMan[*size] -> description, description);

        headMan[*size] -> hobbies = (char*)malloc(NAME_LENGTH*sizeof(char));
        if (headMan[*size] -> hobbies == NULL){
            printf("Allocation of headMan[*size] -> hobbies  failed\n");
            exit(1);
        }
        strcpy(headMan[*size] -> hobbies, hobbies);
        (*size)++;
    }

}

Now I move on the user list with an index for example headMan[i] now when I try to remove a user I use:

void removeMan(User** head, int* numberOfMen,char*  existUser){

    if ((strcmp(head[1] ->username,existUser) == 0)){
        freeUser(head[1]);
        free(head[1]);
        head = (User**)realloc(head, (*numberOfMen-1)*sizeof(User));
    }
    printListMen(head,numberOfMen);
} 

When freeUser frees all the fields of the struct, now I can not understand how the realloc works.

If I have a list of 5 users and I remove the one in the 3rd place, using realloc will move the resize the list to 4? who will be listed in 3rd place?

gbox
  • 809
  • 1
  • 8
  • 22
  • [Don't cast the result of malloc in C](https://stackoverflow.com/q/605845/995714). And `->` should be used without spaces around it, like the `.` operator – phuclv Jan 04 '19 at 03:20

1 Answers1

4

realloc() only resizes the allocated block of memory, it doesn't move anything around. You have to do that yourself.

So if you have your array looking like this:

{ A, B, C, D, E }

and you then remove B, it'll look like this:

{ A, empty, C, D, E }

You then have to move C, D and E so it looks like this:

{ A, C, D, E, empty }

Only then can you call realloc() to shrink it, so it looks like this:

{ A, C, D, E }

Two things to remember about realloc():

  1. When you shrink a block, whatever was in the space that was trimmed is lost forever.
  2. When you expand a block, the contents of the newly expanded space are garbage. Make sure you initialize before using.

In both cases, the original data that fits in the resized area are copied over.

dgnuff
  • 3,195
  • 2
  • 18
  • 32