gpt4 book ai didi

c - 将异常数组归零

转载 作者:行者123 更新时间:2023-12-02 00:01:38 30 4
gpt4 key购买 nike

我有一些 C 代码,转载如下。据我了解,它将 BootPML4 数组的某些位设置为某个值。有人可以解释一下下面的 BootPML4 数组是如何填充的吗?另外,如何确保数组中未使用的值被清零,同时确保下面的两个设置保持不变?

typedef uint64_t pml4_entry_t;
#define PML4_PROT (INTEL_PTE_VALID | INTEL_PTE_WRITE)
pml4_entry_t BootPML4[PTE_PER_PAGE] __attribute__((section("__HIB, __bootPT"))) = {
[0] = ((uint64_t)(PAGE_SIZE) | PML4_PROT),
[KERNEL_PML4_INDEX] = ((uint64_t)(PAGE_SIZE) | PML4_PROT),
};

最佳答案

这是使用在 C99 中添加的 designated initializersC99 draftForward 部分5 段中的标准说:

[...] Major changes from the previous edition include:

并具有以下项目符号:

— designated initializers

6.7.8 Initialization 部分介绍了详细信息,并在 3637 段中提供了以下示例em>:

EXAMPLE 12 Space can be ‘‘allocated’’ from both ends of an array by using a single designator:

int a[MAX] = {
1, 3, 5, 7, 9, [MAX-5] = 8, 6, 4, 2, 0
};

In the above, if MAX is greater than ten, there will be some zero-valued elements in the middle; if it is less than ten, some of the values provided by the first five initializers will be overridden by the second five.

gcc doc designated initializers 有一个更好的例子:

To specify an array index, write ‘[index] =’ before the element value. For example,

int a[6] = { [4] = 29, [2] = 15 };

is equivalent to

int a[6] = { 0, 0, 15, 0, 29, 0 };

The index values must be constant expressions, even if the array being initialized is automatic.

关于c - 将异常数组归零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20983619/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com