C\C++幾個內存處理函數

1、memcpy

void * memcpy ( void * destination, const void * source, size_t num );

複製內存塊

從source指向的位置直接複製num個字節的值到destination指向的內存塊。

對於這個函數,source和destination指向的對象的底層類型是無關的;結果是數據的一個二進制複製。

這個函數不檢查任何結束空字符(null)—— 它總是準確地複製num個字節。

爲了避免溢出,source和destination兩個參數指向的數組的大小應該至少是num字節,並且不應該重疊(對於重疊內存塊,memmove是一個更安全的方法))。

參數

destination
       指向內容被複制到的目的數組的指針,類型被轉換爲void*。
source
       指向複製數據的源數組的指針,類型被轉換爲void*。
num
       要複製的字節數。

返回值

       返回目的指針destination。

Example:

/* memcpy example */
#include <stdio.h>
#include <string.h>

int main ()
{
  char str1[]="Sample string";
  char str2[40];
  char str3[40];
  memcpy (str2,str1,strlen(str1)+1);
  memcpy (str3,"copy successful",16);
  printf ("str1: %s\nstr2: %s\nstr3: %s\n",str1,str2,str3);
  return 0;
}

Output:

str1: Sample string
str2: Sample string
str3: copy successful

2、memmove

void * memmove ( void * destination, const void * source, size_t num );

移動內存塊

從source指向的位置複製num個字節的值到destination指向的內存塊。複製操作的發生就好像使用了中間緩存,允許destination和source重疊。

對於這個函數,source和destination指向的對象的底層類型是無關的;結果是數據的一個二進制複製。

這個函數不檢查任何結束空字符(null)—— 它總是準確地複製num個字節。

爲了避免溢出,source和destination兩個參數指向的數組的大小應該至少是num字節。

參數

destination
       指向內容被複制到的目的數組的指針,類型被轉換爲void*。
source
       指向複製數據的源數組的指針,類型被轉換爲void*。
num
       要複製的字節數。

返回值

       返回目的指針destination。

Example:

/* memmove example */
#include <stdio.h>
#include <string.h>

int main ()
{
  char str[] = "memmove can be very useful......";
  memmove (str+20,str+15,11);
  puts (str);
  return 0;
}

Output:

memmove can be very very useful.
3、memset

void * memset ( void * ptr, int value, size_t num );

填充內存塊

將ptr指向的沒存塊的前num個字節設置爲特定的值value(被解釋爲一個unsigned char類型的值)。

參數

ptr
       指向需要填充的內存塊的指針。
value
       要設置的值,這個值是作爲int型傳遞的,但是該函數用這個值的unsigned char類型轉換值來填充內存塊。
num
       要是指特定值的字節數。

返回值

返回指針ptr。

Example:

/* memset example */
#include <stdio.h>
#include <string.h>

int main ()
{
  char str[] = "almost every programmer should know memset!";
  memset (str,'-',6);
  puts (str);
  return 0;
}

Output:

------ every programmer should know memset!

4、memchr

const void * memchr ( const void * ptr, int value, size_t num );
      void * memchr (       void * ptr, int value, size_t num );
在內存塊中定位字符
在ptr指向的沒存塊的前num個字節中搜索value值得第一次出現(被解釋爲一個unsigned char類型的值),並返回指向該值的指針。

參數

ptr
       指向需要被執行搜索的內存塊的指針。
value
       需要被定位的值。這個值是作爲int型傳遞的,但是該函數用這個值的unsigned char類型轉換值來進行逐位的搜索。
num
       要是分析的字節數。

返回值

       返回指向ptr指向的內存塊中value值第一次出現位置的指針。

       如果沒有找到value值,則該函數返回NULL。

Example:

/* memchr example */
#include <stdio.h>
#include <string.h>

int main ()
{
  char * pch;
  char str[] = "Example string";
  pch = (char*) memchr (str, 'p', strlen(str));
  if (pch!=NULL)
    printf ("'p' found at position %d.\n", pch-str+1);
  else
    printf ("'p' not found.\n");
  return 0;
}

Output:

'p' found at position 5.
5、memcmp

int memcmp ( const void * ptr1, const void * ptr2, size_t num );

比較兩個內存塊

將ptr2指向的內存塊的籤num個字節與ptr2指向的內存塊的前num個字節進行比較,如果它們都相符合則返回0,反之則返回一個指示其中一個較大的非零值。

參數

ptr1
       指向內存塊的指針。
ptr2
       指向內存塊的指針
num
       需要比較的字節數。

返回值

返回一個指示兩個內存塊的內容間的關係的整型值。

0值表示兩個內存塊的內容是相等的。

一個大於0的值表示,兩個內存塊中第一個不匹配的字節是ptr1的比ptr2的大,作爲unsigned char類型比較的;小於0則表示相反。

Example:

/* memcmp example */
#include <stdio.h>
#include <string.h>

int main ()
{
  char str1[256];
  char str2[256];
  int n;
  printf ("Enter a sentence: "); gets(str1);
  printf ("Enter another sentence: "); gets(str2);
  n=memcmp ( str1, str2, 256 );
  if (n>0) printf ("'%s' is greater than '%s'.\n",str1,str2);
  else if (n<0) printf ("'%s' is less than '%s'.\n",str1,str2);
  else printf ("'%s' is the same as '%s'.\n",str1,str2);
  return 0;
}

Output:

Enter a sentence: building
Enter another sentence: book
'building' is greater than 'book'.




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章