Fix allocation of wrong size
authorLukas Jiriste <ljiriste@student.42prague.com>
Thu, 28 Mar 2024 11:01:32 +0000 (12:01 +0100)
committerLukas Jiriste <ljiriste@student.42prague.com>
Thu, 28 Mar 2024 11:01:32 +0000 (12:01 +0100)
The way the allocation was written, it allocated space for pointer
instead of the structure pointed to. This has been corrected.

philo/mem_management.c

index 6ac0713fdd75d83fa8b998abead461500201f980..49b3b6dafbb8c7de237f66e6172435d73938795c 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/03/28 09:39:55 by ljiriste          #+#    #+#             */
-/*   Updated: 2024/03/28 10:49:03 by ljiriste         ###   ########.fr       */
+/*   Updated: 2024/03/28 12:01:03 by ljiriste         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -22,13 +22,13 @@ int init(t_diner *diner, pthread_t **threads)
        const size_t    count = diner->setting.philo_count;
        size_t                  i;
 
-       diner->philos = malloc(sizeof(diner->philos) * count);
-       diner->forks = malloc(sizeof(diner->forks) * count);
-       *threads = malloc(sizeof(*threads) * count);
-       if (!diner->philos || !diner->forks || !*threads)
-               return (1);
        init_mutex(&diner->setting.terminal_lock, NULL);
        init_mutex(&diner->setting.end_lock, NULL);
+       diner->philos = malloc(sizeof(*diner->philos) * count);
+       diner->forks = malloc(sizeof(*diner->forks) * count);
+       *threads = malloc(sizeof(**threads) * count);
+       if (!diner->philos || !diner->forks || !*threads)
+               return (1);
        i = 0;
        while (i < count)
        {