Skip to content

thcardos/Libft

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

45 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

This project has been created as part of the 42 curriculum by thcardos

Libft

Description

Libft is a library of functions in C that recreates the standard functions of the C library, as well as including additional functions useful for future 42 projects. This project is the first project in the 42 curriculum and its aim is to familiarise students with programming in C, memory management and basic data structures.

The main objective is to understand how standard C functions work internally and to develop a solid foundation of knowledge about string manipulation, memory, and linked lists.

Below is a detailed description of each function and its logic:

Part 1 - Libc Functions

These functions recreate standard C library functions with the same behavior.

Character Verification Functions

ft_isalpha

- Checks if a character is alphabetic (a-z, A-Z)

- Returns 1 if the character is a letter, 0 otherwise

- Logic: Verifies if c is between 'a'-'z' or 'A'-'Z'

ft_isdigit

- Checks if a character is a numeric digit (0-9)

- Returns 1 if the character is a digit, 0 otherwise

- Logic: Verifies if c is between '0'-'9'

ft_isalnum

- Checks if a character is alphanumeric

- Returns 1 if the character is a letter or digit, 0 otherwise

- Logic: Combines ft_isalpha and ft_isdigit

ft_isascii

- Checks if a character is in the ASCII table (0-127)

- Returns 1 if the character is ASCII, 0 otherwise

- Logic: Verifies if c is between 0 and 127

ft_isprint

- Checks if a character is printable (32-126)

- Returns 1 if the character is printable, 0 otherwise

- Logic: Verifies if c is between 32 and 126 (space to tilde)

ft_toupper

- Converts a lowercase letter to uppercase

- Returns the uppercase equivalent if c is lowercase, otherwise returns c

- Logic: If c is between 'a'-'z', subtracts 32 to convert to uppercase

ft_tolower

- Converts an uppercase letter to lowercase

- Returns the lowercase equivalent if c is uppercase, otherwise returns c

- Logic: If c is between 'A'-'Z', adds 32 to convert to lowercase

String Functions

ft_strlen

- Calculates the length of a string

- Returns the number of characters before the null terminator

- Logic: Iterates through the string until '\0' is found, counting characters

ft_strchr

- Locates the first occurrence of a character in a string

- Returns a pointer to the first occurrence of c in s, or NULL if not found

- Logic: Iterates through the string comparing each character with c

ft_strrchr

- Locates the last occurrence of a character in a string

- Returns a pointer to the last occurrence of c in s, or NULL if not found

- Logic: Iterates through the entire string and saves the position of the last match

ft_strncmp

- Compares two strings up to n characters

- Returns 0 if equal, negative if s1 < s2, positive if s1 > s2

- Logic: Compares characters one by one until finding a difference, reaching n, or finding '\0'

ft_strlcpy

- Copies a string to a destination buffer with size limit

- Returns the total length of the string it tried to create

- Logic: Copies up to size - 1 characters and always null-terminates the destination

ft_strlcat

- Concatenates strings with size limit

- Returns the total length of the string it tried to create

- Logic: Appends src to the end of dst, ensuring null-termination and size limit

ft_strnstr

- Locates a substring in a string (limited to len bytes)

- Returns a pointer to the beginning of the located substring, or NULL if not found

- Logic: Searches for the first occurrence of little in big within len bytes

ft_strdup

- Duplicates a string in dynamically allocated memory

- Returns a pointer to the new string, or NULL if allocation fails

- Logic: Allocates memory with malloc, copies the string, and returns the pointer

Memory Functions

ft_memset

- Fills a block of memory with a specific byte

- Returns a pointer to the memory area s

- Logic: Sets the first n bytes of s to the value c

ft_bzero

- Sets a block of memory to zero

- No return value (void)

- Logic: Calls ft_memset with value 0

ft_memcpy

- Copies n bytes from src to dest (no overlap handling)

- Returns a pointer to dest

- Logic: Copies n bytes from src to dest byte by byte

ft_memmove

- Copies n bytes from src to dest (handles overlapping)

- Returns a pointer to dest

- Logic: Checks if regions overlap and copies forward or backward accordingly to avoid data corruption

ft_memchr

- Locates a byte in a block of memory

- Returns a pointer to the matching byte, or NULL if not found

- Logic: Searches for the first occurrence of c in the first n bytes of s

ft_memcmp

- Compares two blocks of memory

- Returns 0 if equal, negative if s1 < s2, positive if s1 > s2

- Logic: Compares the first n bytes of s1 and s2 byte by byte

Conversion Functions

ft_atoi

- Converts a string to an integer

- Returns the converted integer value

- Logic: Skips whitespace, handles sign (+/-), converts digits to integer until a non-digit is found

ft_calloc

- Allocates memory and initializes it to zero

- Returns a pointer to the allocated memory, or NULL if allocation fails

- Logic: Allocates nmemb * size bytes with malloc and initializes to zero with ft_bzero

Part 2 - Additional Functions

These functions provide additional string manipulation capabilities not in the standard library.

ft_substr

- Extracts a substring from a string

- Returns a new string (substring), or NULL if allocation fails

- Logic: Allocates memory for len characters, copies characters starting from index start

ft_strjoin

- Concatenates two strings into a new string

- Returns a new string containing s1 followed by s2, or NULL if allocation fails

- Logic: Allocates memory for both strings, copies s1, then appends s2

ft_strtrim

- Removes characters from the beginning and end of a string

- Returns a trimmed copy of the string, or NULL if allocation fails

- Logic: Finds the start and end positions by skipping characters in set, then extracts the substring

ft_split

- Splits a string into an array of strings using a delimiter

- Returns an array of strings, or NULL if allocation fails

- Logic: Counts words separated by c, allocates array, extracts each word into a new string

ft_itoa

- Converts an integer to a string

- Returns a string representing the integer, or NULL if allocation fails

- Logic: Calculates number of digits, handles negative numbers, converts each digit to character

ft_strmapi

- Applies a function to each character of a string

- Returns a new string with the function applied, or NULL if allocation fails

- Logic: Creates a new string, applies function f to each character with its index

ft_striteri

- Applies a function to each character of a string (modifies in place)

- No return value (void)

- Logic: Iterates through the string applying function f to each character with its index

ft_putchar_fd

- Writes a character to a file descriptor

- No return value (void)

- Logic: Uses write system call to output character c to file descriptor fd

ft_putstr_fd

- Writes a string to a file descriptor

- No return value (void)

- Logic: Uses write to output the entire string s to file descriptor fd

ft_putendl_fd

- Writes a string followed by a newline to a file descriptor

- No return value (void)

- Logic: Calls ft_putstr_fd followed by write to output a newline character

ft_putnbr_fd

- Writes an integer to a file descriptor

- No return value (void)

- Logic: Handles negative numbers and edge cases, converts digits to characters recursively and writes them

Part 3 - Linked Lists

These functions provide a complete set of operations for managing singly linked lists.

Structure:

typedef struct s_list
{
    void            *content;  // Pointer to the data
    struct s_list   *next;     // Pointer to the next node
}   t_list;

ft_lstnew

- Creates a new list node

- Returns a pointer to the new node, or NULL if allocation fails

- Logic: Allocates memory for a new node, initializes content with the parameter, sets next to NULL

ft_lstadd_front

- Adds a node at the beginning of the list

- No return value (void)

- Logic: Sets new->next to the current first node, updates the list head to point to new

ft_lstadd_back

- Adds a node at the end of the list

- No return value (void)

- Logic: If list is empty, sets head to new; otherwise traverses to the last node and sets its next to new

ft_lstsize

- Counts the number of nodes in a list

- Returns the number of nodes

- Logic: Traverses the list from head to tail, counting each node

ft_lstlast

- Returns the last node of the list - Returns a pointer to the last node, or NULL if list is empty - Logic: Traverses the list until finding a node whose next is NULL

ft_lstdelone

- Deletes a single node

- No return value (void)

- Logic: Calls the del function on the node's content, then frees the node itself

ft_lstclear

- Deletes all nodes in the list

- No return value (void)

- Logic: Iterates through the list, deleting each node with ft_lstdelone and sets the head to NULL

ft_lstiter

- Applies a function to each node's content

- No return value (void)

- Logic: Traverses the list and applies function f to each node's content

ft_lstmap

- Creates a new list by applying a function to each node

- Returns a pointer to the new list, or NULL if allocation fails

- Logic: Iterates through the original list, applies f to each content, creates new nodes, and builds a new list. If any allocation fails, clears the new list using del

Instructions

Compilation with the library

cc -Wall -Wextra -Werror your_program.c libft.a -o program

Available commands

Comand Description
make Compiles the library
make clean Deletes all the object files (.o)
make fclean Deletes the object files and the library
make re Compiles everything again

Testing the functions:

Here you have the Main File I have created to test my functions.

Memory verification with Valgrind:

# Compile your program with the library
gcc -g -Wall -Wextra -Werror your_program.c -L. -lft -o program

# Run with Valgrind
valgrind --leak-check=full ./program

# Show all possible Leaks
valgrind --leak-check=full --show-leak-kinds=all ./test_libft

# Display detailed information about the errors
valgrind --leak-check=full --track-origins=yes ./test_libft

# Save the report to a file
valgrind --leak-check=full --log-file=valgrind_report.txt ./test_libft

🟩Without errors🟩

HEAP SUMMARY:
    in use at exit: 0 bytes in 0 blocks
  total heap usage: X allocs, X frees, Y bytes allocated

All heap blocks were freed -- no leaks are possible

🟥With errors🟥

LEAK SUMMARY:
   definitely lost: 24 bytes in 1 blocks

This indicates that there is memory that was allocated with malloc but never freed with free.

Using GDB (GNU Debugger)

GDB is an interactive debugger that allows you to inspect your program while it runs.

Basic usage:

# Compile with debugging symbols
gcc -g -Wall -Wextra -Werror main.c -L. -lft -o test

# Start GDB
gdb ./test

GDB commands

Comand Description
run Execute the program
break main Set a breakpoint at main function
break your_program Set a breakpoint at your_program
next Execute next line (step over)
step Execute next line (step into)
print variable Print variable value
continue Continue execution
quit Exit GDB

Using AddressSanitizer

AddressSanitizer detects memory errors at runtime.

gcc -g -fsanitize=address -Wall -Wextra -Werror main.c -L. -lft -o test
./test

This will automatically detect:

  • Buffer overflows

  • Use after free

  • Memory leaks

  • Double free

Resoures

References used:

Use of AI

During the development of this project artificial intelligence has been used as a learning support tool, not as a shortcut. Similar to consulting a book or asking a classmate, AI has been used to:

- Understand concepts that were not clear in the documentation.

- Obtain guidance on what to study.

- Improve the presentation of documentation.

However, the entire process of reasoning, implementation, and debugging has been manual, ensuring the development of essential skills for the rest of the 42 curriculum and for taking exams without external dependencies.

Educational Use:

- README Aesthetics: Visual design, badges, markdown formatting, and documentation structure.

- Resource Guidance: Advice on which resources, documentation, and tutorials to look for to learn specific concepts.

- Definitions for notes: Creation of clear and concise definitions of programming concepts for my personal notes.

- Debugging tips: Suggestions on debugging tools and methodologies (Valgrind, GDB, etc.)

Not used for:

- Code generation: All function code has been written manually without AI assistance.

- Exercise solving: Algorithms and logic have been developed independently.

- Direct copying of solutions: AI-generated answers have not been used to implement functions.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors