From b351d6471aa7fda52a45ea8c93308eb91aec7f32 Mon Sep 17 00:00:00 2001 From: Dominik Kaiser Date: Sat, 18 Jan 2025 11:34:04 +0100 Subject: [PATCH] Fix wrong allocation size --- philo/src/main.c | 11 +++++++---- philo/src/simulation.c | 12 ++++++++++-- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/philo/src/main.c b/philo/src/main.c index 0043e04..ca339bb 100644 --- a/philo/src/main.c +++ b/philo/src/main.c @@ -6,7 +6,7 @@ /* By: dkaiser nbr_of_philos); if (*philos == NULL) return (ft_err(ERR_MALLOC)); - data->forks = (t_fork *)malloc(sizeof(int) * data->nbr_of_philos); + data->forks = (t_fork *)malloc(sizeof(t_fork) * data->nbr_of_philos); if (data->forks == NULL) { free(*philos); @@ -55,7 +56,9 @@ int init(t_philo **philos, t_phdata *data) (*philos)[i].times_eaten = 0; (*philos)[i].data = data; data->forks[i].available = 1; - pthread_mutex_init(&(data->forks[i].mutex), NULL); + result = pthread_mutex_init(&(data->forks[i].mutex), NULL); + if (result != 0) + return (result); i++; } return (EXIT_SUCCESS); @@ -75,8 +78,8 @@ int main(int argc, char *argv[]) result = init(&philos, &data); if (result != EXIT_SUCCESS) return (result); - free(philos); run_simulation(data.nbr_of_philos, philos); + free(philos); free(data.forks); return (result); } diff --git a/philo/src/simulation.c b/philo/src/simulation.c index 011af9b..a30c7d2 100644 --- a/philo/src/simulation.c +++ b/philo/src/simulation.c @@ -6,13 +6,21 @@ /* By: dkaiser