eg0rmaffin

malloc segfault

Jul 11th, 2019
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.56 KB | None | 0 0
  1. #include <stdlib.h>
  2.  
  3. int     ft_strlen(char *str)
  4. {
  5.     int l;
  6.  
  7.     l = 0;
  8.     while (*str++ != '\0')
  9.         l++;
  10.     return (l);
  11. }
  12.  
  13. char    *ft_strdup(char *src)
  14. {
  15.     int     run;
  16.     char    *str;
  17.  
  18.     run = ft_strlen(src);
  19.     if (run == 0)
  20.         return (0);
  21.     str = (char*)malloc(sizeof(char) * (run + 1));
  22.     run = 0;
  23.     while (src[run] != '\0')
  24.     {
  25.         str[run] = src[run];
  26.         run++;
  27.     }
  28.     str[run] = '\0';
  29.     return (str);
  30. }
  31.  
  32.  
  33. #include <stdio.h>
  34.  
  35. char    *ft_strdup(char *src);
  36.  
  37. int main()
  38.     {
  39.               char *f = "";
  40.                printf("%s\n", f);
  41.                 printf("%s\n", ft_strdup(f));
  42.             }
Advertisement
Add Comment
Please, Sign In to add comment