1

Note: There are posts similar to this for C++ only, I didn't find any useful post in regards to C.

I want to set the array of elements with the same value. Of course, this can be achieved simply using a for loop.

But, that consumes a lot of time. Because, in my algorithm this setting array with same value takes place many number of times. Is there any simple way to achieve this in C.

Chaitanya
  • 3,399
  • 6
  • 29
  • 47

8 Answers8

5

Use a for loop. Any decent compiler will optimize this as much as possible.

Neil Kirk
  • 21,327
  • 9
  • 53
  • 91
4

It is a near certainty that you wouldn't be able to improve substantially on the speed of your for loop. There is no magic way to set a value into multiple memory locations faster than it takes to store that value into these multiple memory locations. Regardless of whether you use the for loop or not, all the locations must be written to, which takes most of the time.

There is of course the void * memset ( void * ptr, int value, size_t num ); for values composed of identical bytes1, but under the hood it has a loop. Perhaps the implementation could be very smart about using that loop, but so can the optimizing compiler.

1 Although memset takes an int, it converts it to unsigned char before setting it into the memory region.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • Here, the value to set is obtained dynamically and is large enough. So, `memset` can be ruled out since it can be used for setting all elements to `unsigned char` only. – Chaitanya Oct 02 '13 at 08:06
3

As suggested by other users, use memset if you want to initiate your array with 0 values, but don't do it if the values are not that simple.

For more complicated values, you can have a constant copy of your initial values and copy them later with memcpy:

float original_values[100]; // don't modify these values
original_values[0] = 1.2f;
original_values[1] = 10.9f; 
...

float working_values[100]; // work with these values
memcpy(working_values, original_values, 100 * sizeof(float));
// do your task
working_values[0] *= working_values[1];
...
ChronoTrigger
  • 8,459
  • 1
  • 36
  • 57
1

You can use memset() . It fills no of bytes you want to fill with same byte value.Here you can read man page.

Arya
  • 371
  • 2
  • 17
1

You can use memset() function

Example:

memset(<array-name>,<initialization-value>,<len>);
Sohil Omer
  • 1,171
  • 7
  • 14
0

You can easily memset an array to 0.

If you want a different value, it all depends on the type used.

For char* arrays you can memset them to any value, since char is almost always one byte long.

For an array of structures, if all fields of a structure are to be initialized with 0 or NULL, you can memset it with 0.

You can not memset an array or array of structures to any value other than 0, because memset operates on single bytes. So if you memset an int[] with 1, you will not have an array of 1's.

To initialize an array of structures with a custom value, just fill one structure with the desired data and do an assignment it in a for. The compiler should do it relatively efficiently for you.

Dariusz
  • 21,561
  • 9
  • 74
  • 114
0

If you are talking about initialization see this question. If you want to set the values at a later time then use memset

Community
  • 1
  • 1
msam
  • 4,259
  • 3
  • 19
  • 32
0

Well you can only set your values to zero for a particular array. Here is an example

int arr[5]={0};
Jeris
  • 2,355
  • 4
  • 26
  • 38