Why is memset() incorrectly initializing int?
memset
sets each byte of the destination buffer to the specified value. On your system, an int
is four bytes, each of which is 5 after the call to memset
. Thus, grid[0]
has the value 0x05050505
(hexadecimal), which is 84215045
in decimal.
Some platforms provide alternative APIs to memset
that write wider patterns to the destination buffer; for example, on OS X or iOS, you could use:
int pattern = 5;
memset_pattern4(grid, &pattern, sizeof grid);
to get the behavior that you seem to expect. What platform are you targeting?
In C++, you should just use std::fill_n
:
std::fill_n(grid, 100, 5);
using memset to initialise the int array
memset sets individual bytes, not entire integers which, typically, span four bytes. If you look at the bits of the number 16843009, it is what you get with four bytes with the value 1. As Ed Heal said, don't complicate things by using memset. Use a loop instead.
Why isn't memset assigning 1?
Manpage :
#include <string.h>
void *memset(void *s, int c, size_t n)
The
memset()
function fills the firstn
bytes of the memory area pointed to bys
with the constant bytec
.
Therefore, memset
can't be used to initialize int
array with 1
because if int
is represented by 4 bytes, then it will initialize each bytes with 1
.16843009
is equivalent to 0x01010101
. Each of 4 bytes are initialized with 01
.
Using memset
, an array of int
can only be initialised with 0
or -1
because 0
and -1
both have all bits 0
and 1
respectively in two's complement binary representation regardless of the size of int
data type.
Initializing an integer array using memset and an int value - fails
If you know the size of a table and want to set each element to the certain value you can always write:
int array[10] = { 4, 4, 4, 4, 4, 4, 4, 4, 4, 4 };
If you use gcc you can also do this in this way:
int array[10] = {[0 ... 9] = 4};
When you have to dynamically allocate the array I doubt that there is any alternative solution then just using a simple loop.
Strange problem with memset, why is it zeroing my variable?
When you apply the sizeof
operator to an array, it gives you the size of the whole array in bytes, not the size of one element. Because you're multiplying this value by the number of array elements, you're writing far past the end of the array. This invokes undefined behavior.
You can properly clear each array as follows:
out_t *out[c-1];
memset(out, 0, sizeof(out));
int Ilosc[c-1];
memset(Ilosc, 0, sizeof(Ilosc));
Or better still:
out_t *out[c-1] = { NULL };
int Ilosc[c-1] = { 0 };
This will initialize all array elements to NULL or 0 without having to call memset
.
Why memset works wrong in apple clang compiler?
memset
converts the second operand (argument) into unsigned char
and copies it into all destination bytes. In your case, this resulting unsigned char is 0xFF
, which copied into all elements of type int
gives their values -1.
Documentation link: https://en.cppreference.com/w/cpp/string/byte/memset.
Recommendation: Don't use low-level "C" functions like memset
unless you are very sure of what you are doing and that you really need it. You don't need to set array values in a loop, we have algorithms in C++, such as std::fill
suggested in comments. You might also want to use std::array
instead of the plain "C" array.
A more C++-like and much more readable alternative:
std::array<int, SIZE> arr2;
std::fill(std::begin(arr2), std::end(arr2), INF);
Related Topics
Accessing Environment Variables in C++
How to Create an Array When the Size Is a Variable Not a Constant
Why Doesn't My Template Accept an Initializer List
C++ Qt Signal and Slot Not Firing
Explain C++ Sfinae to a Non-C++ Programmer
Copy an Cv::Mat Inside a Roi of Another One
Dll Redirection Using Manifests
Undefined Reference to '_Gxx_Personality_Sj0'
Standard Library Containers with Additional Optional Template Parameters
Why Does This Delay-Loop Start to Run Faster After Several Iterations with No Sleep
What Is the Proper Use of the Comma Operator
C++ Return Value Created Before or After Auto Var Destruction
Does Boost Have a Datatype for Set Operations That Is Simpler Than the Stl