SUBJECT:   Memory °ü·Ã ÇÔ¼öµé

Description :

 o memcpy()

   => copy characters between buffers (¹®ÀÚ¿­ º¹»ç)

        #include 

        char      *memcpy(dest,source,cnt);
        char      *dest;
        char      *source;
        in     t  cnt;       --> ¹®ÀÚ¿­ÀÇ ¼ö

   => memcpy()´Â 'source'¿¡¼­ 'dest'·Î 'cnt'¸¸Å­ÀÇ ¹®ÀÚ¸¦ º¹»çÇÑ´Ù.   
      ¸¸ÀÏ 'dest'¿Í 'source'ÀÇ ÀϺκÐÀÌ °ãÄ¡¸é º¹»ç¸¦ ÇÏ´Â Áß¿¡ 'source'ÀÇ 
      ³»¿ëÀ» ÀÒ¾î¹ö¸± ¼ö Àֱ⠶§¹®¿¡ memcpy()´Â 'source'¸¦ °ãÄ¡Áö ¾Êµµ·Ï 
      ÇØ¾ß ÇÑ´Ù.

   <°á°ú> 'dest'ÀÇ Æ÷ÀÎÅÍ

   ¢Í ¿¹Á¦ 
        #include 
       
        char *buffr1="haco";
        char *buffr2="ura";
        main()
        {
          memcpy(buffr1,buffr2,4);
          printf("%s\n",buffr1);
        }
     
        hyundai2% a.out
                  ura

 o memset()

   => Initialize buffer (¹öÆÛ¸¦ ÃʱâÈ­)  

        #include 
 
        char    *memset(dest,ch,cnt);
        char    *dest;
        int     ch;
        int     cnt;

   => memset()Àº ¹öÆÛ 'dest'ÀÇ ³»¿ëÀ» óÀ½ºÎÅÍ 'cnt'±îÁö¿¡ 'ch'ÀÇ °ªÀ» ½á³Ö´Â´Ù.

   <°á°ú> 'dest'ÀÇ Æ÷ÀÎÅÍ

    ¢Í ¿¹Á¦
         #include                      #include 

         char buff[6]="sikgi";                   char buff[6]="sikgi"; 
         main()                                  main()
         {                                       {
           printf("%s...1\n", buff);                printf("%s...1\n",buff);
           memset(buff,'\k',6);                     memset(buff,'\0',6);
           printf("%s...2\n", buff);                printf("%s...2\n",buff);
         }                                       }

        hyundai2% a.out                          hyundai2% a.out
                  sikgi...1                                sikgi...1 
                  kkkkkk...2                               ...2

        hyundai2% a.out > test1                  hyundai2% a.out > test2

        hyundai2% ljsdump test1
        0000 : 73 69 6b 67 69 2e 2e 2e 31 0a 6b 6b 6b 6b 6b 6b sikgi...1.kkkkkk
        0010 : 2e 2e 2e 32 0a                                  ...2.

        hyundai2% ljsdump test2                 
        0000 : 73 69 6b 67 69 2e 2e 2e 31 0a 2e 2e 2e 32 0a    sikgi...1....2.

 o memcmp()

   => compare characters from two buffers (¹®ÀÚ¿­ ºñ±³)

       #include 
      
       int  memcmp(str1,str2,cnt);
       char   *str1;        ---> ù¹ø° ¹öÆÛ
       char   *str2;        ---> µÎ¹ø° ¹öÆÛ 
       int    cnt;          ---> ¹®ÀÚ¿­ÀÇ ¼ö

   => memcmp()´Â 'cnt'¸¸Å­ÀÇ ¹®ÀÚ¸¦ 'str1'°ú 'str2'¿¡¼­ ºñ±³ÇÑ´Ù.
      (¿©±â¿¡¼­ 'a'´Â 'b'º¸´Ù ÀÛ°í, 'b'´Â 'b'¿Í °°°í, 'c'´Â 'b'º¸´Ù Å©´Ù.)

   <°á°ú>  µÎ°³ ¹öÆÛ »çÀÌÀÇ °ü°è¸¦ °¡¸®Å°´Â °ª

            -------------------------------------
                °á   °ú          | µ¹¾Æ¿À´Â °ª
            -------------------------------------
              str1 <  str2        |    < 0
              str1 == str2        |     0
              str1 > str2         |    > 0
            -------------------------------------
   <ÁÖÀÇ>  ´ë¼Ò ¹®ÀÚ´Â ±¸º°µÈ´Ù. Áï, 'a'´Â 'A'¿Í °°Áö ¾Ê´Ù.
              
    ¢Í ¿¹Á¦
       #include 

       char str1[11]="skiyaki";
       char str2[11]="nobadayaki";
       int  comp;
       
       main()
       {
          comp=memcmp(str1,str2,10);
          printf("%d\n", comp);
       }

       hyundai2% a.out
                 5   --> 's' - 'n' = 115 - 110 

Revision History

ÀÛ¼ºÀÏÀÚ : 96.10.11
ÀÛ¼ºÀÚ : ÀÌÁø¼ö

¼öÁ¤ÀÏÀÚ :
¼öÁ¤ÀÚ :