原型
void * memset ( void * ptr, int value, size_t num );
功能
这个函数通常为新申请的内存做初始化工作。
Fill block of memory
Sets the first num bytes of the block of memory pointed by ptr to the specified value (interpreted as an unsigned char).
参数
ptr
Pointer to the block of memory to fill.
value
Value to be set. The value is passed as an int, but the function fills the block of memory using the unsigned char conversion of this value.
num
Number of bytes to be set to the value.
size_t is an unsigned integral type.
例子
/* memset example */
#include <stdio.h>
#include <string.h>
int main ()
{
char str[] = "almost every programmer should know memset!";
memset (str,'-',6);
puts (str);
return 0;
}
output:------ every programmer should know memset!
实现
void * memset(void * ptr, int value, size_t num)
{
char* ptr_func = (char*)ptr;
if (ptr == nullptr || num < 0)
return nullptr;
while (num--) {
*ptr_func++ = value;
}
return ptr;
}
memcpy/memmove/memset/strcpy/my_strcpy/strlen/strcat/strcmp实现代码测试
#include <iostream>
#include <vector>
#include <stack>
#include <string>
#include <assert.h>
using namespace std;
class Solution {
public:
//移动内存
void* memcpy(void* dest, const void* src, size_t n);
void* memmove(void* dest, const void* src, size_t n);
void * memset(void * ptr, int value, size_t num);
char* strcpy(char* dest, const char* src);
char* my_strcpy(char* dest, const char* src);
size_t strlen(const char* str);
char* strcat(char* dest, const char* src);
int strcmp(const char* str1, const char* str2);
};
int main()
{
Solution sol;
char str1[20] = "hello world";
char str2[40], str3[40], str4[40];
sol.memcpy(str2, str1, sizeof(str1) / sizeof(str1[0]));
sol.memmove(str3, str1, sizeof(str1) / sizeof(str1[0]));
sol.strcpy(str4, str1);
//sol.strcpy(str1, str1 + 1);
//sol.my_strcpy(str1 + 1, str1);
char str5[20] = "hello world";
int len = sol.strlen(str1);
sol.strcat(str1, str5);
int cmp = sol.strcmp(str1, str5);
sol.memset(str1, 0, strlen(str1)/sizeof(str1[0]));
cout << str2 << endl << str3 << endl << str4;
cout << endl;
system("pause");
return 0;
}
//无需考虑内存重叠问题
void * Solution::memcpy(void * dest, const void * src, size_t n)
{
char* d = (char*)dest;
const char* s = (const char*) src;
while (n--)
{
*d++ = *s++;
}
return dest;
}
void * Solution::memmove(void * dest, const void * src, size_t n)
{
char* d = (char*)dest;
const char* s = (const char*)src;
if (s > d)//源地址在后,从前往后
{
while (n--)
*d++ = *s++;
}
if (s < d)//源地址在前,从后往前
{
d = d + n - 1;
s = s + n - 1;
while (n--)
{
*d-- = *s--;
}
}
return dest;
}
void * Solution::memset(void * ptr, int value, size_t num)
{
char* ptr_func = (char*)ptr;
if (ptr == nullptr || num < 0)
return nullptr;
while (num--) {
*ptr_func++ = value;
}
return ptr;
}
char * Solution::strcpy(char * dest, const char * src)
{
assert(dest != nullptr && src != nullptr);
char * address = dest;
while ((*dest++ = *src++) != '\0')
;
return address;
}
char * Solution::my_strcpy(char * dest, const char * src)
{
assert(dest != nullptr && src != nullptr);
char *d = dest;
int srcLen = strlen(src) + 1;//加上'\0'
if (d > src)//从后往前拷贝
{
d = d + srcLen - 1;
src = src + srcLen - 1;
while (srcLen--)
*d-- = *src--;
}
if (d < src)//从前往后拷贝
{
while (srcLen--)
*d++ = *src++;
}
//d = src时不拷贝,直接返回
return d;
}
size_t Solution::strlen(const char * str)
{
assert(str != nullptr);
int len = 0;
while (*str++ != '\0')
{
len++;
}
return len;
}
char * Solution::strcat(char * dest, const char * src)
{
assert(dest != nullptr && src != nullptr);
char *d = dest;
while (*d != '\0')//移动字符串到末尾
d++;
while (*src != '\0')
{
*d++ = *src++;
}
return d;
}
int Solution::strcmp(const char * str1, const char * str2)
{
assert(str1 != nullptr && str2 != nullptr);
while (*str1 == *str2)//不等或者遇到'\0'时返回结果
{
if (*str1 == '\0')
return 0;
str1++;
str2++;
}
return *str1 - *str2;
}