Tuesday, January 3, 2012

memmove() vs memcpy() vs strcpy()

Describe the use of following functions

1.memset()
2.memcmp()
3.memmove()
4.memcpy()
5.memchr()

Also expain memmove() vs memcpy()

memset():
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.


strcpy() vs memcpy()
The strcpy() function is designed to work exclusively with strings. It copies each byte of the source string to the destination string and stops when the terminating null character (\0) has been moved. On the other hand, the memcpy() function is designed to work with any type of data.Because not all data ends with a null character, you must provide the memcpy() function with the number of bytes you want to copy from the source to the destination. The following program shows examples of both the strcpy() and the memcpy() functions:

#include <stdio.h>
#include <string.h>
typedef struct cust_str {
     int  id;
     char last_name[20];
     char first_name[15];
} CUSTREC;
void main(void);
void main(void)
{
     char*   src_string = “This is the source string”;
     char    dest_string[50];
     CUSTREC src_cust;
     CUSTREC dest_cust;
     printf(“Hello!  I’m going to copy src_string into dest_string!\n”);

/* Copy src_string into dest_string. Notice that the destination
   string is the first argument. Notice also that the strcpy()
   function returns a pointer to the destination string. */

printf(“Done! dest_string is: %s\n”,
       strcpy(dest_string, src_string));
printf(“Encore! Let’s copy one CUSTREC to another.\n”);
printf(“I’ll copy src_cust into dest_cust.\n”);

/* First, initialize the src_cust data members. */

src_cust.id = 1;
strcpy(src_cust.last_name, “Strahan”);
strcpy(src_cust.first_name, “Troy”);

/* Now, use the memcpy() function to copy the src_cust structure to
   the dest_cust structure. Notice that, just as with strcpy(), the
   destination comes first. */

memcpy(&dest_cust, &src_cust, sizeof(CUSTREC));
 printf(“Done! I just copied customer number #%d (%s %s).”,
               dest_cust.id, dest_cust.first_name, dest_cust.last_name);
}
When dealing with strings, you generally should use the strcpy() function, because it is easier to use with strings. When dealing with abstract data other than strings (such as structures), you should use the memcpy() function.

memcmp(): 

int memcmp ( const void * ptr1, const void * ptr2, size_t num );
 
Compares the first num bytes of the block of memory pointed by ptr1 to the first num bytes pointed by ptr2, returning zero if they all match or a value different from zero representing which is greater if they do not.
Returns an integral value indicating the relationship between the content of the memory blocks:
A zero value indicates that the contents of both memory blocks are equal.
A value greater than zero indicates that the first byte that does not match in both memory blocks has a greater value in ptr1 than in ptr2 as if evaluated as unsigned char values; And a value less than zero indicates the opposite.

memchr():
const void * memchr ( const void * ptr, int value, size_t num );
      void * memchr (       void * ptr, int value, size_t num );
Locate character in block of memory
Searches within the first num bytes of the block of memory pointed by ptr for the first occurrence of value (interpreted as an unsigned char), and returns a pointer to it.

1 comment:

  1. http://www.java-samples.com/showtutorial.php?tutorialid=591

    ReplyDelete