7

Possible Duplicate:
How to initialize an array in C
initializing an array of ints

I wonder over the fastest/simplest way to initialize an int array to only contain -1 values. The array I need is 90 ints long so the straightforward way should be to initialize it like this:

int array[90]={-1, -1, -1, ...};

but I only want to use the array once so I want to be able to use it dynamically and be able to free it after using it in the program, so Im more looking for a fast way like calloc, but instead of zeros, -1 of course.

Community
  • 1
  • 1
patriques
  • 5,057
  • 8
  • 38
  • 48
  • Another one: http://stackoverflow.com/questions/201101/how-to-initialize-an-array-in-c – P.P Nov 21 '12 at 16:21

6 Answers6

11

If you are using gcc then use designated initializer

int array[90] = { [ 0 ... 89 ] = -1}

int array[90],i;
for(i = 0; i < 90 ; arr[i++] = -1);

To do this dynamically , you will have to allocate using malloc then you only free the memory, otherwise freeing the memory which is not allocated by malloc , calloc or realloc is undefined behavior.

Use this:

int *array;
array=malloc(sizeof(int)*n);
for(i=0;i<n;array[i++]=-1);
// After use free this
free(array);
Donald Duck
  • 8,409
  • 22
  • 75
  • 99
Omkant
  • 9,018
  • 8
  • 39
  • 59
  • 1
    Good tip, but I think you missed something? I.e __I want to be able to use it dynamically and be able to free it after__ – enhzflep Nov 21 '12 at 16:12
  • you can do it dynamically also – Omkant Nov 21 '12 at 16:14
  • 1
    Yes indeed, but you can't free `int array[90] = ....`. Also, memset is optimized to copy many bytes per cpu operation. The for loop needs 90 iterations, so unless the compiler unrolls the loop for you, then optimizes it further you'll take longer if you 'roll-your-own'. I'll find a reference if you like. – enhzflep Nov 21 '12 at 16:21
  • @enhzflep: see my edit for dynamic array – Omkant Nov 21 '12 at 16:22
  • 1
    memset only works to set 0, -1 and all the other numbers where the 4bytes in the integer are identical, but doesn't work as a general solution. – LtWorf Nov 21 '12 at 16:29
  • @LtWorf : I havn't mentioned `memset ` so what's your point ? – Omkant Nov 21 '12 at 16:31
  • Well, bugger me sideways - I'd confused memset with memmove or something else, perhaps strcpy, I forget. Anyway, using the high performance counter shows the time taken for 100 million elements to be equal for a memset call and a loop. You post gets my vote. :) – enhzflep Nov 21 '12 at 16:33
5

It is not possible to do it in Standard C at initialization without explicitly enumerating all initializers.

In GNU C you can use GNU C designated initializers

 int array[90] = {[0 ... sizeof array - 1] = -1};

after initialization:

   int i;

   for (i = 0; i < sizeof array / sizeof *array; i++)
   {
       array[i] = -1;
   }
ouah
  • 142,963
  • 15
  • 272
  • 331
  • 1
    The `memset` approach seems a bit hackish to me. It's true that `-1` is represented as a sequence of bytes that all have the value `-1`, but the reason it works isn't obvious at a glance IMHO. – ruakh Nov 21 '12 at 16:08
  • @ruakh I agree it is. And it's not portable outside two's complement. I somewhat thought it was a `char` array but for an `int` array I think a `for` loop is better. I updated my answer to use a loop instead of the `memset` call. – ouah Nov 21 '12 at 16:15
  • @ouah can I free this array afterwards? – patriques Nov 21 '12 at 16:41
2

It hurts to write this, but you could always use a macro

#define FILL(arr, val) \
for(int i_##arr = 0; i_##arr < sizeof arr / sizeof *arr; ++i_##arr) \
{ \
    arr[i_##arr] = val;\
}

Then in other code:

int array[90];
FILL(array, -1);
Jacob
  • 995
  • 4
  • 12
  • Why, though? If you're just going to do this with the most straight-forward solution possible, a simple `for` loop, write a function called `fill`. There is no reason to use a macro for this. – user229044 Aug 14 '23 at 18:55
1

90 words isn't much memory. You're likely to use a good fraction of your time allocating/de-allocating the memory. Putting it on the stack is probably faster than dynamically creating the memory. I'd see if a for loop or Omkant's answer would work. If it turns out to really be the bottleneck, then you can start to optimize.

for (i = 0; i < 90; ++i) { array[i] = -1; }
Paul Rubel
  • 26,632
  • 7
  • 60
  • 80
0
memset( array, -1 , sizeof(array) ) ;

It can be used for initialising with 0 or -1

hack3r
  • 575
  • 1
  • 7
  • 13
0

There is no simple way, calloc only initializes to 0.

you can do

int *array = malloc(sizeof(int)*size);
for (i=0;i<size;i++) array[i] = -1;

or

memset(array,-1,sizeof(int)*size);

You can use memset BUT it only works if you want to use the values "0" or "-1", otherwise it won't work as expected because memset sets the same value for all the bytes.

LtWorf
  • 7,286
  • 6
  • 31
  • 45