Libft
42 Abu Dhabi

Libft

Custom C Library Implementation

1 month
Individual Project
CMakefileMemory ManagementGCC

A comprehensive recreation of standard C library functions from scratch to gain deeper understanding of their underlying implementation, serving as a foundation for C programming projects.

Key Features

String Functions

Complete implementation of string manipulation functions including strlen, strcpy, strncmp, strjoin, and advanced string operations for robust text processing.

Memory Functions

Memory allocation and manipulation functions like memset, memcpy, memcmp, calloc with optimized performance and comprehensive safety checks.

Linked Lists

Comprehensive linked list implementation with functions for creation, manipulation, mapping, filtering, and advanced data structure operations.

Character Functions

Character classification and conversion functions including isalpha, isdigit, toupper, tolower for robust character handling and validation.

Conversion Functions

Number conversion utilities like atoi, itoa for seamless data type transformations and numeric string processing capabilities.

Error Handling

Robust error handling and memory safety measures to prevent buffer overflows, memory leaks, and undefined behavior in C programs.

Development Journey

Part 1

Libc Functions Recreation

Implemented standard C library functions with identical behavior, focusing on string manipulation, memory operations, and character classification functions.

Part 2

Additional Utility Functions

Created additional utility functions not in standard library, including string splitting, joining, transformation, and enhanced memory allocation functions.

Bonus

Linked List Data Structure

Implemented a complete linked list data structure with comprehensive manipulation functions for advanced data handling and algorithmic operations.

Challenges & Solutions

Memory Management Precision

Problem:

Managing memory allocation and deallocation without standard library functions while preventing memory leaks and ensuring optimal performance.

Solution:

Implemented meticulous memory tracking with custom allocation functions, comprehensive cleanup procedures, and error handling for all allocation failures.

Function Behavior Accuracy

Problem:

Recreating exact behavior of standard library functions including edge cases, error conditions, and performance characteristics.

Solution:

Conducted extensive testing with various inputs, edge cases, and comparison with original functions to ensure identical behavior and reliability.

Performance Optimization

Problem:

Achieving optimal performance while maintaining code readability and ensuring compatibility across different systems and compilers.

Solution:

Applied algorithmic optimizations, efficient memory usage patterns, and compiler-friendly code structures for maximum performance.

ft_split.c
c
char	**ft_split(char const *s, char c)
{
	char	**result;
	size_t	word_count;
	size_t	i;

	if (!s)
		return (NULL);
	word_count = count_words(s, c);
	result = malloc(sizeof(char *) * (word_count + 1));
	if (!result)
		return (NULL);
	i = 0;
	while (i < word_count)
	{
		result[i] = get_next_word(&s, c);
		if (!result[i])
		{
			free_array(result, i);
			return (NULL);
		}
		i++;
	}
	result[i] = NULL;
	return (result);
}

static size_t	count_words(char const *s, char c)
{
	size_t	count;
	size_t	in_word;

	count = 0;
	in_word = 0;
	while (*s)
	{
		if (*s != c && !in_word)
		{
			in_word = 1;
			count++;
		}
		else if (*s == c)
			in_word = 0;
		s++;
	}
	return (count);
}