This activity has been created as part of the 42 curriculum by akamamji.
Libft is the first project in the 42 Common Core curriculum. The goal is to build a custom C library (libft.a) that implements standard C library functions (libc) along with other useful utility functions.
This project is essential for understanding:
- Low-level memory management (
malloc,free). - Pointer arithmetic and manipulation.
- How standard string and memory functions behave under the hood.
- Data structures, specifically Linked Lists (Bonus part).
Once completed, this library will be reused in future school projects.
The project includes a Makefile that compiles the source files and generates the static library. Run the following commands in the terminal root of the repository:
-
Compile the library (Mandatory):
make
This generates
libft.a. -
Compile with Bonus functions (Linked Lists):
make bonus
This adds the list manipulation functions to the library.
-
Clean object files:
make clean
-
Full clean (objects + library):
make fclean
-
Recompile:
make re
To use Libft in your C projects, you need to include the header file and link the static library during compilation.
Example:
- Include the header in your C file:
#include "libft.h"
- Compile your file linking with
libft.a:cc main.c -L. -lft -o my_program
- Man Pages: The primary reference for understanding the behavior of libc functions (e.g.,
man 3 memset,man 3 strlcat). - CPP Reference / GeeksforGeeks: Used for clarifying edge cases and return values for functions like
memmoveandstrnstr. - GNU Make Manual: Referenced for creating the Makefile and understanding rules like
.PHONY.
As part of the learning process, AI tools (Gemini) were used to assist with the following specific tasks:
- Debugging Memory Errors (Valgrind):
- AI was used to interpret complex Valgrind errors, specifically "Invalid write of size 8" in
ft_split. It helped identify an operator precedence issue in themallocallocation size logic.
- AI was used to interpret complex Valgrind errors, specifically "Invalid write of size 8" in
- Logic Verification & Edge Cases:
- Verified the logic for handling
INT_MINinft_itoato prevent overflow. - Clarified the logic for memory overlap handling in
ft_memmove(copying backwards vs forwards).
- Verified the logic for handling
- Concept Explanation:
- Used to understand the difference between
unsigned charcasting in memory functions and standardcharpointers.
- Used to understand the difference between
Note: All code implementation and final logic decisions were made by the student. AI was used strictly as a debugging assistant and for conceptual clarification.
The library includes the following categories of functions:
- Libc Functions:
memset,bzero,memcpy,memmove,memchr,memcmp,strlen,strlcpy,strlcat,strchr,strrchr,strnstr,strncmp,atoi,isalpha,isdigit,isalnum,isascii,isprint,toupper,tolower,calloc,strdup. - Additional Functions:
ft_substr,ft_strjoin,ft_strtrim,ft_split,ft_itoa,ft_strmapi,ft_striteri,ft_putchar_fd,ft_putstr_fd,ft_putendl_fd,ft_putnbr_fd. - Bonus (Linked Lists):
ft_lstnew,ft_lstadd_front,ft_lstsize,ft_lstlast,ft_lstadd_back,ft_lstdelone,ft_lstclear,ft_lstiter,ft_lstmap.