Using memset for integer array in C

memset 2d array
c memset array
memset char array
memset in c
memset implementation
when to use memset
memset vector
memcpy in c
char str[] = "beautiful earth";
memset(str, '*', 6);
printf("%s", str);

Output:
******ful earth

Like the above use of memset, can we initialize only a few integer array index values to 1 as given below?

int arr[15];
memset(arr, 1, 6);

No, you cannot use memset() like this. The manpage says (emphasis mine):

The memset() function fills the first n bytes of the memory area pointed to by s with the constant byte c.

Since an int is usually 4 bytes, this won't cut it.


If you (incorrectly!!) try to do this:

int arr[15];
memset(arr, 1, 6*sizeof(int));    //wrong!

then the first 6 ints in the array will actually be set to 0x01010101 = 16843009.

The only time it's ever really acceptable to write over a "blob" of data with non-byte datatype(s), is memset(thing, 0, sizeof(thing)); to "zero-out" the whole struture/array. This works because NULL, 0x00000000, 0.0, are all completely zeros.


The solution is to use a for loop and set it yourself:

int arr[15];
int i;

for (i=0; i<6; ++i)    // Set the first 6 elements in the array
    arr[i] = 1;        // to the value 1.

memset() in C with examples, If the object is not trivially-copyable (e.g., scalar, array, or a C-compatible struct), void* memset( void* str, int ch, size_t n); Parameters str[] : Pointer to the object to We can use memset() to set all values as 0 or -1 for integral data types also. 1) Like above use of memset, can we initialize only few integer array index values to 1 as given below?? int arr[15]; memset(arr,1,6); Practice As Follows. No, you cannot use memset() like this. The manpage says (emphasis mine): The memset() function fills the first n bytes of the memory area pointed to by s with the constant byte c. Since an

Short answer, NO.

Long answer, memset sets bytes and works for characters because they are single bytes, but integers are not.

memset in C++, I dont think you can with memset. You can set each BYTE of that block of memory to 5 but not each element (which is made up of 4 bytes). memset-C++ Reference It operates one byte each time. So it works fine if you assign the second arguments with a int value no more than 0xff. As for your version, the third arguments is the number of array elements, so you got your output. Actually the truth is you are supposed to assign the third arguments the NUMBER OF BYTES you want.

The third argument of memset is byte size. So you should set total byte size of arr[15]

memset(arr, 1, sizeof(arr));

However probably, you should want to set value 1 to whole elements in arr. Then you've better to set in the loop.

for (i = 0; i < sizeof(arr)/sizeof(arr[0]); i++) {
    arr[i] = 1;
}

Because memset() set 1 in each bytes. So it's not your expected.

how to use memset for int?, Copying data using the memset() function in C. Note: Since an Integer is of 4 bytes, to set the first 2 elements of the array, we will pass n as 8, i.e. 4 * 2 = 8. For large arrays, like a couple MiB or something, it might make a bit of sense to maintain a pool of zeroed arrays. IDK if you're gaining much over letting the OS do that for you, though, and using calloc. For smaller arrays, memset is very cheap. If you're going to touch the array right after zeroing, you should probably do both in the same thread.

On Linux, OSX and other UNIX like operating systems where wchar_t is 32 bits and you can use wmemset() instead of memset().

#include<wchar.h>
...
int arr[15];
wmemset( arr, 1, 6 );

Note that wchar_t on MS-Windows is 16 bits so this trick may not work.

Copying data using the memset() function in C, In C, use can try fill by copy (use memcpy() repeatedly over memset won''t work on an array of integers because it sets every byte to the value  No, you cannot use memset() like this. The manpage says (emphasis mine): The memset() function fills the first n bytes of the memory area pointed to by s with the constant byte c. Since an int is usually 4 bytes, this won't cut it.

No, you can't [portably] use memset for that purpose, unless the desired target value is 0. memset treats the target memory region as an array of bytes, not an array of ints.

A fairly popular hack for filling a memory region with a repetitive pattern is actually based on memcpy. It critically relies on the expectation that memcpy copies data in forward direction

int arr[15];

arr[0] = 1;
memcpy(&arr[1], &arr[0], sizeof arr - sizeof *arr);

This is, of course, a pretty ugly hack, since the behavior of standard memcpy is undefined when the source and destination memory regions overlap. You can write your own version of memcpy though, making sure it copies data in forward direction, and use in the above fashion. But it is not really worth it. Just use a simple cycle to set the elements of your array to the desired value.

using memset to reset an integer array., MEMSET in C is a C standard library function that sets, or, more semantically, fills​, Computers store digital information in the form of bits and bytes, with one byte an integer, and a size_t (an unsigned integer type designed to hold any array  memset in C++ Converts the value ch to unsigned char and copies it into each of the first n characters of the object pointed to by str[]. If the object is not trivially-copyable (e.g., scalar, array, or a C-compatible struct), the behavior is undefined.

MEMSET in C, memset. C/C++ Reference const int ARRAY_LENGTH; char the_array[​ARRAY_LENGTH]; . Input size, Initialized with a for-loop, Initialized with memset(). The C library function void *memset (void *str, int c, size_t n) copies the character c (an unsigned char) to the first n characters of the string pointed to, by the argument str. Following is the declaration for memset () function. str − This is a pointer to the block of memory to fill. c − This is the value to be set.

memset, c − This is the value to be set. The value is passed as an int, but the function fills the block of memory using the unsigned char conversion of this value. n −  string.h memset() function with example: Here, we are going to learn about the memset() function of string.h in C/C++ language, which is used to fill a block of memory with the given value. A humble request Our website is made possible by displaying online advertisements to our visitors.

C library function - memset(), It depends … the most common suggestion is to use memset thus [code]int arr[​100]; memset(arr, 0, sizeof(arr)); [/code]If you have an array of integral scalar types  A lot of times, we can use memset() to zero initialize arrays. Often, the performance of memset() is much faster than similar methods like calloc(). The below example illustrates this point while comparing the running time of both memset() and calloc() on a Linux Machine, using the <time.h> header file.

Comments
  • possible duplicate of memset integer array?
  • Nope. memset() casts down to a byte and dupes it across the region.
  • You could always read the memset documentation to find out what it does.
  • @JoeFrambach The accepted answer to that question doesn't apply here.
  • Possible duplicate of Why does "memset(arr, -1, sizeof(arr)/sizeof(int))" not clear an integer array to -1?
  • In my system size of int is 4 bytes. So i tried memset(arr,1,8). But it is initializing the whole array with that 4 byte value instead of filling only first 8 bytes as per my third argument.
  • I would be more convinced by your claim memset did this if you start out by initializing the whole array to 0 (using int arr[15] = {0}) and then doing the memset(arr, 1, 8); And if you still have an array full of 0x01010101's after that, you have a buggy c runtime :-).
  • @Jonathon, I agree it's the wrong thing to do, but the claim is that memset is behaving in a way radically different from what it's supposed to do. I pointed out that probably is not the case and described a way to really check it.
  • @MikeWoolf Right. My comment was directed to the OP. Certainly he is free to code whatever he wishes, but if he's hoping to somehow trick memset into initializing his array, it's going to end poorly.
  • @briankip The array decays to a pointer here. The & is optional in this case; memset(&arr, ...) would do the same thing.
  • such a logically appealing answer!
  • But in my system size of int is 4 bytes. So i tried memset(arr,1,8). But it is initializing the whole array with that 4 byte value instead of filling only first 8 bytes as per my third argument
  • You must not use constant value 8, you've better to use count of element size. for example n * sizeof(int).
  • When calling memset(arr, 1, 4), it become 0x01 0x01 0x01 0x01. But your expected is 0x00 0x00 0x00 0x01. (This is depend on your CPU archtecture).