Code360 powered by Coding Ninjas X Code360 powered by Coding Ninjas X
Table of contents
Memset in 2D Array
Frequently Asked Questions
What is macros?
Is memset and malloc the same? 
What is a possible replacement of memset in c++?
Last Updated: Mar 27, 2024

Memset in C++

Author Urwashi Priya
0 upvote
Master Python: Predicting weather forecasts
Ashwin Goyal
Product Manager @


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.


It includes “cstring” header file.


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


Now, if we write the below statement.

int a[10]={0};


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};


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

Next code segment,

int n;
int a[n]=0;


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;
int a[n];
memset(a, 10, sizeof(a));


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;
int a[10];
memset(a, -1, sizeof(a));

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;



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);


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));


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

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

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job

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++.


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!


Previous article
Compilers for C++
Next article
malloc() vs new
Live masterclass