重新分配后组织指针数组

时间:2019-01-04 01:26:20

标签: c realloc

我创建了一个动态列表:

结构为:

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

在创建User ** headMan之后,我通过以下方式添加用户:

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)++;
    }

}

现在,当我尝试删除我使用的用户时,现在在用户列表上移动一个索引,例如headMan [i]:

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);
} 

freeUser释放结构的所有字段时,现在我不明白重新分配的工作原理。

如果我有5个用户的列表,并且在第3个位置删除了一个用户,那么使用realloc会将列表大小调整为4?谁将排在第三位?

1 个答案:

答案 0 :(得分:4)

realloc()仅调整分配的内存块的大小,不会移动任何内容。你必须自己做。

因此,如果您的数组如下所示:

{ A, B, C, D, E }

然后您删除B,它看起来像这样:

{ A, empty, C, D, E }

然后,您必须移动CDE,使其看起来像这样:

{ A, C, D, E, empty }

然后您才能调用realloc()来缩小它,所以它看起来像这样:

{ A, C, D, E }

关于realloc()的两件事要记住:

  1. 缩小块时,修剪空间中的任何内容都会永远丢失。
  2. 扩展块时,新扩展空间的内容是垃圾。使用前请确保已初始化。

在两种情况下,都将复制适合调整大小区域的原始数据。