memcpy function and memmove

memcpy () and memmove () is a function in C language, which is declared in the header file string.h, specifically the following statement:

void* __cdecl memcpy(
    _Out_writes_bytes_all_(_Size) void* 	  _Dst,
    _In_reads_bytes_(_Size)       void const* _Src,
    _In_                          size_t      _Size
    );
    
_VCRTIMP void* __cdecl memmove(
    _Out_writes_bytes_all_opt_(_Size) void*       _Dst,
    _In_reads_bytes_opt_(_Size)       void const* _Src,
    _In_                              size_t      _Size
    );

Description
memcpy count of bytes to copy from the src target; wmemcpy copy count wide characters (two bytes). If the source and destination overlap, memcpy behavior is uncertain. Memmove process using the overlap region.

Reference
https://docs.microsoft.com/zh-cn/cpp/c-runtime-library/reference/memcpy-wmemcpy?view=vs-2019

In the C / C ++ written some of the more common requirements do not have standard library functions, functional mommove function here about self-summary:
function prototypes

void *memcpy(void *dst, const void *src, size_t size);
void *memmove(void *dst, const void *src, size_t size);

Function to achieve

void* memcpy(void* dst, const void* src, size_t size)
{
    char *dst_t = (char*)dst;
    char *src_t = (char*)src;
 
    while(size--) {
        *dst_t++ = *src_t++;
    }
    return dst;
}

Because memcpy is not covered by the deal with the situation, it is no longer recommended for use, but for memmove src <dst <src + case size needs to be moved from back to front, the situation is covered in order to deal properly.

void* memmove(void* dst, const void* src, size_t size)
{
    char* dst_t = (char*)dst;	// 指向首地址
    char* src_t = (char*)src;

    if(dst_t > src_t && (src_t + size>s_dst)) {  // 从后往前移动
        dst_t = dst_t + size - 1;
        src_t = src_t + size - 1;
        while(size--) {
            *dst_t-- = *src_t--;
        }
    }else {
        while(size--) {
            *dst_t++ = *src_t++;
        }
    }
    return dst;
}

<---------------------------- text dividing line ------------------ ---------->
randomly insert a few simple algorithm
1, binary search

int BinarySearch(int s[], int n, int key)
{
    int low=0, high=n-1;
    int mid;
    while(low <= high)
    {
        mid = (low + high) / 2;
        if(s[mid] == key)  return mid;
        else if(s[mid] > key)   high = mid - 1;
        else low = mid + 1
    }
    return -1;
}

2. Select Sort

void Select_Sort(int arr[],int sz)
{
    int i = 0;
    int j = 0;
    int temp = 0;
    int minIndex = 0;
    for (i = 0; i < sz; ++i)
    {
        minIndex = i;
        for (j = i+1; j < sz; ++j)
        {
            if (arr[minIndex]>arr[j])
                minIndex = j;//更新最小值的下标
        }
        if (arr[i]>arr[minIndex])
        {
            temp = arr[i];
            arr[i] = arr[minIndex];
            arr[minIndex] = temp;
        }
    }
}

time complexity O ( n 2 ) O (n ^ 2) , the spatial complexity O ( 1 ) O (1) .
3, Quick Sort

//递归实现快速排序
#include <cstdio>
#include<cstdlib>

void quickSort(int* arr,int low,int high);
int Partition(int* arr,int low,int high);

int main(int argc,char* argv[])
{
    int i,a[]={4,2,6,9,1,3,5};
    int length = sizeof(a)/sizeof(a[0]);
    quickSort(a,0,length-1);
    printf("After sorted:  ");
    for(i=0;i<length;i++)
        printf("%d ",a[i]);
}

void quickSort(int* arr,int low,int high)
{
    if(low<high)
    {
        int pivotloc = Partition(arr,low,high);
        quickSort(arr,low,pivotloc-1);
        quickSort(arr,pivotloc+1,high);
    }
}

int Partition(int* arr,int low,int high)
{
    int pivotkey = arr[low];
    while(low<high)
    {
        while(low<high && arr[high] >= pivotkey)
            --high;	// 从后往前扫
        arr[low] = arr[high];
        while(low<high && arr[low] <= pivotkey)
            ++low;	// 从前往后扫
        arr[high] = arr[low];
    }
    arr[low] = pivotkey;
    return low;
}

time complexity O ( l O g   n ) O(log\ n) , the spatial complexity O ( l O g   n ) O(log\ n) .

Published 25 original articles · won praise 23 · views 10000 +

Guess you like

Origin blog.csdn.net/Secur17y/article/details/101384305