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

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 閱讀(562) 評(píng)論(0)  編輯 收藏 引用


只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。
網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


導(dǎo)航

<2025年9月>
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011

統(tǒng)計(jì)

常用鏈接

留言簿

隨筆檔案

搜索

最新評(píng)論

閱讀排行榜

評(píng)論排行榜

青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <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ⅴ一区二区三区视频| 一本一道久久综合狠狠老精东影业| 亚洲美女在线观看| 国产精品青草综合久久久久99| 久久国产精品久久久久久电车| 久久av红桃一区二区小说| 黄色另类av| 亚洲精品影视| 国产一区二区三区四区五区美女| 欧美成人黄色小视频| 欧美日韩成人一区二区| 久久久国产精品亚洲一区| 免费观看不卡av| 欧美一级成年大片在线观看| 久久最新视频| 午夜精品一区二区三区在线视| 久久精品夜色噜噜亚洲a∨ | 亚洲国产成人tv| 亚洲视频一区二区| 亚洲国产一区二区精品专区| 亚洲深夜av| 亚洲精品国产视频| 久久国产一区二区三区| 一本大道av伊人久久综合| 一本色道久久综合亚洲精品婷婷| 国产真实久久| 亚洲视频成人| 一本色道久久88综合亚洲精品ⅰ | 欧美欧美全黄| 久久精视频免费在线久久完整在线看| 99精品99| 久久精品国产一区二区三区| 中文国产成人精品久久一| 久久人人看视频| 欧美有码视频| 国产精品欧美久久| 亚洲裸体俱乐部裸体舞表演av| 亚洲国产高清aⅴ视频| 亚洲欧美一区在线| 亚洲一区www| 欧美高潮视频| 亚洲黑丝一区二区| 在线精品亚洲| 久久九九热免费视频| 亚洲免费在线视频一区 二区| 免费在线观看成人av| 免费试看一区| 在线观看亚洲专区| 久久久久久91香蕉国产| 久久精品一区四区| 国产精自产拍久久久久久蜜| 亚洲一级在线观看| 亚洲欧美国产精品桃花| 国产精品久久久久久久久搜平片| 99精品免费| 亚洲天堂成人在线观看| 欧美日韩一区自拍| 中国日韩欧美久久久久久久久| 日韩亚洲欧美高清| 欧美日本韩国在线| 一本色道久久88亚洲综合88| 亚洲一区影院| 国产精品视频免费一区| 亚洲欧美国产日韩天堂区| 欧美中文字幕在线| 国内精品视频在线观看| 久久久久久久波多野高潮日日| 久久综合狠狠综合久久激情| 亚洲国产精品成人综合| 欧美国产日韩xxxxx| 亚洲破处大片| 午夜精品久久久久久久蜜桃app | 国产一级揄自揄精品视频| 性欧美精品高清| 麻豆精品精华液| 91久久夜色精品国产九色| 欧美黄色一级视频| 夜夜爽99久久国产综合精品女不卡| 宅男噜噜噜66一区二区66| 欧美性猛交xxxx乱大交蜜桃| 欧美伊人久久久久久午夜久久久久 | 国产精品日日摸夜夜摸av| 性欧美超级视频| 欧美黄色日本| 亚洲香蕉网站| 精品av久久707| 欧美日韩亚洲一区二区三区在线观看 | 亚洲自拍偷拍福利| 国产在线不卡| 欧美日韩不卡| 欧美在线观看网站| 亚洲精品视频一区| 久久久人成影片一区二区三区观看| 最新成人在线| 国产婷婷精品| 欧美日韩国产经典色站一区二区三区| 亚洲欧美日韩一区二区| 欧美激情网站在线观看| 欧美一级理论性理论a| 亚洲丰满在线| 国产视频欧美视频| 欧美伦理91| 久久婷婷久久| 午夜久久久久久| 亚洲精品久久嫩草网站秘色| 久久全国免费视频| 亚洲尤物在线| 亚洲作爱视频| 91久久精品久久国产性色也91 | 久久久免费精品视频| 宅男66日本亚洲欧美视频| 欧美成人在线影院| 久久久久九九九| 性做久久久久久久免费看| 亚洲人成在线观看| 在线电影院国产精品| 国产精品午夜av在线| 欧美日韩免费看| 欧美成人精品h版在线观看| 久久久久国产精品麻豆ai换脸| 亚洲图片在线| 在线一区免费观看| 一本高清dvd不卡在线观看| 亚洲国产第一| 亚洲国产精品久久久久秋霞蜜臀 | 亚洲欧美在线看| 亚洲欧美久久久久一区二区三区| 在线亚洲免费| 中文精品一区二区三区 | 久热精品在线| 久久躁日日躁aaaaxxxx| 久久久精品tv| 久久久久久尹人网香蕉| 久久免费偷拍视频| 久久午夜电影| 美女精品在线观看| 免费观看成人| 亚洲国产成人不卡| 亚洲精品美女| 99精品欧美一区二区三区 | 欧美高清视频一区| 欧美77777| 亚洲国产第一页| 亚洲最新在线| 亚洲欧美日本伦理| 久久精品日产第一区二区三区| 久久久久久久一区二区| 久久亚洲二区| 欧美女同在线视频| 国产精品爽爽ⅴa在线观看| 国产综合久久久久影院| 亚洲二区在线视频| 宅男噜噜噜66国产日韩在线观看| 亚洲性xxxx| 久久精品国产亚洲精品| 免费成人你懂的| 亚洲精品一区二区三区福利| 亚洲免费在线电影| 久久久亚洲欧洲日产国码αv| 欧美成人日本| 国产精品久久网站| 激情小说另类小说亚洲欧美| 亚洲精品一区二区网址| 亚洲免费在线观看| 美日韩精品视频| 一本色道久久88综合日韩精品| 欧美一区二区免费观在线| 快播亚洲色图| 国产精品欧美激情| 91久久国产综合久久| 亚洲欧美精品在线观看| 免费在线欧美黄色| 一本色道**综合亚洲精品蜜桃冫| 久久国产一区二区| 欧美四级在线| 亚洲国产精品va| 欧美一区二区三区喷汁尤物| 亚洲国产欧美在线人成| 亚洲欧美日韩专区| 欧美日韩国产成人在线| 国内精品免费在线观看| 一区二区三区欧美视频| 免费视频一区| 欧美一区网站| 国产精品久久九九| 一本久久精品一区二区| 麻豆久久婷婷| 欧美尤物巨大精品爽| 国产精品久久久久免费a∨大胸|