In the C++ programming language, memset is a function used to fill memory blocks.
- Initially, it converts the value of ' ch ' to the unsigned character. Here 'ch' refers to the character to be filled with another value passed in the memset function.
- After then it copies the character 'ch' into each of the first 'n' characters of the objeccpp tutorialed by the st .
- Here the 'n' is referred to as the size of the block, which is mentioned in the memset , and it must be equal or smaller with the size of the objeccpp tutorialed by st .
- If the value of n is greater than the size of the objeccpp tutorialed by the st , it will generate error hence undefined.
- If sometimes, a case arises where the object is not copyable, then also it will generate an error, and the behavior of the function is the same as in the previous case, i.e., undefined .
- In the C++ programming language, the memset function is present in the < cstring > header file; without mentioning this header file, you would not be able to access the use of the memset function.
- Here, the object which is not copy-able is as follows: array, C-compatible struct, scalar, and so on; hence the behavior of memset function is undefined in this case.
- The only difference between the memset function and the memcpy function is that in memset function not only copies the value but replace it with the other substitute, e.g., if we want to replace each character of a particular string-like, ' cool ', with the alphabet ' f ', then; as a result, and it would be looking at final is; ' ffff '. But in memcpy function, it only copies the value from one place to another or copies a block of content from a particular place and puts it on another block of content.
Syntax of memset function:
void* memset ( void* st , int ch , size_t n ) ;
Now, let us discuss the terminologies which are used here.
- st : st represents the pointer to the memory required to be filled. It is a pointer to the object, where the character ' ch ' has been copied. As per the programmer's choice, they can change the name from st to some other, depending on their need and work.
- ch: ' ch ' here refers to the character , which is needed to be filled. It also varies from programmer to programmer choice, and they can rename it accordingly. It is converted to the unsigned char, whenever required and also it is converted into int format, but int acceptable values are only 0 and -1.
- n: It denotes the size of the block, or can say it the numeric value which specifies the number of times the character ' ch ' needed to be copied and converted into the value passed in the memset function.
- Increase readability: The memset function in C++ is mainly used to convert every character of the whole string into a particular int value which is passed as an input into the memset function. It is a one-line code ; hence very short and ultimately increases the readability.
- Reduce line of code: Instead of using unnecessary use of loops to assign and convert the value of each character present in the string with the int value, which is only passed as an input in this memset function, and the same task has been performed easily as compared with the lengthy method.
- It is more Faster: Using the memset function is faster to convert each character of the given string into the value passed through an input, maybe of int type or any other, depending on the programmer. Its working is very fast rather than applying loops and while statements for performing the same task.
- Useful in getting rid of Misalignment Problem: The memset function in C++ helps the programmer to get rid of misalignment problem. Sometimes, the case occurs where you find that you are dealing with the problem of misalignment of data in the processor, which leads to the error in the program. In this case, the memset and memcpy functions in C++ are the ultimate solutions of it.
Advantages of memset function of C++
Implementation of memset in C++
As previously discussed, the functionality of the memset function in C++ has been explored. To delve deeper into this topic, let's enhance our understanding through an illustrative example.
Example 1:
// Program to implement memset ( ) function in C++
#include <iostream>
#include <cstring> // header file that contains the memset ( ) function, without it // we cannot able to access the use of memset ( ) function
using namespace std ;
int main()
{
char st [ ] = " JavaCppTutorial " ;
char ch ;
cout << " Printing the given string : " << st << endl ;
cout << " Enter an alphabet from which you want to replace the each charater of the given string : " ;
cin >> ch ;
memset ( st , ch , sizeof ( st ) ) ;
cout << " Printing the string after replacing the each character of the string with the given substitute : " << endl ;
cout << " " << st ;
return 0 ;
}
The output of the above program:
In the previous example, we observed the transformation of individual characters in the string ' JavaCppTutorial ' into a single alphabet specified by the user. It's important to note that spaces are treated as individual characters in this process.
Example 2:
// Program to implement memset ( ) function in C++
#include < iostream >
#include < cstring > // header file that contains the memset ( ) function, without it //we cannot able to access the use of memset ( ) function
using namespace std ;
int main()
{
int arr [100] ;
int n , i , p ;
cout << " Enter size of an integer array: " ;
cin >> n ;
cout << " Enter an array elements: " << endl ;
for ( i = 0 ; i < n ; i++ )
{
cin >> arr [ i ] ;
}
cout << " Printing the given array : " << endl ;
for ( i = 0 ; i < n ; i++ )
{
cout << " " << arr [ i ] << endl ;
}
cout << " Enter an integer value from which you want to replace the each charater of the given string : " ;
cin >> p ;
memset ( arr , p , sizeof( arr ) ) ;
cout << " Printing the array after replacing the each value of the array with the given substitute : " << endl ;
for ( i = 0 ; i < n ; i++ )
{
cout << " " << arr [ i ] << endl ;
}
return 0 ;
}
The output of the above program:
In the previous illustration, we observed the transformation of each character in the 'JavaCppTutorials' string into an integer value specified by the user. It's important to note that even spaces are treated as individual characters in this process. The only valid integer values considered are 0 and -1. Using any other integer value may lead to unpredictable results, effectively replacing all string characters with garbage values if a different integer, such as 2, is provided.