Questions tagged [memset]

memset is a C standard library function that sets the first N bytes of the block of memory to the specified value (interpreted as an unsigned char)

502 questions
186
votes
9 answers

Why use bzero over memset?

In a Systems Programming class I took this previous semester, we had to implement a basic client/server in C. When initializing the structs, like sock_addr_in, or char buffers (that we used to send data back and forth between client and server) the…
PseudoPsyche
  • 4,332
  • 5
  • 37
  • 58
124
votes
7 answers

Reset C int array to zero : the fastest way?

Assuming that we have a T myarray[100] with T = int, unsigned int, long long int or unsigned long long int, what is the fastest way to reset all its content to zero (not only for initialization but to reset the content several times in my program)?…
Vincent
  • 57,703
  • 61
  • 205
  • 388
104
votes
17 answers

What is the equivalent of memset in C#?

I need to fill a byte[] with a single non-zero value. How can I do this in C# without looping through each byte in the array? Update: The comments seem to have split this into two questions - Is there a Framework method to fill a byte[] that might…
Jedidja
  • 16,610
  • 17
  • 73
  • 112
102
votes
13 answers

Fastest way to zero out a 2d array in C?

I want to repeatedly zero a large 2d array in C. This is what I do at the moment: // Array of size n * m, where n may not equal m for(j = 0; j < n; j++) { for(i = 0; i < m; i++) { array[i][j] = 0; } } I've tried using…
Eddy
  • 6,661
  • 21
  • 58
  • 71
92
votes
4 answers

What's the use of memset() return value?

memset() is declared to return void* that is always the same value as the address passed into the function. What's the use of the return value? Why does it not return void?
sharptooth
  • 167,383
  • 100
  • 513
  • 979
71
votes
2 answers

Why is std::fill(0) slower than std::fill(1)?

I have observed on a system that std::fill on a large std::vector was significantly and consistently slower when setting a constant value 0 compared to a constant value 1 or a dynamic value: 5.8 GiB/s vs 7.5 GiB/s However, the results are…
Zulan
  • 21,896
  • 6
  • 49
  • 109
66
votes
2 answers

How memset initializes an array of integers by -1?

The manpage says about memset: #include void *memset(void *s, int c, size_t n) The memset() function fills the first n bytes of the memory area pointed to by s with the constant byte c. It is obvious that memset can't be used to…
haccks
  • 104,019
  • 25
  • 176
  • 264
65
votes
3 answers

Why does memset take an int instead of a char?

Why does memset take an int as the second argument instead of a char, whereas wmemset takes a wchar_t instead of something like long or long long?
user541686
  • 205,094
  • 128
  • 528
  • 886
61
votes
7 answers

Is memset() more efficient than for loop in C?

Is memset() more efficient than for loop. Considering this code: char x[500]; memset(x,0,sizeof(x)); And this one: char x[500]; for(int i = 0 ; i < 500 ; i ++) x[i] = 0; Which one is more efficient and why? Is there any special instruction in…
David
  • 4,634
  • 7
  • 35
  • 42
54
votes
10 answers

Using memset for integer array 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);
user1762571
  • 1,888
  • 7
  • 28
  • 47
50
votes
6 answers

Why does "memset(arr, -1, sizeof(arr)/sizeof(int))" not clear an integer array to -1?

Is it not possible to use memset on an array of integers? I tried the following memset call and didn't get the correct integer values in the int array. int arr[5]; memset (arr, -1, sizeof(arr)/sizeof(int)); Values I got are: arr[0] = -1 arr[1] =…
Ravi Gupta
  • 6,258
  • 17
  • 56
  • 79
43
votes
11 answers

Should C++ programmer avoid memset?

I heard a saying that c++ programmers should avoid memset, class ArrInit { //! int a[1024] = { 0 }; int a[1024]; public: ArrInit() { memset(a, 0, 1024 * sizeof(int)); } }; so considering the code above,if you do not use memset,how…
Jichao
  • 40,341
  • 47
  • 125
  • 198
42
votes
7 answers

Is it legal to use memset(…, 0, …) on an array of doubles?

Is it legal to zero the memory of an array of doubles (using memset(…, 0, …)) or struct containing doubles? The question implies two different things: From the point of view of C standard: Is this undefined behavior of not? (On any particular…
Andrei
  • 8,606
  • 10
  • 35
  • 43
37
votes
2 answers

How to provide an implementation of memcpy

I am trying to write some bare metal code with a memset-style loop in it: for (int i = 0; i < N; ++i) { arr[i] = 0; } It is compiled with GCC and GCC is smart enough to turn that into a call to memset(). Unfortunately because it's bare metal I…
Timmmm
  • 88,195
  • 71
  • 364
  • 509
36
votes
2 answers

Is there analog of memset in go?

In C++ I can initialize an array with some value using memset: const int MAX = 1000000; int is_prime[MAX] memset(is_prime, 1, sizeof(is_prime)) What memset does, crudely can be described as filling the array with some value, but doing this really…
Salvador Dali
  • 214,103
  • 147
  • 703
  • 753
1
2 3
33 34