본문 바로가기
Code/C

[C언어] 내장함수 비교 - memcpy, memccpy / memmove / memchr / memcmp

by 코드포휴먼 2020. 10. 8.

#include <string.h>에 있는 메모리 관련 함수를 정리한다.

레퍼런스는 man 가이드다.

 

 

memcpy와 memccpy 비교

메모리 영역을 복사하는 함수다.

PROTOTYPE #include <string.h>
void *memcpy
(void *restrict dst, const void *restrict src, size_t n);
#include <string.h>
void *memccpy
(void *restrict dst, const void *restrict src, int c, size_t n);
NAME copy memory area copy string until character found
DESCRIPTION The  memcpy() function copies n bytes from memory area src to memory area dst.   - The memccpy() function copies no more than n bytes from memory area src to memory area dst, stopping when the character c is found.
- 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.
RETURN a pointer to dst
(the original value of dst)
- a pointer to the next character in dst after c
- NULL 
: if c was not found in the first n characters of src.
CONSIDERATIONS - The memory areas must not overlap.
- Use memmove() if the memory areas do overlap.
- The source and destination strings should not overlap, as the behavior is undefined.

 

 

memmove

memmove는 memcpy의 과정과 유사하지만 temporary array에 한번 거쳐서 옮긴다는 점이 다르다.

따라서 memmove는 복사할 메모리가 겹칠 경우 사용한다.

PROTOTYPE #include <string.h>
void *memmove(void *dst, const void *src, size_t len);
NAME copy byte string (copy memory area)
DESCRIPTION The memmove() function copies len bytes from string src to string dst.
RETURN a pointer to dst
(the original value of dst)
CONSIDERATIONS The two strings may overlap; the copy is always done in a non-destructive manner.
(as though the bytes in src are first copied into a temporary array that does not overlap src or dst, and the bytes are then copied from the temporary array to dst.) 

 

 

memchr

PROTOTYPE #include <string.h>
void *memchr(const void *s, int c, size_t n);
NAME locate byte in byte string
(scan memory for a character)
DESCRIPTION - The memchr() function locates the first occurrence of c (converted to an unsigned charin string s.
- The  memchr() function scans the initial n bytes of the memory area pointed to by s for the first instance of c.
- Both c and the bytes of the memory area pointed to by s are interpreted as unsigned char.
RETURN - a pointer to the byte located (to the matching byte)
- NULL
: if no such byte exists within n bytes (if the character does not occur in the given memory).
CONSIDERATIONS  

 

 

memcmp

For a nonzero return value, the sign is determined by the sign of the difference between the first pair of bytes (interpreted  as  unsigned char) that differ in s1 and s2.

PROTOTYPE #include <string.h>
int memcmp(const void *s1, const void *s2, size_t n);
NAME compare byte string
(compare memory areas)
DESCRIPTION - The memcmp() function compares the first n bytes (each interpreted as unsigned char) of the memory areas s1 and s2.
- s1 and s2 are treated as unsigned char values, so that `\200' is greater than `\0', for example.
- The memcmp() function compares byte string s1 against byte string s2.  Both strings are assumed to be n bytes long.

RETURN - an integer less than, equal to, or greater than zero
(the difference between the first two differing bytes)
: if the first n bytes of s1 is found, respectively, to be less than, to match, or be greater than the first n bytes of s2. 
- zero
: if two strings are identical / if n is zero
CONSIDERATIONS Zero-length strings are always identical.

 

댓글