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

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


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


導航

<2025年12月>
30123456
78910111213
14151617181920
21222324252627
28293031123
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>
            国产农村妇女精品一区二区| 国产一区二区黄| 在线观看视频一区二区| 欧美伊人久久久久久午夜久久久久 | 国产精品成人一区二区艾草| 亚洲香蕉视频| 亚洲一级在线观看| 国产一区二区三区久久久| 久久久精品网| 欧美jjzz| 亚洲一区国产精品| 午夜精品国产| 亚洲人成在线观看一区二区| 亚洲免费电影在线| 国产精品羞羞答答xxdd| 久久久久久久欧美精品| 久久在精品线影院精品国产| 一区二区三区导航| 午夜精品理论片| 亚洲国产精品第一区二区| 亚洲精品欧美| 国产一区观看| 亚洲精品欧美极品| 国产无一区二区| 亚洲黄页视频免费观看| 欧美午夜不卡在线观看免费 | 亚洲一二三四区| 激情综合中文娱乐网| 91久久精品日日躁夜夜躁国产| 欧美视频在线一区| 六月婷婷久久| 国产精品看片资源| 欧美顶级艳妇交换群宴| 国产伦精品一区二区三区视频孕妇| 久久一区二区精品| 欧美日韩视频在线一区二区| 老**午夜毛片一区二区三区| 欧美午夜激情视频| 欧美成人一区二区三区在线观看| 欧美午夜www高清视频| 欧美成人福利视频| 国产精品女主播在线观看| 欧美激情视频一区二区三区在线播放 | 久久全球大尺度高清视频| 一区二区三区国产在线观看| 久久综合网络一区二区| 欧美淫片网站| 久久久久国产精品人| 欧美国产一区视频在线观看| 久久精品亚洲精品| 欧美午夜宅男影院| 欧美高清成人| 精品成人在线观看| 午夜国产精品视频免费体验区| 日韩一区二区福利| 久久福利一区| 久久精品国产96久久久香蕉| 欧美日韩亚洲网| 91久久极品少妇xxxxⅹ软件| 韩国av一区二区三区| 亚洲女同同性videoxma| 亚洲网站视频| 欧美日韩一区二区在线观看视频| 欧美激情在线免费观看| 伊人久久噜噜噜躁狠狠躁| 欧美中文字幕不卡| 久久国产一区二区| 国产欧美一区在线| 亚洲女性裸体视频| 亚洲女人天堂av| 国产精品福利影院| 亚洲一区二区三| 亚洲欧美制服另类日韩| 国产精品v日韩精品v欧美精品网站| 亚洲人成绝费网站色www| 亚洲人成在线观看网站高清| 猛干欧美女孩| 亚洲丶国产丶欧美一区二区三区| 最新亚洲视频| 欧美精品一区二区三区一线天视频 | 亚洲视频在线一区| 国产精品久久国产精品99gif| 亚洲一区免费观看| 久久精品一区中文字幕| 精品69视频一区二区三区| 久久狠狠婷婷| 欧美大片免费久久精品三p | 国产精品xxxav免费视频| 在线综合亚洲欧美在线视频| 午夜免费电影一区在线观看| 国产欧美一区二区精品忘忧草| 欧美一区深夜视频| 免费在线观看日韩欧美| 亚洲久色影视| 国产精品美女久久久久av超清| 欧美在线3区| 亚洲国产精品成人精品| 亚洲午夜激情免费视频| 国产女人18毛片水18精品| 久久久久久久综合| 亚洲国产欧美一区| 亚洲男人的天堂在线| 激情偷拍久久| 欧美伦理91| 欧美一区二区啪啪| 亚洲黄色在线| 久久av红桃一区二区小说| 1024成人网色www| 国产精品v片在线观看不卡 | 亚洲精品午夜精品| 欧美在线免费一级片| 亚洲国产成人不卡| 欧美午夜视频在线| 久久一区亚洲| 亚洲欧美一区二区激情| 亚洲国产精品高清久久久| 欧美一进一出视频| 亚洲精品综合| 好吊视频一区二区三区四区| 欧美日韩国产123区| 久久嫩草精品久久久精品| 一本在线高清不卡dvd| 欧美aaa级| 欧美一区综合| 亚洲一区一卡| 一本色道久久综合亚洲精品按摩| 国产午夜精品久久| 国产精品久久久久久久久动漫| 老妇喷水一区二区三区| 午夜日韩激情| 亚洲一区二区动漫| 日韩性生活视频| 亚洲欧洲综合| 欧美大片专区| 欧美gay视频| 久久久久久久综合色一本| 午夜精品久久久久| 亚洲视频综合在线| 在线视频欧美一区| 99在线|亚洲一区二区| 亚洲国产影院| 亚洲激情午夜| 亚洲欧洲免费视频| 曰韩精品一区二区| 尤物yw午夜国产精品视频| 国产婷婷色一区二区三区在线| 国产精品主播| 国产视频精品网| 国产手机视频一区二区| 国产视频亚洲精品| 国产精品综合不卡av| 国产精品实拍| 国产色产综合产在线视频| 国产九九视频一区二区三区| 国产精自产拍久久久久久| 国产午夜精品视频免费不卡69堂| 国产欧美日韩一区二区三区在线| 国产精品久久久久aaaa九色| 国产乱码精品一区二区三区忘忧草 | 亚洲网在线观看| 亚洲欧美日韩国产成人| 亚洲女人天堂成人av在线| 欧美一级免费视频| 久久久www成人免费毛片麻豆| 久久国产精品黑丝| 久久婷婷久久| 欧美激情1区2区3区| 亚洲精品麻豆| 亚洲一区二区成人| 久久精品成人欧美大片古装| 美女精品国产| 欧美午夜不卡在线观看免费 | 欧美日韩一区二区免费视频| 欧美性猛交一区二区三区精品| 国产精品人人做人人爽人人添| 国产精品欧美久久| 国内精品美女av在线播放| 亚洲黄色三级| 亚洲一区bb| 老司机67194精品线观看| 亚洲国产美女久久久久| 一区二区三区日韩欧美| 亚洲欧美综合网| 欧美成人dvd在线视频| 国产精品男女猛烈高潮激情| 影音先锋成人资源站| 亚洲少妇在线| 久久在线免费观看| 亚洲裸体俱乐部裸体舞表演av| 亚洲综合电影| 欧美成人性网| 国产日本欧美一区二区三区| 亚洲国产精品一区二区www在线 | 亚洲一区二区视频在线观看| 久久国产精品99国产| 亚洲日本va午夜在线电影| 欧美在线亚洲在线| 欧美日韩免费一区| 精品二区久久| 羞羞漫画18久久大片|