如何初始化只有-1值的数组[重复]

时间:2021-02-18 13:32:07

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

可能重复:如何在C中初始化一个int初始化数组

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数组只包含-1值的最快/最简单的方法。我需要的数组是90英特长,所以直截了当的方法应该像这样初始化它:

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.

但我只想使用一次这样的数组,所以我希望能够动态地使用它,并且能够在程序中使用它后释放它,所以我更喜欢像calloc这样的快速方式,而不是零,-1当然。

6 个解决方案

#1


9  

If you are using gcc then use designated initializer

如果您使用的是gcc,请使用指定的初始化程序

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.

要动态执行此操作,您必须使用malloc进行分配,然后只释放内存,否则释放未由malloc,calloc或realloc分配的内存是未定义的行为。

Use this:

int *array;
array=malloc(sizeof(int)*n);
for(i=0;i<n;array[i++]=-1);
// After use free this
free(array);

#2


5  

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

如果没有显式枚举所有初始值设定项,则无法在初始化时在标准C中执行此操作。

In GNU C you can use GNU C designated initializers

在GNU C中,您可以使用GNU C指定的初始化程序

 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


3  

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);

#4


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.

90字的记忆力不多。您可能会花费大量时间来分配/取消分配内存。将它放在堆栈上可能比动态创建内存要快。我会看看for循环或Omkant的答案是否有效。如果它真的是瓶颈,那么你可以开始优化。

for (i = 0; i < 90; ++i) { array[i] = -1; }

#5


1  

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

It can be used for initialising with 0 or -1

它可用于初始化为0或-1

#6


1  

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

没有简单的方法,calloc只初始化为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.

您可以使用memset但它只在您想使用值“0”或“-1”时才有效,否则它将无法正常工作,因为memset为所有字节设置相同的值。

#1


9  

If you are using gcc then use designated initializer

如果您使用的是gcc,请使用指定的初始化程序

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.

要动态执行此操作,您必须使用malloc进行分配,然后只释放内存,否则释放未由malloc,calloc或realloc分配的内存是未定义的行为。

Use this:

int *array;
array=malloc(sizeof(int)*n);
for(i=0;i<n;array[i++]=-1);
// After use free this
free(array);

#2


5  

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

如果没有显式枚举所有初始值设定项,则无法在初始化时在标准C中执行此操作。

In GNU C you can use GNU C designated initializers

在GNU C中,您可以使用GNU C指定的初始化程序

 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


3  

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);

#4


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.

90字的记忆力不多。您可能会花费大量时间来分配/取消分配内存。将它放在堆栈上可能比动态创建内存要快。我会看看for循环或Omkant的答案是否有效。如果它真的是瓶颈,那么你可以开始优化。

for (i = 0; i < 90; ++i) { array[i] = -1; }

#5


1  

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

It can be used for initialising with 0 or -1

它可用于初始化为0或-1

#6


1  

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

没有简单的方法,calloc只初始化为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.

您可以使用memset但它只在您想使用值“0”或“-1”时才有效,否则它将无法正常工作,因为memset为所有字节设置相同的值。