Initialization of an integer array using memset and int value - fails

I am writing host code for the CUDA program, so I am stuck using the standard C functions. I am having trouble initializing the elements of an integer array using the memset function. I was impressed that you could use memset to initialize an integer array, for example, for all four of these:

 int num_elements = 10; int* array_example = (int*)malloc(num_elements * sizeof(int)); memset(array_example, 4, sizeof(array_example)); 

But when I do this, it sets every byte, not every int , to 4. If I say:

 memset(array_example, 4, 1); 

I get 4 in the first whole, and if I say:

 memset(array_example, 4, 2); 

I get 1024 in the first integer and 0 in the second. I understand that the memset function sets the number of bytes specified in the third parameter to 4, but is there a way to use memset to set each integer to 4 instead of each byte? Otherwise, I am stuck using a for loop? My GPU has low processing power, so I don’t have access to some of the nicer CUDA add-ons, which allow me to use C ++ more.

+4
source share
4 answers

If you know the size of the table and want to set each element to a specific 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 it like this:

 int array[10] = {[0 ... 9] = 4}; 

When you need to dynamically allocate an array, I doubt there is an alternative solution just using a simple loop.

+4
source

There is no standard alternative to memset that writes integers. You need to write a loop.

+2
source

memset can be used successfully (not a matter of luck) if the bits representing the int value have a pattern that is constant byte by byte, given the representation of int itself (for example, a 2-pad).

For example, if you are a memset array with 4, it turns out that each int is initialized to 0x04040404 (given sizeof (int) = 32), which may be okay or independent of your needs.

This refers to a number of specific initialization value values ​​for an integer.

But this leads to barely portable code.

It should always work if you are going to initialize every int at zero.

+2
source

Just add an option and add a few points:

  • If you are working in C ++ rather than C, try using std::fill() , which is generic, and let the compiler worry about optimization:

     std::fill_n(my_array, array_length, constant_value); 
  • Signature memset() :

     void *memset(void *s, int c, size_t n); 

    while it supposedly accepts int , it actually expects the value (unsigned) of a byte (i.e. between 0 and 0xFF ).

  • The tool’s ongoing response is some useful memset'ing that you can use that include using (unsigned) ints, set arrays to 0 or to UINT_MAX , i.e. up to 0xFFFF or 0xFFFFFFFF , etc., depending on sizeof(unsigned) .

  • If we strided memset() , you could use four of these two to get the 4-byte ints set to the array. However, we do not do this, and in fact it seems that at present there is no advantage for this, just looping.

+2
source

All Articles