Table of contents
1.
Introduction
2.
Memset in 2D Array
3.
Frequently Asked Questions
3.1.
What is macros?
3.2.
Is memset and malloc the same? 
3.3.
What is a possible replacement of memset in c++?
4.
Conclusion
Last Updated: Mar 27, 2024
Easy

Memset in C++

Author Urwashi Priya
0 upvote

Introduction

memset() function is used to fill a block of memory with a particular value either 0 or -1. It sets the destination value with the given value. Before copying the character, it first converts the signed character into the unsigned character. This function generally increases the readability of the program.

MEMSET IN C++

It includes “cstring” header file.

Syntax:

memset(variable name, values, sizeof(variable name));

 

Now, if we write the below statement.

int a[10]={0};
cout<<a[5];
You can also try this code with Online C++ Compiler
Run Code

 

Our output would be 0 as a[10]={0} initialises all 10 places by 0.

But instead of above code segment, if we write,

int a[10]={5};
cout<<a[5];
You can also try this code with Online C++ Compiler
Run Code

 

The result would again be 0 as this form of initialisation is valid only in case of 0.

Next code segment,

int n;
cin>>n;
int a[n]=0;
cout<<a[5];
You can also try this code with Online C++ Compiler
Run Code

 

Our output would result in compilation error because initialisation of some variable n is not allowed.

To solve these problems, we brought memset.

int n;
cin>>n;
int a[n];
memset(a, 10, sizeof(a));
cout<<a[2];
You can also try this code with Online C++ Compiler
Run Code

 

This segments output some garbage value. This is because it is clearly specified that the value must be assigned only with 0 or -1. In case of string, everything can get replaced.

int n;
cin>>n;
int a[10];
memset(a, -1, sizeof(a));
cout<<a[2];
You can also try this code with Online C++ Compiler
Run Code


The output would be -1.

In string data type,

#include <cstring>
#include <iostream>
using namespace std;
 
int main()
{
    char str[] = "codingninja";
    memset(str, 't', sizeof(str));
    cout << str;
    return 0;
}
You can also try this code with Online C++ Compiler
Run Code

 

Output:
ttttttttttt

Also see, Fibonacci Series in C++, Abstract Data Types in C++

Memset in 2D Array

int arr[10][20] = {0};
memset(arr, 0, sizeof arr);
You can also try this code with Online C++ Compiler
Run Code

 

All values of the matrix get replaced by 0.

In case of dynamic array,

//initialisng the dynamic array
int *arr = malloc((10*20) * (sizeof *arr)); 
//copying value.
memset(arr, 0, (10*20*) * (sizeof *arr));
You can also try this code with Online C++ Compiler
Run Code

 

Till now, I assume you must have got a basic idea about memset.

You can also do a free certification of Basics of C++

Frequently Asked Questions

What is macros?

macros are defined by #deine directive. It replaces the name of macro by value of macro. 

Ex: # define a 10

a is the name of macro and wherever a exists in the program it is replaced by 10.

Is memset and malloc the same? 

No, memset is used to set the bytes in a block of memory whereas malloc is used to allocate a block of memory.

What is a possible replacement of memset in c++?

std:fill algorithm is a possible replacement for the sae purpose in c++.

Conclusion

This article taught us about memset. 

We hope you could easily take away all critical and conceptual techniques by walking over the given examples. 

Also Read - C++ Interview Questions

Refer to our guided paths on Coding Ninjas Studio to upskill yourself in Data Structures and AlgorithmsCompetitive ProgrammingJavaScriptSystem Design, and many more! If you want to test your competency in coding, you may check out the mock test series and participate in the contests hosted on Coding Ninjas Studio! But if you have just started your learning process and looking for questions asked by tech giants like Amazon, Microsoft, Uber, etc; you must have a look at the problemsinterview experiences, and interview bundle for placement preparations.

Nevertheless, you may consider our paid courses to give your career an edge over others!

Do upvote our blogs if you find them helpful and engaging!

Happy Learning!

 

Live masterclass