resolu calloc pour taille zero
This commit is contained in:
@@ -6,7 +6,7 @@
|
||||
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/11/25 13:54:53 by hulamy #+# #+# */
|
||||
/* Updated: 2019/11/25 14:28:35 by hulamy ### ########.fr */
|
||||
/* Updated: 2019/11/28 16:00:32 by hulamy ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@@ -20,13 +20,44 @@
|
||||
** return a pointer to the allocated memory
|
||||
*/
|
||||
|
||||
/*
|
||||
** #include <libc.h>
|
||||
**
|
||||
** void ft_bzero(void *s, size_t n)
|
||||
** {
|
||||
** size_t i;
|
||||
** unsigned char *ptr;
|
||||
**
|
||||
** if (n)
|
||||
** {
|
||||
** ptr = (unsigned char *)s;
|
||||
** i = 0;
|
||||
** while (i < n)
|
||||
** ptr[i++] = '\0';
|
||||
** }
|
||||
** }
|
||||
**
|
||||
** void *ft_calloc(size_t count, size_t size);
|
||||
**
|
||||
** int main(void)
|
||||
** {
|
||||
** void *str;
|
||||
**
|
||||
** str = ft_calloc(0, 0);
|
||||
** if (str == ((void *)0))
|
||||
** printf("failed\n");
|
||||
** free(str);
|
||||
** return (0);
|
||||
** }
|
||||
*/
|
||||
|
||||
#include "libft.h"
|
||||
|
||||
void *ft_calloc(size_t count, size_t size)
|
||||
{
|
||||
void *tmp;
|
||||
|
||||
if (!count || !size || !(tmp = malloc(count * size)))
|
||||
if (!(tmp = malloc(count * size)))
|
||||
return (NULL);
|
||||
ft_bzero(tmp, count * size);
|
||||
return (tmp);
|
||||
|
||||
@@ -1,15 +1,3 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_calloc.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/11/25 13:54:53 by hulamy #+# #+# */
|
||||
/* Updated: 2019/11/25 14:28:35 by hulamy ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
/*
|
||||
** exemple allocation for 5 integers with malloc then calloc :
|
||||
** a = (int *)malloc(5 * sizeof(int)); //5*4bytes = 20 bytes
|
||||
@@ -26,7 +14,7 @@ void *ft_calloc(size_t count, size_t size)
|
||||
{
|
||||
void *tmp;
|
||||
|
||||
if (!count || !size || !(tmp = malloc(count * size)))
|
||||
if (!(tmp = malloc(count * size)))
|
||||
return (NULL);
|
||||
ft_bzero(tmp, count * size);
|
||||
return (tmp);
|
||||
|
||||
@@ -1,15 +1,3 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_strlcpy.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/11/25 13:57:19 by hulamy #+# #+# */
|
||||
/* Updated: 2019/11/25 15:53:19 by hulamy ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
/*
|
||||
** copy size - 1 length of src into dest,
|
||||
** terminate it with a '\0'
|
||||
|
||||
Reference in New Issue
Block a user