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

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>
            亚洲激情女人| 在线精品国产成人综合| 欧美成人资源| 亚洲一区二区综合| 99国产一区二区三精品乱码| 日韩视频在线永久播放| 国产精品99久久久久久久久久久久| 正在播放日韩| 欧美在线你懂的| 免费日韩一区二区| 99国产欧美久久久精品| 亚洲在线一区二区三区| 久久影视精品| 欧美性猛交99久久久久99按摩| 国产一区再线| 一区二区三区.www| 久久久噜噜噜久久| 国产精品一区二区男女羞羞无遮挡| 欧美亚州一区二区三区| 国产自产2019最新不卡| 一区二区久久| 六月婷婷久久| 亚洲图片在线观看| 欧美国产激情| 一区视频在线播放| 亚洲一区在线观看视频| 欧美成人午夜77777| 在线亚洲一区二区| 欧美国产精品专区| 黄色在线成人| 亚洲欧美综合网| 亚洲人成人一区二区在线观看| 性做久久久久久久免费看| 欧美精品一区在线观看| 在线观看成人一级片| 亚洲欧洲av一区二区| 亚洲欧洲综合另类在线| 久久久噜噜噜| 国内精品亚洲| 久久aⅴ乱码一区二区三区| 亚洲精品一线二线三线无人区| 久久久精品午夜少妇| 国产日韩欧美一区在线 | 欧美在线日韩在线| 欧美日在线观看| 欧美天堂在线观看| 亚洲国产高清一区二区三区| 久久精品三级| 亚洲一区二区四区| 欧美日韩一区二区免费视频| 亚洲日本中文字幕| 欧美岛国在线观看| 久久综合给合| 亚洲电影免费| 欧美国产日韩一二三区| 99在线热播精品免费99热| 欧美黄免费看| 国产欧美日韩在线观看| 欧美/亚洲一区| 欧美专区一区二区三区| 国产色爱av资源综合区| 欧美怡红院视频| 亚洲自拍三区| 国产日本亚洲高清| 久久精品二区三区| 久久爱91午夜羞羞| 国产一区二区三区电影在线观看| 欧美在线影院| 久久国产直播| 亚洲人成亚洲人成在线观看图片| 亚洲黄色片网站| 欧美久久99| 一区二区三区高清在线| 在线中文字幕不卡| 国内久久视频| 亚洲激情第一页| 欧美性大战久久久久久久| 久久精品国产清自在天天线| 久久久午夜视频| 99视频在线精品国自产拍免费观看| 日韩视频免费在线观看| 国产欧美精品一区aⅴ影院| 久热国产精品| 欧美日韩第一区日日骚| 欧美在线一级视频| 免费久久精品视频| 亚洲一区免费看| 久久久精彩视频| 亚洲色图自拍| 久久精品主播| 亚洲一区在线看| 久久综合网hezyo| 中文在线一区| 美女成人午夜| 欧美在线视频导航| 美女视频网站黄色亚洲| 欧美一级片在线播放| 免费在线观看精品| 欧美中文字幕| 欧美日韩精品在线视频| 久久综合亚州| 国产精品日本欧美一区二区三区| 久久亚洲精品视频| 国产精品第一区| 亚洲第一区在线观看| 国产亚洲精品bt天堂精选| av不卡免费看| 亚洲精品一区二区三区福利| 久久精品网址| 欧美亚洲一级| 欧美四级在线| 亚洲人成亚洲人成在线观看| 激情另类综合| 欧美怡红院视频| 香港久久久电影| 欧美日一区二区在线观看 | 国产精品久久久久久久一区探花| 国产精品尤物| 亚洲一区国产一区| 欧美成人免费在线| 久久激情久久| 国产精品日韩欧美一区| 亚洲日韩欧美视频| 亚洲国产精品尤物yw在线观看| 午夜电影亚洲| 欧美日韩亚洲一区| 欧美国产欧美综合| 好吊视频一区二区三区四区 | 亚洲国产精品va| 久久精品成人一区二区三区| 亚洲综合首页| 欧美日韩在线综合| 一区二区三区精品久久久| 日韩视频一区二区在线观看| 美日韩精品视频| 欧美高清在线视频| 尤物99国产成人精品视频| 欧美一区不卡| 久久久综合网| 悠悠资源网亚洲青| 狂野欧美激情性xxxx欧美| 狼人天天伊人久久| 亚洲国产精品精华液2区45| 蜜臀99久久精品久久久久久软件 | 一区二区激情视频| 欧美日韩国产综合网| 日韩一级片网址| 亚洲欧美日韩成人| 国产日韩欧美一区二区三区四区| 午夜亚洲性色福利视频| 老司机免费视频一区二区| 一区免费视频| 欧美精品www在线观看| 亚洲欧美日韩久久精品 | 一区二区三区你懂的| 亚洲欧美在线高清| 国产午夜精品美女视频明星a级| 欧美在线视频观看免费网站| 浪潮色综合久久天堂| 亚洲精品视频一区二区三区| 欧美日韩国产麻豆| 午夜精品视频在线观看| 欧美国产视频在线| 亚洲一区在线视频| 一区二区三区我不卡| 欧美日韩你懂的| 久久爱另类一区二区小说| 亚洲啪啪91| 久久久夜色精品亚洲| 一区二区三区蜜桃网| 狠狠色综合日日| 欧美视频日韩视频在线观看| 久久成人免费视频| 99视频精品免费观看| 美女图片一区二区| 亚洲欧美日韩综合一区| 亚洲人成在线免费观看| 国产日韩视频| 欧美特黄视频| 欧美大片91| 亚洲久久一区| 久久免费视频网站| 亚洲夜间福利| 亚洲一区二区三区四区视频| 亚洲国产成人91精品| 久久激情网站| 国产一区二区三区视频在线观看| 这里只有精品丝袜| 91久久久久久久久| 欧美精品一区二区三区在线播放 | 亚洲无线一线二线三线区别av| 狂野欧美一区| 亚洲电影有码| 香蕉av777xxx色综合一区| 亚洲乱码国产乱码精品精天堂| 国产精品影院在线观看| 欧美高清视频一区二区三区在线观看 | 欧美精品久久久久久久久久| 久久综合伊人77777| 久久gogo国模裸体人体|