青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

Using memset(), memcpy(), and memmove() in C

The article is from http://www.java-samples.com/showtutorial.php?tutorialid=591

To set all the bytes in a block of memory to a particular value, use memset(). The function prototype is

void * memset(void *dest, int c, size_t count);

The argument dest points to the block of memory. c is the value to set, and count is the number of bytes, starting at dest, to be set. Note that while c is a type int, it is treated as a type char. In other words, only the low-order byte is used, and you can specify values of c only in the range 0 through 255.

Use memset() to initialize a block of memory to a specified value. Because this function can use only a type char as the initialization value, it is not useful for working with blocks of data types other than type char, except when you want to initialize to 0. In other words, it wouldn't be efficient to use memset() to initialize an array of type int to the value 99, but you could initialize all array elements to the value 0. memset() will be demonstrated in program below.

The memcpy() Function

memcpy() copies bytes of data between memory blocks, sometimes called buffers. This function doesn't care about the type of data being copied--it simply makes an exact byte-for-byte copy. The function prototype is

void *memcpy(void *dest, void *src, size_t count);

The arguments dest and src point to the destination and source memory blocks, respectively. count specifies the number of bytes to be copied. The return value is dest. If the two blocks of memory overlap, the function might not operate properly--some of the data in src might be overwritten before being copied. Use the memmove() function, discussed next, to handle overlapping memory blocks. memcpy() will be demonstrated in program below.

The memmove() Function

memmove() is very much like memcpy(), copying a specified number of bytes from one memory block to another. It's more flexible, however, because it can handle overlapping memory blocks properly. Because memmove() can do everything memcpy() can do with the added flexibility of dealing with overlapping blocks, you rarely, if ever, should have a reason to use memcpy(). The prototype is

void *memmove(void *dest, void *src, size_t count);

dest and src point to the destination and source memory blocks, and count specifies the number of bytes to be copied. The return value is dest. If the blocks overlap, this function ensures that the source data in the overlapped region is copied before being overwritten. Sample program below demonstrates memset(), memcpy(), and memmove().

A demonstration of memset(), memcpy(), and memmove().

1: /* Demonstrating memset(), memcpy(), and memmove(). */
2:
3: #include <stdio.h>
4: #include <string.h>
4:
5: char message1[60] = "Four score and seven years ago ...";
6: char message2[60] = "abcdefghijklmnopqrstuvwxyz";
7: char temp[60];
8:
9: main()
10: {
11:    printf("\nmessage1[] before memset():\t%s", message1);
12:    memset(message1 + 5, `@', 10);
13:    printf("\nmessage1[] after memset():\t%s", message1);
14:
15:    strcpy(temp, message2);
16:    printf("\n\nOriginal message: %s", temp);
17:    memcpy(temp + 4, temp + 16, 10);
18:    printf("\nAfter memcpy() without overlap:\t%s", temp);
19:    strcpy(temp, message2);
20:    memcpy(temp + 6, temp + 4, 10);
21:    printf("\nAfter memcpy() with overlap:\t%s", temp);
22:
23:    strcpy(temp, message2);
24:    printf("\n\nOriginal message: %s", temp);
25:    memmove(temp + 4, temp + 16, 10);
26:    printf("\nAfter memmove() without overlap:\t%s", temp);
27:    strcpy(temp, message2);
28:    memmove(temp + 6, temp + 4, 10);
29:    printf("\nAfter memmove() with overlap:\t%s\n", temp);
30:
31: }
message1[] before memset():     Four score and seven years ago ...
message1[] after memset():      Four @@@@@@@@@@seven years ago ...
Original message: abcdefghijklmnopqrstuvwxyz
After memcpy() without overlap: abcdqrstuvwxyzopqrstuvwxyz
After memcpy() with overlap:    abcdefefefefefefqrstuvwxyz
Original message: abcdefghijklmnopqrstuvwxyz
After memmove() without overlap:        abcdqrstuvwxyzopqrstuvwxyz
After memmove() with overlap:   abcdefefghijklmnqrstuvwxyz

ANALYSIS: The operation of memset() is straightforward. Note how the pointer notation message1 + 5 is used to specify that memset() is to start setting characters at the sixth character in message1[] (remember, arrays are zero-based). As a result, the 6th through 15th characters in message1[] have been changed to @.

When source and destination do not overlap, memcpy() works fine. The 10 characters of temp[] starting at position 17 (the letters q through z) have been copied to positions 5 though 14, where the letters e though n were originally located. If, however, the source and destination overlap, things are different. When the function tries to copy 10 characters starting at position 4 to position 6, an overlap of 8 positions occurs. You might expect the letters e through n to be copied over the letters g through p. Instead, the letters e and f are repeated five times.

If there's no overlap, memmove() works just like memcpy(). With overlap, however, memmove() copies the original source characters to the destination.

posted on 2010-08-31 11:06 lhking 閱讀(580) 評論(0)  編輯 收藏 引用


只有注冊用戶登錄后才能發表評論。
網站導航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


導航

<2010年6月>
303112345
6789101112
13141516171819
20212223242526
27282930123
45678910

統計

常用鏈接

留言簿

隨筆檔案

搜索

最新評論

閱讀排行榜

評論排行榜

青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            亚洲日本成人| 日韩视频在线观看国产| 欧美在线你懂的| 午夜精品久久久久久久久久久久久| 欧美午夜一区| 亚洲欧美日韩在线观看a三区| 一区二区三区精密机械公司| 国产精品yjizz| 久久国产精品黑丝| 美女在线一区二区| 亚洲综合视频在线| 篠田优中文在线播放第一区| 国产亚洲视频在线| 亚洲国产精品一区| 欧美网站在线| 玖玖玖免费嫩草在线影院一区| 蜜桃久久精品一区二区| 亚洲网站在线看| 久久国产毛片| av成人老司机| 欧美怡红院视频| 亚洲精品一区二区三区樱花| 亚洲综合精品| 亚洲精品美女91| 亚洲欧美不卡| av成人福利| 久久精品亚洲一区二区| 一区二区三区免费看| 欧美在线免费看| 夜夜狂射影院欧美极品| 欧美伊久线香蕉线新在线| 亚洲精品一区在线观看| 午夜亚洲影视| 亚洲手机视频| 免费成人网www| 久久久久9999亚洲精品| 欧美丝袜一区二区| 欧美福利在线| 国产在线成人| 亚洲伊人第一页| 夜夜嗨av一区二区三区免费区| 午夜视频在线观看一区| 这里只有精品电影| 欧美成在线视频| 美女福利精品视频| 国产香蕉97碰碰久久人人| 99在线热播精品免费| 亚洲国产一区二区三区在线播 | 久久精品一区蜜桃臀影院| 欧美国产欧美综合| 男人的天堂亚洲| 国内视频一区| 午夜在线观看免费一区| 亚洲一区二区三区久久| 欧美激情亚洲精品| 欧美顶级艳妇交换群宴| 在线播放豆国产99亚洲| 欧美在线视频免费播放| 久久不见久久见免费视频1| 国产精品久久久久9999| 日韩一级在线| 一区二区欧美国产| 欧美日本一区| 一区二区三区视频观看| 一区二区三区蜜桃网| 欧美日韩一区二区三区高清| 亚洲看片网站| 一区二区三区免费在线观看| 欧美日韩国产经典色站一区二区三区| 欧美成人免费全部| 亚洲精品一区二区三区四区高清| 欧美**字幕| 亚洲精品在线免费观看视频| 99re热精品| 欧美视频在线播放| 一区二区三区高清在线| 午夜精品一区二区三区电影天堂 | 欧美午夜在线| 亚洲一区二区三区在线观看视频| 正在播放亚洲| 国产乱人伦精品一区二区| 欧美资源在线观看| 麻豆精品在线播放| 亚洲日本成人| 国产精品videossex久久发布| 亚洲一区制服诱惑| 蜜桃久久精品乱码一区二区| 136国产福利精品导航网址应用| 久热精品视频在线免费观看| 亚洲国产精品久久久久秋霞影院| 一区二区日韩| 国产揄拍国内精品对白| 欧美成人免费在线视频| 亚洲少妇自拍| 久久综合伊人77777麻豆| 亚洲伦理在线观看| 国产日产欧产精品推荐色 | 国产日韩欧美中文| 久久精品男女| 日韩视频中文字幕| 久久久久久久久伊人| 91久久中文字幕| 国产精品普通话对白| 久久视频国产精品免费视频在线 | 亚洲资源在线观看| 国产在线精品二区| 欧美日韩国产综合久久| 久久超碰97中文字幕| 亚洲精品一品区二品区三品区| 午夜日韩激情| 99精品国产热久久91蜜凸| 国产精品久久久久99| 麻豆成人精品| 欧美一区二区网站| 99re热精品| 亚洲国产精品精华液2区45| 欧美一区二区精美| 99精品国产福利在线观看免费| 国产无一区二区| 国产精品99免费看| 欧美aa国产视频| 久久亚洲精选| 久久av资源网| 午夜精品久久久久久久| 一本久久a久久免费精品不卡| 免费日韩成人| 久久精品在线播放| 性欧美1819性猛交| 亚洲在线1234| 亚洲在线黄色| 亚洲图片欧洲图片av| 亚洲最新在线| 亚洲美女福利视频网站| 91久久久久久久久久久久久| 伊人成人网在线看| 韩国av一区二区三区在线观看 | 久久另类ts人妖一区二区| 亚洲一区激情| 一本色道久久| 一区二区三区导航| 一本色道精品久久一区二区三区| 亚洲国产另类 国产精品国产免费| 久久精品亚洲精品| 久久9热精品视频| 久久精品国产视频| 久久字幕精品一区| 免费亚洲网站| 亚洲成色777777女色窝| 亚洲国产国产亚洲一二三| 亚洲第一成人在线| 亚洲精品国产欧美| 99精品欧美| 亚洲一区二区少妇| 欧美一区激情| 裸体丰满少妇做受久久99精品| 久久夜色精品一区| 欧美激情一级片一区二区| 欧美精品电影| 国产精一区二区三区| 国语自产在线不卡| 亚洲美女电影在线| 亚洲免费视频在线观看| 翔田千里一区二区| 久久综合激情| 亚洲精品欧美激情| 西瓜成人精品人成网站| 久久久无码精品亚洲日韩按摩| 美女被久久久| 国产精品欧美日韩一区| 国模精品一区二区三区| 亚洲精品日韩在线观看| 亚洲欧美精品一区| 免费观看一级特黄欧美大片| 亚洲人成人99网站| 亚洲伊人久久综合| 美国十次成人| 国产精品视频久久| 亚洲国产精品久久久久秋霞蜜臀 | 亚洲综合色自拍一区| 久久久久青草大香线综合精品| 欧美激情一级片一区二区| 一区二区高清视频| 久久久久久久久久久久久女国产乱| 美女主播精品视频一二三四| 国产精品美腿一区在线看| 亚洲激情电影在线| 欧美伊人精品成人久久综合97| 欧美大尺度在线| 亚洲欧美在线网| 欧美精品在线看| 亚洲第一精品影视| 午夜精品久久久久| 亚洲欧洲一区二区天堂久久| 久久高清一区| 国产精品男女猛烈高潮激情| 亚洲另类自拍| 美女脱光内衣内裤视频久久影院| 一区二区欧美国产| 欧美激情精品久久久久久| 韩国成人理伦片免费播放|