본문 바로가기

코딩/C

Libft (Part 1 : Libc Function)

<틀린 부분, 부족한 부분 언제든지 댓글 달아주세요. 아직 코린이입니다.>

You must re-code the following functions. These function do not need any external functions

1. memset

void    *ft_memset(void *b, int c, size_t len)

man memset

description : The memset() function writes len bytes of value c (converted to an unsigned char) to the string b.
return : The memset() function returns its first argument.

- 메모리 블록 채우기

- b로 시작하는 메모리 주소부터 len만큼 바이트를 c로 채운다.

- cunsigned char로 형변환

- b가 리턴된다.


2. bzero

void    ft_bzero(void *s, size_t n)

man bzero

description : The bzero() function writes n zeroed bytes to the string s. If n is zero, bzero() does nothing.

- 초기화

- s영역에 n크기만큼 초기화


3. memcpy

void    *ft_memcpy(void *dst, const void *src, size_t n)

man memcpy

description : The memcpy() function copies n bytes from memory area src to memory area dst. If dst and src overlap, behavior is undefined. Applications in which dst and src might overlap should use memmove(3) instead.

return : The memcpy() function returns the original value of dst.

- 메모리 일부를 복사한다.

- src 가리키는 곳부터 n만큼 dst가 가리키는 곳에 복사

- dstsrc 타입은 위함수와 무관

- dstsrc 가리키는 배열 크기는 항상 num이상이어야 한다.(블록이 겹쳐있으면 memmove함수 이용할 것)


4. memccpy

void    *ft_memccpy(void *dst, const void *src, int c, size_t n)

man memcpy

description
- The memccpy() function copies bytes from string src to string dst. If the character c (as converted to an unsigned char) occurs in the string src, the copy stops and a pointer to the byte after the copy of c in the string dst is returned. Otherwise, n bytes are copied, and a NULL pointer is returned.
- The source and destination strings should not overlap, as the behavior is undefined.

- memcpy와 같은 원리

- 차이점 : srcc가 있으면 복사 중단 > dstc다음의 바이트 가리키는 포인터 반환

- 없을 경우 n만큼 복사된 후 NULL포인터 반환


5. memmove

void    *ft_memmove(void *dst, const void *src, size_t len)

man memmove

description : The memmove() function copies len bytes from string src to string dst. The two strings may overlap; the copy is always done in a non-destructive manner.

return : The memmove() function returns the original value of dst.

- 메모리 블록을 옮긴다.

- src가 가리키는 곳 부터 len만큼 det가 가리키는 곳으로 옮긴다.

- Overflow 방지를 위해 dstsrc가 가리키는 배열은 n바이트 이상 되어야 한다.

- 42seoul slack에 올라온 설명 정리


6. memchr

void    *ft_memchr(const void *s, int c, size_t n)

man memchr

description : The memchr() function locates the first occurrence of c (converted to an unsigned char) in string s.
return : The memchr() function returns a pointer to the byte located, or NULL if no such byte exists within n bytes.

- 메모리 블록에서 문자 찾음.

- s가 가리키는 메모리의 처음 n바이트 중에서 처음으로 c와 일치하는 값 주소 리턴


7. memcmp

int    ft_memcmp(const void *s1, const void *s2, size_t n)

man memcmp

description : The memcmp() function compares byte string s1 against byte string s2. Both strings are assumed to be n bytes long.

return : The memcmp() function returns zero if the two strings are identical, otherwise returns the difference between the first two differing bytes (treated as unsigned char values, so that \200 is greater than \0, for example). Zero-length strings are always identical. This behavior is not required by C and portable code should only depend on the sign of the returned value.

- 두 개의 메모리 블록 비교

- 두 메모리 블록 같으면 0 리턴

- 다를 경우 unsigned char의 차이값만큼 리턴(s1이 클 경우 0보다 큰 값 / s2가 클 경우 0보다 작은 값)


8. strlen

size_t    ft_strlen(const char *s)

man strlen

description : The strlen() function computes the length of the string s. The strnlen() function attempts to compute the length of s, but never scans beyond the first maxlen bytes of s.
The strlen() function returns the number of characters that precede the terminating NUL character. The strnlen() function returns either the same result as strlen() or maxlen, whichever is smaller.

- 문자열 길이


9. strlcpy

size_t    ft_strlcpy(char *dst, const char *src, size_t dstsize)

man strlcpy

man strlcpy와 strlcat이 같이 나와있다. 너무 길다....ㅜㅜ

- 생성하려고 시도한 문자열 전체 길이 반환 = src 길이

- dstsrcdstsize만큼 복사 (마지막은 NULL값이기 때문에 dstsize보다 1 작게 복사

- 복사 후 dst 마지막에 NULL 넣어줘야 한다.


10. strlcat

size_t    ft_strlcat(char *dst, const char *src, size_t dstsize)

man strlcat

마찬가지다 위와 동일하다. 너무 길다....ㅜㅜ

- 생성하려고 시도한 문자열 전체 길이 반환 = dst의 초기 길이 + src의 길이

- dst뒤에 src를 붙이는 함수 (dst길이 : dstsize)

- dstsize보다 dst가 클 경우 : dstsize + scr길이 리턴


11. strchr

char    *ft_strchr(const char *s, int c)

man strchr

description
- The strchr() function locates the first occurrence of c (converted to a char) in the string pointed to by s. The terminating null character is considered to be part of the string; therefore if c is \0, the functions locate the terminating \0.
- The strrchr() function is identical to strchr(), except it locates the last occur-rence of c.

return : The functions strchr() and strrchr() return a pointer to the located character, or NULL if the character does not appear in the string.

- 문자열 내 일치하는 문자가 있는가

- 문자열에서 c부터 리턴 (c의 첫 번째 표시에 대한 포인터)

- 찾지 못하면 NULL 리턴


12. strrchr

char    *ft_strrchr(const char *s, int c)

man strrchr

description
- The strchr() function locates the first occurrence of c (converted to a char) in the string pointed to by s. The terminating null character is considered to be part of the string; therefore if c is \0, the functions locate the terminating \0.
- The strrchr() function is identical to strchr(), except it locates the last occur-rence of c.

- 문자열 뒤부터 봤을 때 일치하는 c가 있는가?

- c의 마지막 표시에 대한 포인터 리턴

- 찾지 못하면 NULL 리턴


13. strnstr

char    *ft_strnstr(const char *haystack, const char *needle, size_t len)

man strnstr

해당 함수도 직접 찾아보는 것을 추천드린다.

- haystack의 문자열 안에 needle문자열이 있는가

- 최대 len의 수만 탐색

- neddle문자열이 비어있을 경우 haystack문자열 리턴

- 찾은 경우 찾은 첫 글자 포인터 리턴 / 못찾으면 NULL 리턴


14. strncmp

int    *ft_strncmp(const char *s1, const char *s2, size_t n)

man strncmp

description
- The strcmp() and strncmp() functions lexicographically compare the null-terminated strings s1 and s2.
- The strncmp() function compares not more than n characters. Because strncmp() is designed for comparing strings rather than binary data, characters that appear after a \0 character are not compared.

return : The strcmp() and strncmp() functions return an integer greater than, equal to, or less than 0, according as the string s1 is greater than, equal to, or less than the string s2. The comparison is done using unsigned characters, so that \200 is greater than \0.

- 문자열 비교

- n까지 일치하면 0 리턴

- 일치하지 않을 경우 ascii코드 값 비교를 통해서 s1이 클 경우 양수 / s2가 클 경우 음수 리턴


15. atoi

int    ft_atoi(const char *str)

man atoi

description
The atoi() function converts the initial portion of the string pointed to by str to int representation.
It is equivalent to:
(int)strtol(str, (char **)NULL, 10);
While the atoi() function uses the current locale, the atoi_l() function may be passed a locale directly. See xlocale(3) for more information.

- isspace 판별기 필요

- Piscine 과정에서 했던 atoi가 아닌 시험에서 나온 atoi 조건에 대해 만족해야 하는 것 같다.

- 계산 값이 int 범위를 벗어난다면 : 양수일 경우 -1 / 음수일 경우 0

- 부호 신경써야한다.


16. isalpha, isdigit, isalnum, isascii, isprint, toupper, tolower

- 해당 함수들은 ascii코드를 사용해서 해결하는 함수들


You must also re-code the following functions, using the function malloc

17. calloc

void    *ft_calloc(size_t count, size_t size)

man calloc

description
- The calloc() function contiguously allocates enough space for count objects that are size bytes of memory each and returns a pointer to the allocated memory. The allocated memory is filled with bytes of value zero.

- malloc을 이용 count * size만큼 할당
- bzero를 활용해 할당한 배열을 0으로 채움


18. strdup

char    *ft_strdup(const char *s1)

man strdup

description
- The strdup() function allocates sufficient memory for a copy of the string s1, does the copy, and returns a pointer to it. The pointer may subsequently be used as an argument to the function free(3).
- If insufficient memory is available, NULL is returned and errno is set to ENOMEM.
-The strndup() function copies at most n characters from the string s1 always NUL terminating the copied string.

- strlcpy함수 사용
- malloc함수 사용을 위한 strlen함수 이용

'코딩 > C' 카테고리의 다른 글

Libft(Bonus Part)  (0) 2021.02.06
Libft (Part 2 : Additional functions)  (0) 2021.02.06