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.

1

6 Answers 6

13

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);
Sign up to request clarification or add additional context in comments.

7 Comments

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
you can do it dynamically also
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: see my edit for dynamic array
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.
|
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;
   }

3 Comments

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 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 can I free this array afterwards?
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);

1 Comment

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

Comments

-1
memset( array, -1 , sizeof(array) ) ;

It can be used for initialising with 0 or -1

3 Comments

How would you use it to initialize an array with 1?
memset( array, 1 , sizeof(array) ) ; to initialise an array with all 1's.
Nope, that won't work. Try it; if you've got 32-bit integers, it will initialize them all to 16843009!
-1

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.

1 Comment

Again, memset won't work for ints.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.