Note : If you found this repo you should stop for a second and read this
Because reading a line from a
fdis way too tedious. 😪
- About 📌
- Implementation 📜
- Mandatory Requirements
- Bonus Requirements
- Structure 🚧
- Mandatory & Bonus w/ Arrays: Files and Functions 🗂
- Mandatory w/ Linked Lists: Files and Functions 🔗
- Usage 🏁
- Debugging 🪲
- Debugging
get_next_line
get_next_line is a 42 Common Core project that delves into how static variables and file descriptors work. It is a challenging introduction to memory allocation.
get_next_line must:
- Return the contents of a line per function call read from a given
file descriptor, until the end of the file is reached. - it must be implemented so that it handles any
BUFFER_SIZEdefined at compile time.
- Must use only one
staticvariable. - The function must be able to handle multiple file descriptors appropriately.
- Each function call should be able to read from different
fdwithout loosing track of partial lines read into itsstaticbuffer.
---
title get_next_line Mandatory & Bonus Structure
---
classDiagram
class gnl["get_next_line.c"]
class gnlu["get_next_line_utils.c"]
class gnlh["get_next_line.h"]
gnlh <--> gnlu
gnlh <--> gnl
gnl : char *get_next_line(int fd)
gnl : static char *ft_getline(int fd, char *vault)
gnl : static char *ft_gettillnl(char *vault)
gnl : static char *ft_getrest(char *vault)
gnlu : char * ft_strjoin_gnl(char *s1, char *s2)
gnlu : char * ft_strlen_gnl(char *str)
gnlu : char * ft_strchr_gnl(const char *s, int c)
---
title get_next_line Mandatory w/ Linked Lists Structure
---
classDiagram
class gnl["get_next_line.c"]
class gnlu["get_next_line_utils.c"]
class gnlh["get_next_line.h"]
gnlh <--> gnlu
gnlh <--> gnl
gnl : char *get_next_line(int fd)
gnl : void ft_getline(int fd, t_list **strs, int *c_read)
gnl : void ft_storestr(t_list **strs char *buffer, int c_read)
gnl : void ft_get_strs(t_list *strs, char *line)
gnl : void ft_clear_strs(t_list **strs)
gnlu : char * ft_strjoin_gnl(char *s1, char *s2)
gnlu : char * ft_strlen_gnl(char *str)
gnlu : char * ft_strchr_gnl(const char *s, int c)
- Clone repository & enter directory:
git clone https://github.com/PedroZappa/42_get_next_line.git zedro-gnl
cd zedro-gnl[!Note]
Two distinct implementation can be found in this repository: the one on
srcllimplementsLinked Listswithout the bonus; while the other onsrcbusesArraysand meets both mandatory and bonus requirements.
- To check all the available commands:
make help- Compile mandatory with
Arrays🗂:
make- Compile bonus with
Arrays🗂:
make bonus- Compile mandatory with
Linked Lists🔗:
make extrall[!Important]
The
Linked Listsimplementation does not fully pass norminette's tests. It was my first attempt at this project, I decided to keep it for future reference on handlingLinked Lists. It seems impossible to solve this challenge usingLinked Liststaking the appropriate precautions to avoid memory leaks while adhering toThe Norm.
Within each implementation folder there is a .gdbinit file defining commands to quickly display relevant debug info customized for each implementation. trace-commands and logging is turned on to save gdb's output into a gdb.txt file that we can track in real time with the command tail for a better debugging experience.
[!NOTE]
For user convenience the
makecommand will copy the.gdbinitfile related to the selected implementation into the root of the project.
- In your home directory, create a
.gdbinitfile with the following content:
set auto-load safe-path /This command is used to set the directories from which
gdbcan automatically load files. Whengdbstarts it reads commands from several initialization files,.gdbinitbeing one of them, it defines the commands to be automatically executed at the start of agdbsession.
[!Important] Do this step only if you trust all the contents of the system you're working on, otherwise change
/to the path of the folder you cloned this repo to.
- Compile your chosen
get_next_lineimplementation, for survey convenience the-gflag is called by default at compile time:
make
# or
make bonus
# or
make extrall- Open another terminal instance and fire up
valgrindandvgdbby runningvalgrindwith the flag--vgdb-error=0:
valgrind -q --vgdb-error=0 ./a.out
vgdbis a small tool that allowsgdbandvalgrindto work together.
- On the first terminal, run
gdbwith the--tuiflag to launchgdbwith a graphical user interface:
gdb --tui ./a.out <file>- On a third terminal instance run
tailwith the-fflag, which will keep track and print to the screengdb's output:
tail -f gdb.txt- To investigate the state of the memory at any point of execution of the program run the custom command
mchk:
(gdb) mchkThis command is an alias for
monitor leak_check full reachable any
[!Important]
If you use
tmuxgivemake gdbandmake vgdbrules a try 😉