초시 단언

2321 단어 단언
#include <stdio.h>

#include <stdlib.h>



//#define NDEBUG	//  NDBUG

#include <assert.h>

typedef unsigned char byte;



#define DEBUG	//       





/* memcpy ───          */

void* memcpy(void* pvTo, void* pvFrom, size_t size)

{

	byte* pbTo = (byte*)pvTo;

	byte* pbFrom = (byte*)pvFrom;

	if(pvTo == NULL || pvFrom == NULL)

	{

		fprintf(stderr, "Bad args in memcpy
"); abort(); } while(size-->0) *pbTo++ = *pbFrom++; return(pvTo); } void* memcpy2(void* pvTo, void* pvFrom, size_t size) { byte* pbTo = (byte*)pvTo; byte* pbFrom = (byte*)pvFrom; #ifdef DEBUG if(pvTo == NULL || pvFrom == NULL) { fprintf(stderr, "Bad args in memcpy
"); abort(); } #endif while(size-->0) *pbTo++ = *pbFrom++; return(pvTo); } void* memcpy3(void* pvTo, void* pvFrom, size_t size) { byte* pbTo = (byte*)pvTo; byte* pbFrom = (byte*)pvFrom; assert(NULL != pvTo && NULL != pvFrom ); while(size-->0) *pbTo++ = *pbFrom++; return(pvTo); } #ifdef DEBUG void _Assert(char* , unsigned); /* */ #define ASSERT(f) \ if(f) \ NULL; \ else \ _Assert(__FILE__ , __LINE__)// #else #define ASSERT(f) NULL #endif void _Assert(char* strFile, unsigned uLine) { fflush(stdout); fprintf(stderr, "
Assertion failed: %s, line %u
",strFile, uLine); fflush(stderr); abort(); } void* memcpy4(void* pvTo, void* pvFrom, size_t size) { byte* pbTo = (byte*)pvTo; byte* pbFrom = (byte*)pvFrom; ASSERT(NULL != pvTo && NULL != pvFrom ); while(size-->0) *pbTo++ = *pbFrom++; return(pvTo); } /* memcpy ─── */ void* memcpy5(void* pvTo, void* pvFrom, size_t size) { byte* pbTo = (byte*)pvTo; byte* pbFrom = (byte*)pvFrom; ASSERT(NULL != pvTo && NULL != pvFrom ); // ASSERT(pbFrom+size <= pbTo || pbTo+size <= pbFrom); /* ? , memmove */ while(size-->0) *pbTo++ = *pbFrom++; return(pvTo); } const int len = 20; int main() { char *pchFrom = new char[len]; pchFrom[0] = 's'; pchFrom[1] = '\0'; char *pchTo = NULL; //memcpy3(pchTo, pchFrom,len); //memcpy4(pchTo, pchFrom,len); memcpy5(pchTo, pchFrom,len); return 0; }

좋은 웹페이지 즐겨찾기