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

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 閱讀(581) 評論(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>
            亚洲手机成人高清视频| 午夜精品成人在线视频| 你懂的成人av| 久久亚洲不卡| 亚洲国产精品ⅴa在线观看| 欧美成人激情在线| 欧美激情一区二区三区蜜桃视频| 亚洲人成在线观看| 日韩视频在线免费| 国产精品一区一区| 麻豆精品传媒视频| 欧美精品免费播放| 午夜亚洲视频| 久久久久国产精品人| 亚洲伦理在线| 国产精品99久久久久久久vr| 国产主播精品在线| 亚洲成色www8888| 欧美日韩一级大片网址| 欧美一区二区精品在线| 久久精品亚洲热| 一本一本久久| 久久国产精品99精品国产| 亚洲人久久久| 欧美亚洲在线视频| av成人老司机| 欧美一区二区三区四区在线观看地址| 亚洲国产精品成人综合| 在线一区日本视频| 亚洲第一中文字幕| 亚洲综合丁香| 亚洲狼人精品一区二区三区| 亚洲男女自偷自拍图片另类| 亚洲国产日韩一级| 欧美一级午夜免费电影| 一本色道精品久久一区二区三区| 午夜伦理片一区| 在线亚洲+欧美+日本专区| 久久久久国产精品人| 亚洲一区综合| 欧美国产视频日韩| 免费观看在线综合| 国产亚洲精品aa| 日韩亚洲在线观看| 亚洲乱码久久| 免费在线亚洲欧美| 开心色5月久久精品| 国产乱码精品| 一区二区三区免费网站| 亚洲三级影院| 久久综合99re88久久爱| 久久久蜜桃精品| 国产精品一区二区男女羞羞无遮挡| 亚洲娇小video精品| 经典三级久久| 欧美一区不卡| 久久久精品国产免费观看同学| 欧美视频第二页| 亚洲三级电影在线观看 | 亚洲精品一品区二品区三品区| 国产综合自拍| 久久成人资源| 久久五月天婷婷| 国产综合18久久久久久| 欧美一区二区在线播放| 久久狠狠亚洲综合| 国产一区二区久久精品| 欧美亚洲日本国产| 久久久人成影片一区二区三区| 国产午夜精品美女视频明星a级 | 亚洲人成小说网站色在线| 亚洲国产精品激情在线观看| 久久精品免费观看| 农村妇女精品| 亚洲精品视频在线播放| 欧美激情国产高清| 亚洲美女av在线播放| 中文一区二区在线观看| 欧美性猛交xxxx乱大交蜜桃| 亚洲午夜电影在线观看| 欧美亚洲网站| 精品成人a区在线观看| 久久男人资源视频| 亚洲人www| 新67194成人永久网站| 国产一区二区无遮挡| 久久久999国产| 亚洲片区在线| 欧美在线国产精品| 激情成人亚洲| 欧美激情亚洲综合一区| 亚洲午夜久久久久久尤物| 久久国产精品99国产| 在线观看91久久久久久| 欧美另类一区二区三区| 亚洲欧美国产不卡| 美女视频黄免费的久久| 一二三四社区欧美黄| 国产亚洲一级| 欧美精品一区二区三区高清aⅴ| 一区二区三区视频在线观看| 久久婷婷国产综合国色天香| 亚洲人成在线观看| 国产欧美91| 欧美成人一区在线| 欧美一级久久久久久久大片| 欧美激情第9页| 久久成人免费电影| 99视频精品| 国自产拍偷拍福利精品免费一| 欧美国产日本| 久久精品亚洲国产奇米99| 亚洲老板91色精品久久| 久久这里只有| 亚洲欧美日韩精品久久亚洲区| 亚洲电影在线| 国产日韩欧美另类| 欧美日韩一区三区| 欧美91视频| 久久久久久亚洲精品不卡4k岛国| 一本色道久久综合亚洲精品按摩| 欧美激情影音先锋| 久久先锋影音av| 欧美一区1区三区3区公司| 亚洲精品一区二区三区av| 国产综合香蕉五月婷在线| 国产精品jizz在线观看美国 | 欧美日韩在线视频一区二区| 久久免费99精品久久久久久| 亚洲欧美日韩综合aⅴ视频| 亚洲理论电影网| 91久久中文字幕| 免费在线看一区| 久久字幕精品一区| 久久五月天婷婷| 久久久久久久高潮| 久久精品国产视频| 午夜亚洲伦理| 午夜视频一区二区| 性欧美大战久久久久久久久| 亚洲伊人伊色伊影伊综合网| 在线综合+亚洲+欧美中文字幕| 亚洲欧洲日产国码二区| 亚洲福利视频二区| 亚洲日韩中文字幕在线播放| 亚洲激情亚洲| 日韩亚洲精品电影| 一区二区三区日韩精品| 亚洲色诱最新| 亚洲专区免费| 欧美在线三区| 老司机凹凸av亚洲导航| 蜜乳av另类精品一区二区| 欧美多人爱爱视频网站| 欧美激情第9页| 亚洲区中文字幕| 一区二区三区色| 亚洲影视在线| 久久乐国产精品| 欧美大片一区二区三区| 欧美日韩亚洲激情| 国产日产欧美a一级在线| 国产一区视频网站| 亚洲激情一区| 亚洲综合精品一区二区| 久久精品亚洲一区二区| 欧美肥婆在线| 中文在线资源观看网站视频免费不卡| 亚洲一区二区三区高清| 久久精品理论片| 欧美大片免费观看| 国产精品乱码一区二区三区| 国产一本一道久久香蕉| 亚洲欧洲日本在线| 亚洲欧美久久| 麻豆成人小视频| 99国产精品视频免费观看| 午夜老司机精品| 欧美精品一区在线播放| 国产一区二区三区久久久久久久久| 在线播放亚洲| 亚洲欧美经典视频| 欧美激情精品久久久久久蜜臀| 在线视频日韩| 欧美.日韩.国产.一区.二区| 国产精品三级视频| 亚洲国产高潮在线观看| 午夜久久电影网| 亚洲国产一区二区三区a毛片| 亚洲图片激情小说| 欧美jizz19性欧美| 国产一区二区三区高清播放| 亚洲美女91| 久久免费少妇高潮久久精品99| 亚洲乱码视频| 久热精品视频在线| 国产午夜亚洲精品理论片色戒| 99re这里只有精品6| 美女脱光内衣内裤视频久久影院 | 久久国产黑丝|