Chaitanya
Chaitanya

Reputation: 3469

Setting all the elements to a same value in C

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.

Upvotes: 1

Views: 1726

Answers (8)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726479

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.

Upvotes: 4

ChronoTrigger
ChronoTrigger

Reputation: 8607

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];
...

Upvotes: 3

Sohil Omer
Sohil Omer

Reputation: 1181

You can use memset() function

Example:

memset(<array-name>,<initialization-value>,<len>);

Upvotes: 1

Neil Kirk
Neil Kirk

Reputation: 21763

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

Upvotes: 5

Jeris
Jeris

Reputation: 2473

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

int arr[5]={0};

Upvotes: 0

msam
msam

Reputation: 4287

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

Upvotes: 0

Dariusz
Dariusz

Reputation: 22241

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.

Upvotes: 0

Arya
Arya

Reputation: 371

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

Upvotes: 1

Related Questions