Thông tin

Tác giả Louis Navarre & Alexandre Gobeaux
Hạn chót Không có hạn chót
Giới hạn nộp bài Không có giới hạn
Các tag chuyên mục s3, category_pointer, category_free, category_malloc, level4

Tags

Đăng nhập

[S3] Manipulate the memory

Given the following structure university_t:

typedef struct university {
    char* city;
    int creation;
    person_t* rector;
} university_t;

And the structure person_t:

typedef struct person {
    char* name;
    int salary;
    int age;
} person_t;

You are asked to implement the functions init_all and free_all, which respectively initialises the structure university_t and frees all the memory associated with it.

The name and the city have been allocated with malloc.

Hint: all the data may not have been initialised correctly. Therefore, you have to handle all the cases (e.g. some pointers can be already NULL, and don't need to be freed).


Câu hỏi 1: Init all

Write the body of the function init_all. You must copy the strings.

Hint : Use strcpy.

/*
 * @city != NULL
 * @rectname != NULL
 * @return: a pointer to the university structure containing the elements
 */
 university_t* init_all(char* city, int creation, char* rectname, int age, int salary){
Câu hỏi 2: Free all

Write the body of the function free_all.

/*
 * Frees all the memory associated with u
 * @pre: u != NULL
 */
 void free_all(university_t* u){