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

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>
            久久裸体视频| 欧美大片免费久久精品三p | 国产又爽又黄的激情精品视频| 亚洲欧美日韩第一区| 亚洲一区二区三区免费视频| 国产精品日本一区二区| 欧美在线观看你懂的| 欧美在线日韩在线| 国内外成人在线| 欧美不卡高清| 欧美日韩精品一区二区三区| 亚洲天堂免费在线观看视频| 午夜精品久久久久久久男人的天堂 | 欧美福利视频网站| 欧美激情国产日韩| 亚洲欧美国产日韩中文字幕 | 欧美成人精品1314www| 免费成人高清在线视频| 亚洲欧美日韩综合aⅴ视频| 亚洲欧美中文日韩v在线观看| 极品尤物av久久免费看| 亚洲精品欧美激情| 国产日本欧美一区二区三区| 欧美h视频在线| 国产精品成人在线观看| 欧美a级理论片| 国产精品日韩一区| 亚洲第一伊人| 国产欧美午夜| 最新国产乱人伦偷精品免费网站| 国产精品一区二区你懂得| 欧美刺激性大交免费视频 | 久久精品一二三区| 亚洲社区在线观看| 久久久久一区二区| 亚洲欧美日韩网| 欧美成人一品| 久久久久久一区二区三区| 欧美激情bt| 欧美插天视频在线播放| 国产欧美一区二区三区另类精品| 亚洲欧洲精品一区二区三区波多野1战4| 国产欧美精品日韩精品| 亚洲精品小视频在线观看| 永久555www成人免费| 亚洲一区二区在线播放| 日韩亚洲欧美精品| 久色婷婷小香蕉久久| 久久不见久久见免费视频1| 欧美日精品一区视频| 亚洲高清电影| 在线日韩视频| 久久精品国产2020观看福利| 性欧美大战久久久久久久免费观看 | 午夜亚洲伦理| 午夜国产精品影院在线观看| 欧美日韩国产色视频| 欧美福利视频网站| 亚洲国产婷婷香蕉久久久久久| 欧美亚洲综合在线| 久久精品99久久香蕉国产色戒 | 久久久久久久综合狠狠综合| 久久国产主播| 国产性天天综合网| 亚洲在线成人| 香蕉亚洲视频| 国产女人精品视频| 欧美呦呦网站| 免费观看成人| 亚洲黄色成人| 欧美日韩国产区| 亚洲色无码播放| 午夜精品www| 国产日韩欧美高清免费| 欧美一级大片在线观看| 久久午夜视频| 在线成人激情视频| 欧美国内亚洲| 日韩视频三区| 欧美在线日韩精品| 国产综合欧美| 欧美岛国在线观看| 99视频精品| 久久精视频免费在线久久完整在线看| 国产欧美日韩视频一区二区三区| 欧美自拍偷拍午夜视频| 亚洲成在线观看| 一区二区欧美日韩| 国产欧美日韩在线视频| 久久欧美肥婆一二区| 亚洲欧洲一区二区三区久久| 亚洲一区二区三区午夜| 国产欧美日韩精品专区| 免费欧美电影| 亚洲婷婷综合色高清在线| 久久久国产精彩视频美女艺术照福利| 亚洲国产成人高清精品| 欧美日韩福利视频| 性欧美videos另类喷潮| 亚洲国产小视频在线观看| 亚洲欧美在线高清| 亚洲国产专区| 国产精品视频导航| 久久视频这里只有精品| 亚洲精品一区二区三| 久久精品国产精品亚洲综合| 亚洲精品日韩一| 国产欧美日韩不卡| 欧美精品久久99| 性色一区二区| 夜夜嗨av一区二区三区四区| 老司机免费视频久久| 亚洲欧美日韩国产一区二区| 亚洲黄色成人网| 狠狠88综合久久久久综合网| 欧美日韩一区二区在线观看视频| 欧美一区日韩一区| 亚洲性视频网站| 亚洲人妖在线| 亚洲第一精品电影| 久久天天躁夜夜躁狠狠躁2022 | 亚洲美女电影在线| 国产午夜精品理论片a级探花| 欧美日韩高清区| 久久综合给合| 久久精品国产综合| 午夜欧美精品久久久久久久| 国产精品99久久久久久久vr| 亚洲国产成人久久综合| 免费观看成人| 免费影视亚洲| 欧美 日韩 国产在线| 久久国产精品高清| 欧美一区二区三区在线播放| 午夜精品久久久| 亚洲自拍偷拍福利| 亚洲永久视频| 午夜精品亚洲| 午夜伦理片一区| 亚洲欧美视频一区| 亚洲欧美日韩电影| 性欧美8khd高清极品| 性感少妇一区| 久久精品国产成人| 久久米奇亚洲| 欧美成人高清视频| 欧美高清日韩| 欧美国产日韩在线观看| 欧美福利在线| 亚洲人成在线播放网站岛国| 亚洲久久在线| 亚洲午夜激情免费视频| 亚洲网站视频| 欧美永久精品| 蜜桃久久av| 欧美激情一区二区三区在线视频 | 亚洲一区二区视频| 午夜亚洲性色视频| 久久人人爽人人爽爽久久| 免费欧美日韩| 欧美三级欧美一级| 国产欧美婷婷中文| 亚洲第一天堂无码专区| 99在线精品视频在线观看| 亚洲永久免费av| 久久久精品性| 亚洲高清资源| 亚洲男人的天堂在线| 久久久久久穴| 欧美日韩国产综合在线| 国产欧美综合在线| 亚洲人线精品午夜| 亚洲欧美日韩精品久久| 久久九九精品| 亚洲精品在线观| 香港久久久电影| 欧美久久久久中文字幕| 国产精品自在欧美一区| 亚洲福利视频二区| 亚洲免费影视| 亚洲第一精品福利| 亚洲自拍电影| 欧美激情综合色| 国产视频一区二区在线观看| 亚洲人精品午夜在线观看| 欧美一级电影久久| 91久久视频| 久久久成人网| 国产精品视频网| 亚洲精品国久久99热| 久久精品综合| 在线视频精品| 欧美+日本+国产+在线a∨观看| 国产欧美日韩视频一区二区| 一本大道久久a久久综合婷婷| 久久久久久高潮国产精品视| aaa亚洲精品一二三区| 免费久久精品视频| 禁断一区二区三区在线| 午夜视频一区|