-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray_of_structs.c
More file actions
111 lines (96 loc) 路 4.51 KB
/
Copy patharray_of_structs.c
File metadata and controls
111 lines (96 loc) 路 4.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include <stdio.h>
#include <stdlib.h>
/// @brief Custom structure representing an example contact with name and address.
typedef struct Contact {
/// @brief Contact's name
char* name;
/// @brief Contact's address
char* address;
} Contact;
/// @brief Custom structure representing a contact book (array of contacts).
typedef struct ContactBook {
/// @brief Pointer to the contact array
Contact* contacts;
/// @brief Maximum current size of the contact array
/// @details Dynamically regrown with grow_contact_book method
size_t max_size;
/// @brief Current size of the contact array (how many are added)
/// @details Increases automatically whenever new contacts are added
size_t size;
} ContactBook;
const int DEFAULT_ARRAY_SIZE = 4;
/// @brief Initializes a new contact book in the memory.
/// @details Sets the starting maximum size to the value of constant DEFAULT_ARRAY_SIZE.
/// Sets the current array size to 0.
/// @return Pointer to the newly initialized contact book.
ContactBook* initialize_contact_book() {
ContactBook* contact_book = malloc(sizeof(ContactBook));
contact_book->max_size = DEFAULT_ARRAY_SIZE;
contact_book->contacts = malloc(contact_book->max_size * sizeof(Contact));
contact_book->size = 0;
return contact_book;
}
/// @brief Grows the given contact book to double of it's current size.
/// @param contact_book Contact book to grow
void grow_contact_book(ContactBook* contact_book) {
contact_book->max_size *= 2;
contact_book->contacts = realloc(contact_book->contacts, contact_book->max_size * sizeof(Contact));
}
/// @brief Adds new contact to the provided contact book.
/// @details When size runs out in the contact_book, calls the grow_contact_book to automatically create new space.
/// After done writing to the book, the realloc_contact_book method should be called to free all unused memory.
/// @param contact_book The contact book to add new contact to
/// @param contact The new contact to add
void add_contact(ContactBook* contact_book, Contact contact) {
// Check if the book is big enough.
// If not, grow it to double it's size.
if(contact_book->size == contact_book->max_size) {
grow_contact_book(contact_book);
}
// Add the new contact and increase the current size.
contact_book->contacts[contact_book->size] = contact;
contact_book->size++;
}
/// @brief Reallocates the given contact book to the smallest possible memory neeeded.
/// @details Should be called after done writing to the given contact book.
/// @param contact_book Contact book to reallocate
void realloc_contact_book(ContactBook* contact_book) {
contact_book->max_size = contact_book->size;
contact_book->contacts = realloc(contact_book->contacts, contact_book->max_size * sizeof(Contact));
}
/// @brief Frees the given contact book and all of it's content from the memory.
/// @param contact_book Contact book to free from memory
void free_contact_book(ContactBook* contact_book) {
free(contact_book->contacts);
free(contact_book);
}
/// @brief Example usage in the main method.
/// @return True as the output should be always successful :-).
int main() {
// Initialize new contact book
ContactBook* contact_book = initialize_contact_book();
// Add some contacts to the book
// Array begins with size 4 and is automatically regrown (reallocated) to double it's size when space runs up.
Contact contact1 = {"Pavel Fesenko", "Development Street 12"};
Contact contact2 = {"Harry Potter", "Hogwarts, room 164"};
Contact contact3 = {"Joshamie Gibbs", "Tortuga 322"};
Contact contact4 = {"Captain Picard", "Starfleet HQ"};
Contact contact5 = {"Ron Weasley", "Hogwarts, room 164"};
add_contact(contact_book, contact1);
add_contact(contact_book, contact2);
add_contact(contact_book, contact3);
add_contact(contact_book, contact4);
add_contact(contact_book, contact5);
// As we are done writing data into the book, we can now shrink it dynamically to free up additional memory.
// This can be done simply by calling the method below.
realloc_contact_book(contact_book);
// Print the contacts in the book
Contact contact;
for(int i = 0; i < contact_book->size; i++) {
contact = contact_book->contacts[i];
printf("%s - %s\n", contact.name, contact.address);
}
// Finally free all the memory
free_contact_book(contact_book);
return 0;
}