• <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>

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


            只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


            導(dǎo)航

            <2010年6月>
            303112345
            6789101112
            13141516171819
            20212223242526
            27282930123
            45678910

            統(tǒng)計(jì)

            常用鏈接

            留言簿

            隨筆檔案

            搜索

            最新評(píng)論

            閱讀排行榜

            評(píng)論排行榜

            亚洲愉拍99热成人精品热久久| 久久精品亚洲欧美日韩久久| 奇米影视7777久久精品人人爽| 久久精品国产清自在天天线| 麻豆成人久久精品二区三区免费| 色噜噜狠狠先锋影音久久| 人人狠狠综合久久亚洲| 国产91色综合久久免费| 偷窥少妇久久久久久久久| 91精品无码久久久久久五月天| 少妇人妻综合久久中文字幕| 99久久伊人精品综合观看| 成人午夜精品无码区久久| 久久精品无码一区二区三区免费 | 久久精品黄AA片一区二区三区| 亚洲狠狠久久综合一区77777| 一本久久知道综合久久| 亚洲AV伊人久久青青草原| 99久久国产免费福利| 999久久久无码国产精品| 一本久久知道综合久久| 久久综合九色综合网站| 久久久久这里只有精品| 国产午夜精品理论片久久| 久久精品国产只有精品2020| 久久综合久久自在自线精品自| 欧美日韩精品久久久免费观看| 色婷婷噜噜久久国产精品12p| 国产免费福利体检区久久| 国产韩国精品一区二区三区久久| 色偷偷偷久久伊人大杳蕉| 青草国产精品久久久久久| 色欲综合久久中文字幕网| 久久综合香蕉国产蜜臀AV| 日韩乱码人妻无码中文字幕久久| 亚洲av成人无码久久精品| 色婷婷综合久久久久中文| 国产精品久久久久影视不卡| 久久久久综合网久久| 欧美亚洲国产精品久久蜜芽| 精品久久久久久无码人妻蜜桃|