🧠
Hi, Daehuyn Lee
  • Fork-my-brain
  • Network
    • 7. "데이터가 전달되는 원리" OSI 7계층 모델과 TCP:IP 모델
    • [Netwhat] 연습문제 정리
    • 11. IP 라우팅(routing) 동작 과정
    • 3. IP address 란?
    • 2. 컴퓨터 구조를 통해 이해하는 파일(File)과 소켓(Socket)
    • 10 "더 편리한 인터넷을 위해" DHCP && DNS 프로토콜
    • 9. 데이터? 세그먼트? 패킷? 헷갈릴 땐 PDU를 알아보자
    • 8. TCP 와 UDP 차이를 자세히 알아보자
    • 5. 서브넷팅(subnetting)으로 네크워크를 효율적으로 관리하자
    • 4. 넷마스크(Netmask)와 서브넷마스크(Subnetmask)
    • 1. 비유로 이해하는 컴퓨터 구조
    • 6. 공인(Public) && 사설(Private) IP의 차이점
  • Django
    • [Django 1] 가상환경에 Django 설치하기
    • [Django 3] Hello World 웹사이트 만들기
    • [Django 9] static 으로 css 로드하기
    • [Django 10] 한 템플릿에서 복수의 css 파일 적용하기
    • [Django 11] URL app별로 관리하기
    • [Django 8] 템플릿 상속
    • [Django 4] MTV 패턴
    • [Django 6] 블로그 model 만들기
    • [Django 2] Django는 어떻게 작동할까
    • [Django 7] '새 글 작성' 기능 만들기
    • [Django 5] 템플릿 언어
  • Projects
    • 예발자닷컴
      • 4. 프론트엔드의 역할은 어디까지 - 더미데이터 만들기
      • 7. [React 리팩토링] CSS Inline Styling에 Props 사용하기
      • 6. [React 리팩토링] JSX에서 조건문 사용해 렌더링하기
      • 3. 예발자닷컴 프론트서버 업데이트 하기
      • 8. [React 리팩토링] 예발자 프로젝트에 Redux 적용하기
      • 5. [React 리팩토링] JSX로 HTML 렌더링하기
      • 1. 👨‍👨‍👦‍👦 Github로 협업 프로젝트 관리하기
      • 2. React Component를 활용한 웹페이지 디자인 연습
  • Git
    • [Git] Interactive Rebase 실습
    • 오픈소스 개발 참여에 필요한 Git 명령어 정리
    • 개발자가 오픈소스를 읽는 방법
    • 오픈소스 프로젝트 시작하기
    • SSH agent ; Passphrase 입력 없이 Push하기
    • SSH로 원격저장소 접속하기
    • [Github] 개인 저장소를 팀 저장소로 변경하기
    • GitHub Dependabot
    • Git add, commit, push 취소하기
    • 깃헙 잔디 관리 팁
    • 원격저장소 여러개 연결하기
    • Typora(마크다운 에디터) 사용법
  • C
    • C Piscine
      • 메모리 구조를 알아보자
      • Makefile 만들기
      • GCC로 정적 라이브러리 파일 만들기
      • 외부 라이브러리 GCC로 컴파일 하기
      • 정적(Static) 변수
      • 저수준 파일 입출력
      • Makefile 자주 사용하는 문법 정리
      • segmentation fault 해결하기
      • C의 구조체 개념
      • 연결 리스트(linked list)에서 이중 포인터 사용하기
      • 로컬에 Norminette 설치하기
    • GetNextLine
      • [GetNextLine] 과제소개-Reading a line on a fd is way too tedious
      • [GetNextLine] 삽질의 기록
      • [GetNextLine] 리팩토링-프로그램의 목적을 고려한 코드
    • ft_printf
      • 1. 과제소개
      • 2. 가변인자 (Variadic Arguments)
      • 3. 형식태그와 서식지정자 printf 함수의 옵션 알아보기
    • Libft
      • [Libft] Bonus
      • [Libft] Test Program
      • [Libft] 나만의 C 라이브러리 만들기
      • [Libft] Part 2
      • [Libft] Part 1
  • UNIX shell
    • [minishell] 4. 종료상태와 에러메세지 처리
    • [minishell] 1. 과제소개 및 선행지식
    • [minishell] 2. 프로그램 구조 및 개발 기록들
    • [minishell] 5. 파이프(Pipe) 처리
    • [minishell] 3. 시그널(Signal) 처리하기
    • [minishell] 6. 리다이렉션(Redirection) 처리
  • Web
    • Next.js
      • [Next.js] CSS모듈과 복수의 class 사용하기
    • Node.js
      • [Node.js] 웹페이지에 파일 띄우기
      • [Node.js] URL에서 쿼리스트링 추출하기
      • [Node.js] '새 글 작성' 페이지 만들기
    • React
      • [React] 2. 컴포넌트(Component) 생성 및 파일별로 분리하기
      • [React] 1. 파일 구조 이해하기
      • [React] 4. 컴포넌트의 State 란
      • [React] 3. 컴포넌트의 Props 란
    • Javascript
      • Click, Enter 두 개의 이벤트 동시에 등록하기
      • Click eventListener 등록하기
      • JavaScript & C 문법 비교
      • JavaScript 객체 지향의 특징
    • CSS
      • [CSS] box-model, display, position
  • Docker
    • ft_server
      • 2. 도커 설치부터 워드프레스 구축까지
      • 1. 선행지식-Docker? Debian Buster? Nginx? ...
      • 3. Dockerfile 만들기
  • Kubernetes
    • 🌌[쿠버네티스 아키텍처] 3. API 호출
    • 🌌[쿠버네티스 아키텍처] 1. 구성 및 설계
    • 🌌[쿠버네티스 아키텍처] 2. 오브젝트 (Objects)
  • Operating System
    • Philosophers
      • [Philosophers] 예시예제로 보는 뮤텍스와 세마포어의 차이
      • [Philosophers] 식사하는 철학자 문제 소개
  • CPP
    • [CPP-08] STL containers, iterators, algorithms
    • [CPP-06] CPP 형변환 연산자
    • [CPP-04 ex02] 인터페이스(Interface) 클래스
    • [CPP-04 ex00] 다형성(Polymorphism) 및 가상함수
    • [CPP-02] Canonical 클래스 복사 생성자와 대입 연산자 오버로딩
    • [CPP-07] Templates
    • [CPP-01] this 포인터와 문자열 스트림(stringstream)
    • [CPP-01] 클래스의 정적할당과 동적할당 new, delete
    • [CPP-01] 파일 입출력 및 문자열 치환하기
    • [CPP-01] 참조자(reference)와 포인터는 다르다
    • [CPP-02] 정수부동소수값 - 고정소수값 변환
    • [CPP-04 ex01] 추상 클래스의 필요성 순수 가상함수
    • [CPP-00] Megaphone! CPP 표준입출력
    • [CPP-03] (ClapTrap이 뭐지) 다중 상속과 가상 상속
    • [CPP-05] 예외 처리 (exception handling)
    • [CPP-00] 객체지향의 관점으로 클래스 이해하기
    • [CPP-01] 랜덤값 얻기
  • IBM Cloud
    • [IBM Cloud] 1. 클라우드 컴퓨팅 개요
    • [IBM Cloud] 5. 클라우드 컴퓨팅의 구성 요소
    • [IBM Cloud] 3. 클라우드 서비스 모델 및 배포 모델
    • [IBM Cloud] 2. 클라우드를 활용하는 새 기술들
    • [IBM Cloud] 4. 떠오르는 클라우드 트렌드
    • [IBM Cloud] 6. 클라우드 스토리지 유형 및 CDN
  • Assembly
    • [libasm] 어셈블리 프로그램 구조와 x64 레지스터 이해하기
    • [libasm] strlen 함수를 어셈블리어로 짠다면
    • [libasm] 어셈블리 명령어(opcode) 정리
Powered by GitBook
On this page
  • 1. ft_lstnew
  • Prototype
  • Parameters
  • 1. The content to create the new element with.
  • Return value
  • External functs.
  • Description
  • 2. ft_lstadd_front
  • Prototype
  • Parameters
  • 1. The address of a pointer to the first link of a list.
  • 2. The address of a pointer to the element to be added to the list.
  • External functs.
  • Description
  • 3. ft_lstsize
  • Prototype
  • Parameters
  • 1. The beginning of the list.
  • Return value
  • Description
  • 4. ft_lstlast
  • Prototype
  • Parameters
  • 1. The beginning of the list.
  • Return value
  • Description
  • 5. ft_lstadd_back
  • Prototype
  • Parameters
  • 1. The address of a pointer to the first link of a list.
  • 2. The address of a pointer to the element to be added to the list.
  • Description
  • 6. ft_lstdelone
  • Prototype
  • Parameters
  • 1. The element to free.
  • 2. The address of the function used to delete the content.
  • External functs.
  • Description
  • 7. ft_lstclear
  • Prototype
  • Parameters
  • 1. The adress of a pointer to an element.
  • 2. The adress of the function used to delete the content of the element.
  • External functs.
  • Description
  • 8. ft_lstiter
  • Prototype
  • Parameters
  • 1. The adress of a pointer to an element.
  • 2. The adress of the function used to iterate on the list.
  • Description
  • 9. ft_lstmap
  • Prototype
  • Parameters
  • 1. The adress of a pointer to an element.
  • 2. The adress of the function used to iterate on the list.
  • 3. The adress of the function used to delete the content of an element if needed.
  • Return value
  • External functs.
  • Description

Was this helpful?

  1. C
  2. Libft

[Libft] Bonus

libft 프로젝트의 bonus part 함수들을 구현하면서 메모했던 내용들을 정리해두었다. 이 라이브러리의 함수들은 꾸준히 업데이트 되고 있기 때문에 가장 최신의 코드는 여기 깃헙저장소를 참고...

PreviousLibftNext[Libft] Test Program

Last updated 3 years ago

Was this helpful?

libft 프로젝트의 bonus part 함수들을 구현하면서 메모했던 내용들을 정리해두었다. 이 라이브러리의 함수들은 꾸준히 업데이트 되고 있기 때문에 가장 최신의 코드는 를 참고...

1. ft_lstnew

Prototype

t_list *ft_lstnew(void *content);

Parameters

1. The content to create the new element with.

Return value

The new element.

External functs.

malloc

Description

Allocates (with malloc(3)) and returns a new element. The variable ’content’ is initialized with the value of the parameter ’content’. The variable ’next’ is initialized to NULL.

    #include "libft.h"

    t_list      *ft_lstnew(void *content)
    {
        t_list  *new; 

        new = (t_list *)malloc(sizeof(t_list));
        if (new == NULL)
            return (NULL);
        new->content = content;
        new->next = NULL;
        return (new);
    }

새로운 노드 생성하는 함수

  • new 리스트의 next 변수(다음 노드의 포인터)는 NULL로 초기화.

  • 생성하는 함수일 뿐, 추가하거나 삽입하는 함수는 아님 !! 그래서 아래와 같은 의문이 들었지만 쓸데없는 걱정이었다.

    • 원래 NULL을 가리키던 마지막 요소는 어떻게 되는 걸까?

    • 두 개의 리스트가 NULL을 가리키는 것 아닌가?

  • 노드의 생성, 추가, 삽입은 개념이 다 다르다.

    • 추가는 새로 만든 노드를 연결리스트의 제일 마지막에 붙이는 작업

    • 삽입은 노드와 노드 사이에 새로운 노드를 삽입하는 작업

  • malloc으로 메모리를 할당해주는 이유

    • malloc없이 새로운 노드를 만들게 되면, ft_newlst 함수가 종료되면서 반환되는 new 노드는 stack에서 사라지게 된다.

    • malloc을 하면 힙영역에 할당되고, 그래야 함수가 종료돼도 메모리 상에 남아 t_list *new 가 할당 받은 메모리를 가리킬 수 있게 된다.

2. ft_lstadd_front

Prototype

void ft_lstadd_front(t_list **lst, t_list *new);

Parameters

1. The address of a pointer to the first link of a list.

2. The address of a pointer to the element to be added to the list.

External functs.

None

Description

Adds the element ’new’ at the beginning of the list.

    #include "libft.h"

    void    ft_lstadd_front(t_list **lst, t_list *new)
    {
        if (lst == NULL || new == NULL)
            return ;
        new->next = *lst;
        *lst = new;
    }

리스트 제일 앞에 새 리스트 추가하는 함수

  1. 새 노드의 다음 주소를 head로 설정

  2. head를 새 노드로 설정

  3. 연결 리스트 구현시 이중 포인터를 사용하는 이유

    • 단일 연결리스트에서 삽입과 삭제를 통해 head 포인터의 값을 변화시킬 수 있다.

    • 이때, 호출 함수의 포인터변수가 참조하는 객체를 피호출 함수에서 바꾸고자 할 경우 이중 포인터를 사용하면 된다.

  4. t_list **lst 는 t_list 포인터(lst)의 주소를 가리키는 포인터다.

    • t_list **lst 변수가 담고있는 값은 t_list *의 주소

    • t_list ** lst가 담고 있는 t_list * 의 주소는 어떤 리스트의 첫번째 주소

    • 즉, *lst는 head의 주소

  5. 의문점

    원래 head는 데이터가 없으니, head노드가 중간 노드가 됐을때 데이터도 따로 넣어줘야할 것 같다. 그래서 이 함수는 잘 안 쓰지 않을까? 제일 앞에다 새 노드를 추가하는 일보다는 제일 뒤에 추가하는 경우가 더 많은 것 같다.

3. ft_lstsize

Prototype

int ft_lstsize(t_list *lst);

Parameters

1. The beginning of the list.

Return value

Length of the list.

Description

Counts the number of elements in a list.

    #include "libft.h"

    int     ft_lstsize(t_list *lst)
    {
        int size;

        size = 0;
        while (lst != NULL)
        {
            lst = lst->next;
            size++;
        }
        return (size);
    }

- (lst++) == (lst = lst->next)

4. ft_lstlast

Prototype

t_list *ft_lstlast(t_list *lst);

Parameters

1. The beginning of the list.

Return value

Last element of the list.

Description

Returns the last element of the list.

    #include "libft.h"

    t_list      *ft_lstlast(t_list *lst)
    {
            if (lst == NULL)
                    return (NULL);
        while (lst->next != NULL)
                    lst = lst->next;
        return (lst);
    }

5. ft_lstadd_back

Prototype

void ft_lstadd_back(t_list **lst, t_list *new);

Parameters

1. The address of a pointer to the first link of a list.

2. The address of a pointer to the element to be added to the list.

Description

Adds the element ’new’ at the end of the list.

    #include "libft.h"

    void    ft_lstadd_back(t_list **lst, t_list *new)
    {
        t_list *last;

        if (lst == NULL || new_node == NULL)
            return ;
        if (*lst == NULL)
        {
            *lst = new_node;
            return ;
        }
        last = ft_lstlast(*lst);
        new->next = last->next;
        last->next = new;
    }

리스트의 마지막에 새 노드를 추가하는 함수

  1. new의 다음 주소는 원래 last의 다음 주소( = NULL) 로 설정

  2. last의 다음 주소가 new를 가리키도록 변경

  3. ft_lstlast();` 함수의 인자로 lst를 보내야하는지 *lst를 보내야 하는지 헷갈렸다.

    • lstlast 함수는 파라미터로 t_list * 즉, 첫 번째 요소의 주소를 받는다.

    • 이렇게 생각하면 조금 쉬워진다.

      ft_lstadd_back 함수에서 우리가 받은 인자의 자료형은 t_list *이고, (t_list ) = t_list 이기 때문에 lst 를 인자로 보내면 된다.

  4. *lst == NULL 과 lst == NULL 의 차이

    • *lst는 lst의 첫번째 주소, 즉 헤드의 주소를 의미한다. 헤드가 비었다는 건,

      *lst는 빈 리스트 라는 뜻!

    • lst == NULL은 리스트 자체가 존재하지 않는다 뜻!

6. ft_lstdelone

Prototype

void ft_lstdelone(t_list *lst, void (*del)(void *));

Parameters

1. The element to free.

2. The address of the function used to delete the content.

External functs.

free

Description

Takes as a parameter an element and frees the memory of the element’s content using the function ’del’ given as a parameter and free the element. The memory of ’next’ must not be freed.

    #include "libft.h"

    void    ft_lstdelone(t_list *lst, void (*del)(void *))
    {
        if (lst == NULL)
            return ;
        del(lst->content);
        free(lst);
    }
  1. del 함수로 content 변수 free

  2. lst 요소 free

  3. 의문점1 : The memory of ’next’ must not be freed ?

    • next 변수가 가리키는 다음 노드는 지우지 말라는 뜻.

    • 연결 해주는 작업은 이 함수가 끝난 뒤 따로 처리해줘야 할듯.

  4. 의문점2 : 그냥 free(lst) 만 하면 안되는 이유는?

    • content 변수는 주소만 담고 있어서 그 주소가 가리키는 값까지 삭제를 해줘야 하는데, free(lst)를 한 후에는 lst->content 로 접근이 불가능하다. 그래서 content를 먼저 free해주고 그 다음에 lst를 free 해줘야 한다.

    • 순서 중요!

7. ft_lstclear

Prototype

void ft_lstclear(t_list **lst, void (*del)(void *));

Parameters

1. The adress of a pointer to an element.

2. The adress of the function used to delete the content of the element.

External functs.

free

Description

Deletes and frees the given element and every successor of that element, using the function ’del’ and free(3). Finally, the pointer to the list must be set to NULL.

    #include "libft.h"

    void        ft_lstclear(t_list **lst, void (*del)(void *))
    {
        t_list  *curr;
        t_list  *next;

        curr = *lst;
        while (curr)
        {
            next = curr->next;
            ft_lstdelone(curr, del);
            curr = next;
        }
        *lst = NULL;
    }

연결 리스트 전체 노드 데이터를 삭제하는 함수

  1. curr 을 삭제하기 전에 next에 curr의 다음 노드 입력

  2. curr 에 할당된 메모리 해제.

  3. curr을 next 초기화.

  4. lst는 빈 리스트 -> *lst = NULL; (안하면 쓰레기값)

  5. next 말고 prev 변수를 만든 뒤에 prev를 삭제하는 방식도 가능할 것 같다.

    1. prev에 curr 기억

    2. curr = curr->next : curr은 다음 노드로 이동

    3. prev 메모리 해제

8. ft_lstiter

Prototype

void ft_lstiter(t_list *lst, void (* f)(void *));

Parameters

1. The adress of a pointer to an element.

2. The adress of the function used to iterate on the list.

Description

Iterates the list ’lst’ and applies the function ’f’ to the content of each element.

    #include "libft.h"

    void    ft_lstiter(t_list *lst, void (*f)(void *))
    {
        if (lst == NULL || f == NULL)
            return ;
        while (lst)
        {
            f(lst->content);
            lst = lst->next;
        }
    }

리스트를 반복하면서 특정 함수 적용시키는 함수.

9. ft_lstmap

Prototype

t_list *ft_lstmap(t_list *lst, void *(*f)(void * ), void (*del)(void *));

Parameters

1. The adress of a pointer to an element.

2. The adress of the function used to iterate on the list.

3. The adress of the function used to delete the content of an element if needed.

Return value

The new list. NULL if the allocation fails.

External functs.

malloc, free

Description

Iterates the list ’lst’ and applies the function ’f’ to the content of each element. Creates a new list resulting of the successive applications of the function ’f’. The ’del’ function is used to delete the content of an element if needed.

    #include "libft.h"

    t_list      *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *))
    {
        t_list  *new_head;
        t_list  *new_next;
        t_list  *curr;

        if (lst == NULL || f == NULL || del == NULL)
            return (NULL);
        if ((new_head = ft_lstnew(f(lst->content))) == NULL)
            return (NULL);
        curr = new_head;
        lst = lst->next;
        while (lst)
        {
            if ((new_next = ft_lstnew(f(lst->content))) == NULL)
            {
               ft_lstclear(&new_head, del);
               return (NULL);
            }
            curr->next = new_next;
            curr = new_next;
            lst = lst->next;
        }
        return (new_head);
    }
  • lst = lst->next 를 와일문 위에서, 와일문 안에서 두 번 쓴 이유

    lst->next가 NULL이었을 경우 와일문 진입 못하게 하기 위해서.

    • NULL 인데 와일문 진입했을 경우 -> return NULL;

    • NULL 이여서 와일문 진입 안 했을 경우 -> return (new_head) (헤드만 있는 연결 리스트)

  • curr 임시 노드를 만든 이유.

    실제 new_head의 값이 new_next로 바뀌면 안돼서.

참고 :

여기 깃헙저장소
https://junistudy.tistory.com/2
메모리 구조 참고