zoukankan      html  css  js  c++  java
  • 避免内存重叠memmove()性能

    #include <iostream>
    #include <string.h>
    using namespace std;
    void* memmove(void *dst, const void *src, size_t count){
    	// 容错处理
    	if (dst == NULL || src == NULL){
    		return NULL;
    	}
    	unsigned char *pdst = (unsigned char *)dst;
    	const unsigned char *psrc = (const unsigned char *)src;
    	//推断内存是否重叠
    	bool flag1 = (pdst >= psrc && pdst < psrc + count);
    	bool flag2 = (psrc >= pdst && psrc < pdst + count);
    	if (flag1 || flag2){
    		// 倒序拷贝
    		while (count){
    			*(pdst + count - 1) = *(psrc + count - 1);
    			count--;
    		}//while
    	}
    	else{
    		// 拷贝
    		while (count--){
    			*pdst = *psrc;
    			pdst++;
    			psrc++;
    		}//while
    	}
    	return dst;
    }

    版权声明:本文博主原创文章,博客,未经同意不得转载。

  • 相关阅读:
    jquery 選擇器
    jquery 語法
    jQuery 簡介
    js cookies
    基本数据类型
    python----编程语言介绍
    Python---计算机基础
    复习os模块常用的一些操作
    模块的初识
    模块和包
  • 原文地址:https://www.cnblogs.com/bhlsheji/p/4906167.html
Copyright © 2011-2022 走看看