C Programming

How Memset Function is Used

In C, the memset() function is used to set a one-byte value to a memory block byte by byte. This function is useful for initialization of a memory block byte by byte by a particular value. In this article, we will see in detail how this function can be used. So, let’s get started.

Header File:

1
string.h

Syntax:

1
void *memset(void *str, int ch, size_t n)

This function sets the first bytes of the memory block pointed by str by ch.

Arguments:

The function takes 3 arguments:

  1. str: This is the pointer of the memory location where the memory will be set. This is a void pointer, so we can set any type of memory block, but the memory will be set byte by byte.
  2. ch: This is the value that is to be copied to the memory block. This is an integer value, but it is converted to an unsigned character before copied.
  3. n: This is the number of bytes in the memory block which is set.

Return values:

memset() returns the first address of the memory block from where it starts to set the value.

Examples:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//Example1.c
#include<stdio.h>
#include<string.h>

int main()  
{  
    char str[30] = "ABCD EFGH";  
     
    printf("Before memset => %s",str);  
     
    memset(str,'x',3);  
     
    printf("\nAfter memset => %s\n",str);  
     
    return 0;  
}


In Example1.c, we have declared one character array of size 30. Then we have initialized it with the string “ABCD EFGH.” In the memset function, we have passed 3 arguments str, ‘x’ and 3. So, the memory block pointed by str will be reset the first 3 characters by ‘x.’ After memset, when we print the memory, we will get “xxxD EFGH.”

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//Example2.c
#include<stdio.h>
#include<string.h>

int main()  
{  
    char str[30] = "ABCD EFGH";  
     
    printf("Before memset => %s",str);  
     
    memset(str+4,'x',3);  
     
    printf("\nAfter memset => %s\n",str);  
     
    return 0;  
}


In Example2.c, we have passed str+4 to memset function. So, it reset the memory after the 4th location of str.  After memset, when we print the memory, we will get “ABCDxxxGH.”

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// Example3.c
#include<stdio.h>
#include<string.h>

int main()  
{  
    int arr[5],i;  
     
    memset(arr,10,5*sizeof(arr[0]));  
     
    printf("\narr Elements => \n");  
     
    for(i=0;i<5;i++)  
        printf("%d\t",arr[i]);  
     
    printf("\n");
    return 0;
}


In Example3.c, we have declared an integer array of size 5 and trying to initialize it by 10. But from the output, we have seen that the array is not initialized by 10; instead, we have got the value “168430090”. This is because the integer value is greater than one byte and the memset function converts the value to an unsigned character before copied. Now, we will see how we will get the value “168430090”.


The binary representation of 10 is 00000000 00000000 00000000 00001010.

When integer converted to unsigned char, the lower 1 byte is considered. So, when 10 is converted to unsigned char, it’s a binary representation is 00001010.

memset() function sets the memory location byte by byte. So, a total of 4 bytes will be: 00001010 00001010 00001010 00001010.

The decimal value of the binary representation of 4 bytes is 168430090.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// Example4.c
#include<stdio.h>
#include<string.h>

int main()  
{  
    int arr[5],i;  
     
    memset(arr,0,5*sizeof(arr[0]));  
     
    printf("\narr Elements => \n");  
     
    for(i=0;i<5;i++)  
        printf("%d\t",arr[i]);  
     
    printf("\n");
    return 0;
}


In Example4.c, we have initialized the integer array by 0. All bits of the binary representation of 0 is 0. So the array is initialized by 0.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// Example5.c
#include<stdio.h>
#include<string.h>

int main()  
{  
    int arr[5],i;  
     
    memset(arr,-1,5*sizeof(arr[0]));  
     
    printf("\narr Elements => \n");  
     
    for(i=0;i<5;i++)  
        printf("%d\t",arr[i]);  
     
     printf("\n");
     return 0;
 
}


In Example5.c, we have initialized the integer array by 0. All bits of the binary representation of -1 is 1. So the array is initialized by -1.

Conclusion:

In this article, we have seen using the memset function how we can initialize or set the value of a memory block efficiently. We can set any character and 0 or -1 as an integer value to a memory block. Memset function is faster to set a large chunk of contiguous memory in comparison with simply setting the location using a loop.

About the author

Bamdeb Ghosh

Bamdeb Ghosh

Bamdeb Ghosh is having hands-on experience in Wireless networking domain.He's an expert in Wireshark capture analysis on Wireless or Wired Networking along with knowledge of Android, Bluetooth, Linux commands and python. Follow his site: wifisharks.com