-7

I've this code i wrote that sets the array to 0

int arr[4];
memset(arr, 0, sizeof (arr));

Very simple, but how the code works without any errors even though sizeof(arr) = 16 (4 the array size * 4 for int) and the size i used when i declared the array is 4, How memset sets 16 bits to zero and the array i passed as a parameter has the size of 4?

I used memset(arr, 0, sizeof(arr)/sizeof(*arr)); to get the real size of the array and the result was accurate and it gives me 4 but how the above code works correctly?

  • Read the documentation of the function properly before asking a question on SO. – nishantsingh Sep 23 '16 at 09:46
  • why do you use a function from the last century to fiddle around with memory in C++? – 463035818_is_not_an_ai Sep 23 '16 at 09:46
  • 2
    "How memset sets 16 bits to zero" it sets 16 _bytes_ to zero, assuming `sizeof(arr)` == 16. – davmac Sep 23 '16 at 09:48
  • 4
    Use `C` tag for this question rather than `C++` because this in fact is not `C++` at all. But this question is a legit beginner question, I do not understand the minus votes. Reading the docs always helps but if everybody reads the docs really carefully, there would probably not need to be any stackoverflow. – HiFile.app - best file manager Sep 23 '16 at 09:57
  • Possible duplicate of [Using memset for integer array in c](http://stackoverflow.com/questions/17288859/using-memset-for-integer-array-in-c) – Garf365 Sep 23 '16 at 10:01

3 Answers3

1

memset sets 16 bytes (not bits) to 0. This is correct because the size of your array is 16 bytes, as you correctly stated (4 integers x 4 bytes per integer). sizeof knows the number of elements in your array and it knows the size of each element. As you can see in the docs, the third argument of memset takes the number of bytes, not the number of elements. http://www.cplusplus.com/reference/cstring/memset/

But be careful with using sizeof() where you pass array as int x[] or int* x. For example the following piece of code will not do what you expect:

void foo(int arr[]) {
  auto s = sizeof(arr); // be careful! this won't do what you expect! it will return the size of pointer to array, not the size of the array itself
  ...
}

int a[10];
foo(a);
0

The third parameter is number of bytes. Which is 4*4=16 for your case.

memset

Ming
  • 358
  • 1
  • 10
0

Actually the first solution is the correct one.

The function memset takes as third parameter the number of bytes to set to zero.

num: Number of bytes to be set to the value.

sizeof returns the number of bytes occupied by the expression.

In your case sizeof(arr) = 16 which is exactly yhe number of bytes requested by memset function.


Your second solution:

memset(arr, 0, sizeof(arr)/sizeof(*arr));  // Note that: sizeof(arr)/sizeof(*arr) == 16 / 4 (generally) == 4 bytes

will set only the first 4 bytes to zero, that is the first integer of the array. So that solution is wrong if your intent is to set each element of the array to zero.

BiagioF
  • 9,368
  • 2
  • 26
  • 50