初始化布尔值的大小可变的数组用C
所以我现在有一个结构,看起来像这样:初始化布尔值的大小可变的数组用C
typedef struct example {
bool arr[]; //i would like this to be an array of booleans,
//but i don't know the size right now; this is
//probably wrong
} example_t;
我也有一个创建功能,看起来像这样:
example_t *newExample(int SIZE){
example_t *example = malloc(sizeof(example_t));
//initialize arr to a bool array of size SIZE, ideally all false!
example->arr = ???
return example;
}
而且从这样,我就可以做这样的事情:
example_t *example = newExample(MAX);
if (example->arr[10])
....
这是可能的C,创建一个可变大小的布尔值数组?
仅供参考:我需要好歹整数映射到一个char*
或bool
,所以我可以调用arr[num]
并能够得到一个字符串/字或真/假值。两种方式,我不知道如何声明,然后用可变大小初始化。提前致谢!
在C99中,您可以使用flexible array members,其中最后一个成员可以是没有给定维度的数组,但struct
中必须至少有2个成员。
您可以使用指针(这里我使用int
,而不是bool
为了简洁):
#include <stdlib.h>
#include <stdio.h>
typedef struct example {
int *arr;
} example_t;
static example_t *newExample(size_t SIZE)
{
example_t *example = malloc(sizeof(example_t) + sizeof(int) * SIZE);
example->arr = (int *)(example + 1);
example->arr[5] = 5;
return example;
}
int main(void)
{
example_t *x = newExample(10);
printf("%d\n", x->arr[5]);
free(x);
return 0;
}
但是这并没有太大的意义,为什么不添加包含的元素数量的第一部件?
#include <stdlib.h>
#include <stdio.h>
typedef struct example {
size_t size;
int arr[];
} example_t;
static example_t *newExample(size_t SIZE)
{
example_t *example = malloc(sizeof(example_t) + sizeof(int) * SIZE);
example->size = SIZE;
example->arr[5] = 5;
return example;
}
int main(void)
{
example_t *x = newExample(10);
printf("%d\n", x->arr[5]);
free(x);
return 0;
}
使用这种方法富人的优势:您可以将对象传递给任何功能,无需经过额外的参数大小。
如果您事先不知道数组的大小,您可以简单地将它作为指针并在稍后进行分配。
typedef struct example {
bool* arr; // <-- Pointer
int size; // <-- Size of the array
} example_t;
创建一个新的数组遵循相同的步骤。
example_t* newExample(int size) {
int i;
example_t* example = malloc(sizeof(example_t));
example->arr = malloc(sizeof(bool) * size);
example->size = size;
for(i = 0; i < size; i++)
example->arr[i] = false;
return example;
}
不要忘记释放bool arr
和使用后struct
本身的记忆!
可以通过例如后加条内存做到这一点:
size_t boolsize = 10 * sizeof(bool);
struct example *ptr = malloc(sizeof *ptr + boolsize);
你调用malloc只有一次,此内存方式奠定了连续
做这些东西:
结构定义:
typedef struct example {
bool *arr;
} example_t;
创建:
example_t *newExample(int SIZE){
example_t *example = malloc(sizeof(example_t));
example->arr = malloc(SIZE*sizeof(bool)) // allocate space for arr
for(int i = 0;i < SIZE;i++)
example->arr[i] = false; // initialize all of arr elements to false
return example;
}
可选地,也可以保持变量在struct存储arr
阵列(沿着*arr
)的大小,这样就可以使用该访问arr
阵列时以后。
当结构example
的使用完成时,您将不得不手动使用free()
。
允许结构的最后一个成员是未指定大小的数组,因此称为“灵活数组成员”,因此您的声明是正确的。*您有责任计算malloc的正确大小,因此在您的情况下,这样做:
example_t* example = malloc(sizeof(example_t) + SIZE * sizeof(bool));
如果你想它(在一个典型的布尔型false
)初始化为0
,要么使用calloc
:
example_t* example = calloc(1, sizeof(example_t) + SIZE * sizeof(bool));
或者使用memset
的malloc后初始化:
memset(example, 0, sizeof(example_t) + SIZE * sizeof(bool));
*)只要您的结构中还有其他成员在数组之前。否则结构将是无用的,你可以只分配普通数组:
bool *example = malloc(SIZE * sizeof(bool));
我认为OP要求'布尔'数组初始化,而不是'int'数组。但是,这个概念是相同的。 – Muntasir
@Muntasir,是的,概念是一样的,无论如何我编辑答案... –