Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using memset for integer array in C

Tags:

c

memset

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);
like image 382
user1762571 Avatar asked Jun 25 '13 03:06

user1762571


People also ask

How do you do memset on an array?

The syntax of memset() function is as follows : // ptr ==> Starting address of memory to be filled // x ==> Value to be filled // n ==> Number of bytes to be filled starting // from ptr to be filled void *memset(void *ptr, int x, size_t n);

Can memset be used in c?

Description. The memset() function sets the first count bytes of dest to the value c. The value of c is converted to an unsigned character.

Why does memset take an int?

memset predates (by quite a bit) the addition of function prototypes to C. Without a prototype, you can't pass a char to a function -- when/if you try, it'll be promoted to int when you pass it, and what the function receives is an int .

Is memset faster than for loop?

It depends. But, for sure memset is faster or equal to the for-loop. If you are uncertain of your environment or too lazy to test, take the safe route and go with memset.


4 Answers

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.
like image 147
Jonathon Reinhart Avatar answered Oct 22 '22 08:10

Jonathon Reinhart


Short answer, NO.

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

like image 25
vidit Avatar answered Oct 22 '22 08:10

vidit


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.

like image 6
ceilingcat Avatar answered Oct 22 '22 07:10

ceilingcat


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.

like image 5
mattn Avatar answered Oct 22 '22 09:10

mattn