Aug 31, 2016

C Programming #85: Memory functions

C Programming #85: Memory functions

Following article will try to put together memory functions just like how string related functions were covered. Memory functions which are covered are memcmp, memcpy, memmove, memset, memchr. Note all the above function are part of strings.h library. Each of above function is covered with an example.


memcmp

Below is prototype of memcmp :

/* memcmp is similar to strcmp, except that bytes 
   equal to 0 are not treated as comparison terminators.*/
int memcmp(const void *ptr1, const void *ptr2, size_t len);

Example for usage is as follows

#include <stdio.h>
int main()
{
   char a[10] = { 2, 3, 4, 5 };
   char b[10] = { 2, 3, 4, 6 };

   if(memcmp(a, b, 3) == 0) {
      printf("First 3 items are same\n");
   }else {
      printf("First 3 items are not same\n");
   }

   if(memcmp(a, b, 4) == 0) {
      printf("First 4 items are same\n");
   }else {
      printf("First 4 items are not same\n");
   }
   return 0;
}
First 3 items are same
First 4 items are not same

memcpy

Below is prototype of memcpy :

/* memcpy copies len characters from src to dst 
   and returns the original value of dst
   The result of memcpy is undefined if src and 
   dst point to overlapping areas of memory */
void *memcpy(void *dst, const void *src, size_t len);

Example for usage is as follows

#include <stdio.h>
#include <strings.h>
int main()
{
   char a[4] = { 1, 2, 3 };
   char b[4] = { 0, 0, 0 };
   int i;

   printf("b Before memcpy: ");
   for(i = 0; i < 3; i++) {
      printf("%d, ", b[i]);
   }
   printf("\n");

   memcpy(b, a, 3);

   printf("b After memcpy: ");
   for(i = 0; i < 3; i++) {
      printf("%d, ", b[i]);
   }
   printf("\n");

   return 0;
}
b Before memcpy: 0, 0, 0, 
b After memcpy: 1, 2, 3,

memmove

Below is prototype of memmove :

/* memmove is just like memcpy except that 
   memmove is guaranteed to work even 
   if the memory areas overlap */
void *memmove(void *dst, const void *src, size_t len);

memmove takes care of overlap which can be demonstated by following example

#include <stdio.h>
#include <strings.h>
int main()
{
   char a[5] = { 1, 2, 3 };
   char b[5] = { 0, 0, 1, 2, 3 };
   int i;

   printf("a Before memmove: ");
   for(i = 0; i < 5; i++) {
      printf("%d, ", a[i]);
   }
   printf("\n");

   memmove(a + 2, a, 3);

   printf("a After memmove: ");
   for(i = 0; i < 5; i++) {
      printf("%d, ", a[i]);
   }
   printf("\n");

   printf("b Before memmove: ");
   for(i = 0; i < 5; i++) {
      printf("%d, ", b[i]);
   }
   printf("\n");

   memmove(b, b + 2, 3);

   printf("b After memmove: ");
   for(i = 0; i < 5; i++) {
      printf("%d, ", b[i]);
   }
   printf("\n");

   return 0;
}
a Before memmove: 1, 2, 3, 0, 0, 
a After memmove: 1, 2, 1, 2, 3, 
b Before memmove: 0, 0, 1, 2, 3, 
b After memmove: 1, 2, 3, 2, 3,

memset

Below is prototype of memset :

/* memset sets the first len bytes of the memory area 
   pointed to by ptr to the value specified by byteval */
void *memset(void *ptr, int byteval, size_t len);

Example for memset is

#include <stdio.h>
#include <strings.h>
int main()
{
   char a[4] = { 1, 2, 3, 4};
   int i;

   printf("a Before memset: ");
   for(i = 0; i < 4; i++) {
      printf("%d, ", a[i]);
   }
   printf("\n");

   memset(a, '\0', 4);

   printf("a After memset: ");
   for(i = 0; i < 4; i++) {
      printf("%d, ", a[i]);
   }
   printf("\n");

   return 0;
}
a Before memset: 1, 2, 3, 4, 
a After memset: 0, 0, 0, 0,

memchr

Below is prototype of memchr :

/* memchr finds the first occurence of ch in ptr 
   and returns a pointer to it (or a null pointer 
   if ch was not found in the first len bytes */
void *memchr(const void *ptr, int ch, size_t len);

Example that demonstates memchr is as follows

#include <stdio.h>
#include <strings.h>
int main()
{
   char a[4] = {11, 34, 81, 23};
   char *p, i;

   printf("A is: ");
   for(i = 0; i < 4; i++) {
       printf("%d, ", a[i]);
   }
   printf("\n");

   p = memchr(a, 81, 4);

   printf("Pointer of element 81 is %p\n", p);
   printf("Position of element 81 is %d\n", p - a);


   return 0;
}
A is: 11, 34, 81, 23, 
Pointer of element 81 is 0x7ffc82526ab2
Position of element 81 is 2

No comments :

Post a Comment