Skip to content
Snippets Groups Projects
Commit c1bab9e8 authored by ismail.abloua's avatar ismail.abloua
Browse files

dynamic memory alloc

parent cbf4ddc6
Branches
No related tags found
No related merge requests found
......@@ -20,7 +20,7 @@ typedef struct {
} Site;
// Global Variables
Site sites[MAX_SITES];
Site* sites = NULL;
int S, H, M, N;
int depot = 0;
int truck = 2;
......@@ -150,6 +150,13 @@ int main(int argc, char* argv[]) {
// random init
srand(time(NULL));
// Allocate memory for sites
sites = malloc(S * sizeof(Site));
if (sites == NULL) {
perror("malloc");
return EXIT_FAILURE;
}
// Initialize sites
for (int i = 0; i < S; i++) {
sites[i].nb_bikes = N - 2;
......@@ -158,7 +165,16 @@ int main(int argc, char* argv[]) {
sem_init(&sites[i].sem_slots, 0, N - sites[i].nb_bikes);
}
pthread_t habitants[H], maintenance;
pthread_t* habitants = NULL;
pthread_t maintenance;
// Allocate memory for habitants
habitants = malloc(H * sizeof(pthread_t));
if (habitants == NULL) {
perror("malloc");
free(sites);
return EXIT_FAILURE;
}
// Start maintenance thread
pthread_create(&maintenance, NULL, thread_maintenance, NULL);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment