How to use and Implement own memset in C

The memset function copies the value of c (converted to an unsigned char) into each of the first n characters of the object pointed to by s.

Syntax memcpy in C:

void *memset(void *s, int c, size_t n);

Parameters:

s— pointer to the destination object.

c— Value to be filled.

n — Number of bytes to be filled starting from s to be filled.

Return:

The memset function returns the value of s.

 

Note: s is a void pointer so that we can pass any type of pointer to this function.

Sometimes peoples require to create a custom memset function for their project. So here in the below section, I shall describe a method to create the memset() function or you can say that we will see the implementation of memset in C.

 

Example of memset in C,

Let’s see an example code to understand the usage of the memset() function in C.

#include <stdio.h>
#include <string.h>

int main()
{
    char str[50] = "Aticleworld is a programming Blog.";
    printf("\nBefore memset(): %s\n\n", str);

    // Fill 6 characters starting from str[11] with '*'
    memset(str + 11, '*', 6);

    printf("After memset(): %s\n\n", str);

    return 0;
}

Output:

memset in c

 

Explanation: (str + 11) points to first space (0 based index) of the string “Aticleworld is a programming Blog.”, and memset() sets the character ‘*’ starting from first ‘ ‘ (space) of the string up to 6 character positions of the given string and hence we get the output as shown above.

 

One thing you should remember that the memset performs copy operation byte by byte. Let’s see an example code to understand this concept.

#include <stdio.h>
#include <string.h>

#define SIZE_ARRAY 5

void printArray(int arr[], int n)
{
    int i =0;
    for (i=0; i<n; i++)
    {
        printf("0x%x\n", arr[i]);
    }
}

int main()
{
    int arr[SIZE_ARRAY];

    memset(arr,1, sizeof(arr));

    printArray(arr,SIZE_ARRAY);

    return 0;
}

Output:

memset implementation in c

 

Let’s see some important points before implementing your own memset function in C.

  • We can use the memset for any memory buffer either for structure, array, or any data type.
  • We must input the correct memory buffer size, otherwise, show undefined behavior.
  • As I have mentioned in the above program, the memset function works on byte. It is very important to know.
  • Before using the memset we must include string.h header file in our program.
  • The value which you want to copy will first convert in unsigned char.
  • The memory which you are passing in memset must be valid otherwise you will get undefined behavior.

 

How to write your own memset in C?

Your compiler/standard library will likely have a very efficient and tailored implementation of the memset() function. So if not require avoid to create own version of the memset function.

We can easily implement the memset() function in C programming. You need to typecast the given buffer memory to unsigned char*. After that typecasting the value with unsigned char, assigned the value to each byte of the buffer until n (given length).

 

void *my_memset(void *s, int c,  unsigned int len)
{
    unsigned char* p=s;
    while(len--)
    {
        *p++ = (unsigned char)c;
    }
    return s;
}

 

Driver program to test the implemented memset in C

In the below program we are assigning ‘0’ to each byte of the source buffer for a given length.

#include <stdio.h>
#include <string.h>

#define SIZE_ARRAY 5


//Own Created memset
void *my_memset(void *s, int c,  unsigned int len)
{
    unsigned char* p=s;
    while(len--)
    {
        *p++ = (unsigned char)c;
    }
    return s;
}

//function to print char of an char array
void printArray(char arr[], int n)
{
    int i =0;
    for (i=0; i<n; i++)
    {
        printf("%c\n", arr[i]);
    }
}

int main()
{
    char arr[SIZE_ARRAY] = "ABCD";

    // Print Array before calling memset
    printArray(arr,SIZE_ARRAY);

    //Calling own created memset
    my_memset(arr,'0', sizeof(arr));

    // Print Array After calling memset
    printArray(arr,SIZE_ARRAY);

    return 0;
}

Output:

Own created memset in C

 

 

Recommended Articles for you: