C ‘memset’ Examples

Have you ever created an array and manually set its values? Imagine doing that for an array of size 1000…madness! This is where memset comes in. Memset is used to set an arbitrary array size with a value specified by you. Perhaps you have a multi-dimensional array used for a chess board mapping, initially you require the array to be filled with ‘\0′ characters, if you have a very large array it would not make sense to do this manually even creating a for-loop to fill the values does not make sense. Memset is also useful for zeroing out a C structure that you may have just malloc‘ed as well, just to ensure its contents are as they should be.

The memset function is quite basic.

// Header
#include <string.h>
 
// Definition
void *memset(void *s, int c, size_t n);

The memset function takes three arguments. The first argument is a pointer (notice the void *) to whatever data type you are planning to set, the second argument is the specific value you are going to set your data with; there is however a catch in that memset will convert your integer value into an unsigned char when writing the data. The third argument is how many of those specific values are needed.

Well that sounded like mumbo jumbo. I always find concrete examples more useful…sooo..without further ado:

Memset An Integer Array

#include <string.h>
#include <stdio.h>
 
#define DATA_SIZE 10
 
int main(int argc, char **argv)
{
  int i, data[DATA_SIZE];
 
  memset(data,0,sizeof(data));
 
  // Print out array
  for(i = 0; i < DATA_SIZE; i++)
          fprintf(stdout, "data[%u]: %u\n",i,data[i]);
 
  return 0;
}
----- Our Output -----
$ ./memset_t 
data[0]: 0
data[1]: 0
data[2]: 0
data[3]: 0
data[4]: 0
data[5]: 0
data[6]: 0
data[7]: 0
data[8]: 0
data[9]: 0

Lets look at my memset invocation (line 10 above). The first argument is my ‘data’ array that I created, my second argument is ’0′ as I am zeroing out my array, and the third argument is the size of my data (size of an integer (4 bytes) x 10 of them), so 40 bytes of data. Make sure to use the sizeof macro, especially if you are dealing with longs as they differ on a 32bit versus a 64bit machine.

The memset function is mostly used to clear data before use, that being said, you can overwrite anything you want really. If you have a string with a set of characters, you can use memset to overwrite them.

Memset A String

#include <stdio.h>
#include <string.h>
 
int main(int argc, char **argv)
{
  char data_str[] = "The wheels on the bus go round and round.";
  memset (data_str,'*',10);
 
  fprintf(stdout, "%s\n",data_str);
  return 0;
}
----- Our Output -----
$ ./memset_t 
********** on the bus go round and round.

In this example I used the * character as my value, and replaced the first 10 bytes with stars, hence the output.

If you have a C structure you would like to zero out, remember a structure is simply variables set in a line in memory (usually byte aligned), so we can use memset for that as well, for example:

Memset A C Structure

struct ifreq ifr;
 
memset(&ifr, 0, sizeof(ifr));  // clear ifreq structure

Just remember to use the sizeof macro ensuring you have covered the correct size of memory when using memset. Now go memset stuff!