linux系統庫函式之memcpy
阿新 • • 發佈:2019-02-18
551 #ifndef __HAVE_ARCH_MEMCPY
552 /**
553 * memcpy - Copy one area of memory to another
554 * @dest: Where to copy to
555 * @src: Where to copy from
556 * @count: The size of the area.
557 *
558 * You should not use this function to access IO space, use memcpy_toio()
559 * or memcpy_fromio() instead.
560 */
561 void *memcpy(void *dest, const void *src, size_t count)
562 {
563 char *tmp = dest;
564 const char *s = src;
565
566 while (count--)
567 *tmp++ = *s++;
568 return dest;
569 }
570 EXPORT_SYMBOL(memcpy);
552 /**
553 * memcpy - Copy one area of memory to another
554 * @dest: Where to copy to
555 * @src: Where to copy from
556 * @count: The size of the area.
557 *
558 * You should not use this function to access IO space, use memcpy_toio()
559 * or memcpy_fromio() instead.
560 */
561 void *memcpy(void *dest, const void *src, size_t count)
562 {
563 char *tmp = dest;
564 const char *s = src;
565
566 while (count--)
567 *tmp++ = *s++;
568 return dest;
569 }
570 EXPORT_SYMBOL(memcpy);
571 #endif
memcpy函式是記憶體拷貝函式,用於將一段記憶體空間資料拷貝到另一段記憶體空間中,但是它和memmove函式不同的是,它對記憶體空間有要求的,dest和src所指向的記憶體空間不能重疊,否則的資料是錯誤的。
例如:
src所指向的記憶體空間後面部分資料被新拷貝的資料給覆蓋了,所以拷貝到最後,資料肯定不是原來的資料。
如果記憶體空間像上圖所示,就不會導致資料拷貝錯誤這種情況,也就說你在使用這個函式之前,還需要做一個判斷,如果dest < src你才能使用這個函式,不過完全沒有必要,你直接使用另外一個函式memmove函式就可以了。