How to use memset() in c++
If you are trying to reset a data structure to a default value, there are two main functions that people might use, memset() and std::fill(). Both are similar in terms of when one might use them but can differ greatly in what goes on under the hood and how each one should be used.
In this article, we will focus on the memset() method.
In this article, we will focus on the memset() method.
As shown above, the first method, memset(), is a method brought over from the C language. The first parameter, void * ptr, takes in a pointer to a block of memory. This memory location is the location to fill with the int value parameter. In order for the function to know how much memory to fill, starting from where ptr starts
Let's first look at the str array and how memset() is used here.
In the first part of this example, we have a character array, called str, that gets changed using memset(). The first 15 chars in the array get altered with the dash, '-', character. This is done with the line of code memset(str, '-', 15).
This changes the character array from "don't you dare change me!" to "---------------change me!"
The first 15 char values in the array were changed to the character value set in the function memset().
Now let's take a look at the integer array.
We have something that looks very crazy, memset(numberArray, 0, arrLen * sizeof(int))
What in the world is going on here?!?! Let's break it down.
First, we created an array called numberArray that held 5 values {1, 2, 3, 4, 5}
Then, we tried to fill the array with the number 85, so why did we get the number 1431655765 for each value in the array?!?
Let's go into the details of the array being used. The numberArray array holds integers, integers in C++ take up 4 bytes of memory (8bits = 1byte)
This means that the number 1, in the numberArray is actually:
00000000 00000000 00000000 00000001
when stored in memory.
What the memset() function does is reset bytes of memory...meaning that a single int value will get the number 85 placed in each byte!
The number 85 in binary is 01010101, that value is placed in each of the 4 bytes of the integer, it creates the new number:
01010101 01010101 01010101 01010101
This value as a decimal is 1431655765 which is what gets printed out!
This replacement happens for each byte in the entire numberArray, as specified by the size parameter of arrLen * sizeof(int)
The arrLen variable is 5, to account for each element in the array.
The sizeof(int) function returns the memory size that c++ specifies an integer value to be, which is 4 bytes!
In the first example, the str array was made up of char variables. Each char variable is 1 byte in length which is why memset() worked so well on that array. memset() was made to alter bytes of memory so be mindful of that if used on bigger elements such as an integer or a double.
There you have it! How to use memset(). This is a powerful function that can be used in a wide variety of situations.
If you want to manipulate each byte in an array, this is a great tool to do just that. But as said before, be careful! Always be mindful of the memory you are trying to erase and keep your array bounds accounted for.
Happy Coding!
In the first part of this example, we have a character array, called str, that gets changed using memset(). The first 15 chars in the array get altered with the dash, '-', character. This is done with the line of code memset(str, '-', 15).
This changes the character array from "don't you dare change me!" to "---------------change me!"
The first 15 char values in the array were changed to the character value set in the function memset().
Now let's take a look at the integer array.
We have something that looks very crazy, memset(numberArray, 0, arrLen * sizeof(int))
What in the world is going on here?!?! Let's break it down.
First, we created an array called numberArray that held 5 values {1, 2, 3, 4, 5}
Then, we tried to fill the array with the number 85, so why did we get the number 1431655765 for each value in the array?!?
Let's go into the details of the array being used. The numberArray array holds integers, integers in C++ take up 4 bytes of memory (8bits = 1byte)
This means that the number 1, in the numberArray is actually:
00000000 00000000 00000000 00000001
when stored in memory.
What the memset() function does is reset bytes of memory...meaning that a single int value will get the number 85 placed in each byte!
The number 85 in binary is 01010101, that value is placed in each of the 4 bytes of the integer, it creates the new number:
01010101 01010101 01010101 01010101
This value as a decimal is 1431655765 which is what gets printed out!
This replacement happens for each byte in the entire numberArray, as specified by the size parameter of arrLen * sizeof(int)
The arrLen variable is 5, to account for each element in the array.
The sizeof(int) function returns the memory size that c++ specifies an integer value to be, which is 4 bytes!
In the first example, the str array was made up of char variables. Each char variable is 1 byte in length which is why memset() worked so well on that array. memset() was made to alter bytes of memory so be mindful of that if used on bigger elements such as an integer or a double.
There you have it! How to use memset(). This is a powerful function that can be used in a wide variety of situations.
If you want to manipulate each byte in an array, this is a great tool to do just that. But as said before, be careful! Always be mindful of the memory you are trying to erase and keep your array bounds accounted for.
Happy Coding!