1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | #include <iostream> #include <stdlib.h> /* run this program using the console pauser or add your own getch, system("pause") or input loop */ void myMemcpy(char** dst, const char* src, int nBytes); using namespace std; int main(int argc, char** argv) { char* src = "testHello"; // 9byte + 1byte(null terminate) = total 10byte. char* dst = (char*)malloc(sizeof(char) * 10); myMemcpy(&dst, src, 10); cout << "text : " << dst << endl; cout << "addr : " << &dst<< endl; free(dst); return 0; } void myMemcpy(char** dst, const char* src, int nBytes){ // Try to be fast and copy a word at a time instead of byte by byte int* wordDst = (int*)(*dst); int* wordSrc = (int*)src; int numWords = nBytes >> 2; for (int i=0; i < numWords; i++){ *wordDst++ = *wordSrc++; } int numRemaining = nBytes - (numWords << 2); char* byteDst = (char*)wordDst; char* byteSrc = (char*)wordSrc; for (int i=0 ; i < numRemaining; i++){ *byteDst++ = *byteSrc++; } cout << "text : " << *dst << endl; cout << "addr : " << &dst<< endl; } |
2019년 4월 13일 토요일
# 메모리 복사하는 간단한 함수 C++
피드 구독하기:
댓글 (Atom)
댓글 없음:
댓글 쓰기