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

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

導航

<2010年8月>
25262728293031
1234567
891011121314
15161718192021
22232425262728
2930311234

統計

常用鏈接

留言簿

隨筆檔案

搜索

最新評論

閱讀排行榜

評論排行榜

青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <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>
            国产亚洲欧洲| 国产精品免费小视频| 欧美成人午夜激情在线| 亚洲伊人网站| 欧美韩日精品| 亚洲人成7777| 久久久久免费| 久久精品99无色码中文字幕| 午夜精品久久久久久久蜜桃app | 亚洲香蕉成视频在线观看| 久久免费国产| 欧美在线免费看| 欧美中在线观看| 欧美视频一区二区三区…| 国产欧美日韩精品丝袜高跟鞋| 欧美不卡一卡二卡免费版| 亚洲免费婷婷| 久久男人av资源网站| 亚洲欧美激情一区二区| 欧美在线观看一区二区| 在线精品视频在线观看高清 | 久久精品成人一区二区三区 | 香蕉久久一区二区不卡无毒影院| 尤物99国产成人精品视频| 亚洲精品久久久久中文字幕欢迎你| 欧美在线视频播放| 亚洲第一区在线观看| 国产精品高潮呻吟久久av无限| 亚洲欧美日韩成人| 亚洲人线精品午夜| 久久久亚洲欧洲日产国码αv| 小嫩嫩精品导航| 久久高清免费观看| 亚洲中午字幕| 欧美护士18xxxxhd| 一区二区三区国产精华| 亚洲欧美视频在线观看| 亚洲激情第一区| 裸体女人亚洲精品一区| 国产综合视频| 麻豆精品视频在线| 久久久久欧美精品| 91久久夜色精品国产九色| 欧美www在线| 免费观看在线综合色| 亚洲国产一区二区在线| 亚洲国产专区| 国产精品久久激情| 久久久国产精彩视频美女艺术照福利| 亚洲永久免费观看| 狠狠色狠狠色综合日日tαg| 噜噜噜91成人网| 欧美日韩亚洲一区二区三区| 亚洲欧美卡通另类91av| 欧美在线999| 中文久久乱码一区二区| 久久久久久9| 夜夜嗨av一区二区三区网页| 亚洲私人黄色宅男| 亚洲高清视频一区| 一本久道久久综合中文字幕| 国产一区二区三区高清| 日韩午夜在线播放| 亚洲福利国产| 久久精品久久综合| 亚洲一区二区三区在线播放| 午夜精品美女久久久久av福利| 亚洲欧美成人在线| 日韩视频在线免费| 亚洲美女黄色| 亚洲免费黄色| 国产精品国产三级国产| 久久精品官网| 国产精品成人一区二区三区夜夜夜 | 欧美黄在线观看| 久久偷窥视频| 国产一区二区三区电影在线观看 | 久久久成人网| 国产模特精品视频久久久久 | 亚洲在线观看免费视频| 欧美日产国产成人免费图片| 美女成人午夜| 亚洲国产日韩一区| 欧美成年人视频网站欧美| 麻豆精品一区二区av白丝在线| 国产日韩精品入口| 久久久青草青青国产亚洲免观| 欧美一站二站| 在线观看日韩欧美| 欧美久久久久中文字幕| 9色国产精品| 久久久久久欧美| 99精品福利视频| 国产欧美精品久久| 玖玖综合伊人| 欧美伊人久久大香线蕉综合69| 蜜臀91精品一区二区三区| 中日韩美女免费视频网站在线观看| 亚洲欧美成人精品| 国产精品日韩在线观看| 欧美一区二区福利在线| 国产一区二区三区四区| 麻豆精品网站| 欧美在线播放| 亚洲一品av免费观看| 欧美激情一区二区三区四区| 亚洲女ⅴideoshd黑人| 亚洲国产精品www| 国产精品夜夜夜| 欧美日韩在线精品| 欧美日韩国产免费| 欧美a级大片| 欧美精品一区二区三| 暖暖成人免费视频| 久久黄金**| 欧美视频在线播放| 亚洲欧美视频在线观看视频| 黄网站免费久久| 国产精品网站在线观看| 欧美丰满高潮xxxx喷水动漫| 久久精品99国产精品| 国产伦精品一区二区三区视频黑人 | 国产精品伦一区| 亚洲国产欧美久久| 亚洲国内在线| 欧美日韩国产一区二区三区| 9人人澡人人爽人人精品| 亚洲黄色高清| 亚洲在线1234| 欧美国产欧美亚洲国产日韩mv天天看完整 | 99riav国产精品| 在线视频欧美日韩精品| 亚洲午夜在线观看视频在线| 亚洲永久在线| 亚洲国产另类久久精品| 亚洲制服丝袜在线| 欧美激情精品久久久| 国产精品永久免费| 亚洲精品一区二区在线观看| 欧美一区二区三区播放老司机| 免费av成人在线| 亚洲国产影院| 快she精品国产999| 韩日精品视频一区| 免费久久99精品国产自| 亚洲欧美日韩爽爽影院| 欧美精品www| 亚洲精品久久久久| 蜜臀久久99精品久久久久久9| 亚洲精品在线免费观看视频| 久久亚洲春色中文字幕久久久| 国产欧美亚洲精品| 亚洲欧美日韩一区在线观看| 欧美激情亚洲精品| 欧美国产高潮xxxx1819| 1769国产精品| 亚洲精品乱码久久久久| 欧美日韩亚洲一区二区三区在线| 亚洲精品视频二区| 99精品视频一区| 欧美日韩精品在线视频| 一本色道久久精品| 亚洲一区观看| 一区二区三区在线观看视频| 免费观看成人| 欧美母乳在线| 午夜精品三级视频福利| 午夜激情综合网| 亚洲日韩中文字幕在线播放| 99国产精品私拍| 欧美日本在线视频| 欧美一区二区三区电影在线观看| 欧美一区日本一区韩国一区| 玉米视频成人免费看| 日韩视频在线播放| 国产在线观看精品一区二区三区| 欧美好骚综合网| 狠狠色丁香久久综合频道| 亚洲另类视频| 亚洲人成啪啪网站| 久久久国产亚洲精品| 亚洲欧美日韩国产中文在线| 免费欧美高清视频| 麻豆国产精品777777在线| 国产精品萝li| 一区二区三欧美| 亚洲美女在线观看| 久久伊人亚洲| 欧美大片91| 亚洲激情一区二区| 久久久精品动漫| 久久夜色精品国产欧美乱极品| 国产精品久久久久久av福利软件| 亚洲国产精品尤物yw在线观看| 韩国av一区二区三区| 久久久国产一区二区| 猫咪成人在线观看| 亚洲第一主播视频| 欧美激情亚洲一区| 夜夜嗨av一区二区三区四区|