在主函数中更改全局数组的大小
Changing the size of a global array in the main function
我正在编写一个程序,需要创建可变数量的链表。程序开头全局声明如下:
struct Node
{
int Line;
struct Node *Next;
} ;
struct Node* Heads[5];
struct Node* Currs[5];
int NumOfNames;
main()
中的第一个函数将计算并存储 NumOfNames
的值。然后我需要将 Heads[5]
和 Currs[5]
的大小更改为 Heads[NumOfNames]
和 Currs[NumOfNames]
以创建 NumOfNames
数量的链表。可以按照这些思路做任何事情吗?
我对编程还很陌生,我对 malloc
和 realloc
的了解很差,但我认为我需要以某种方式使用这些功能才能实现我的目标。
感谢您提前提出任何建议。
你需要这个:
struct Node
{
int Line;
struct Node *Next;
} ;
struct Node** Heads;
struct Node** Currs;
int NumOfNames ;
main()
{
int NumOfNames = ... ; // calculate the number of names
...
Heads = malloc(NumOfNames * sizeof (struct Node*));
// now Heads point to an array of pointers to struct Node of length NumOfNames
...
Heads[1] = malloc(sizeof (struct Node));
...
}
停止使用静态数组,改为在运行时分配:
struct Node* Heads;
struct Node* Currs;
Heads = malloc(NumOfNames * sizeof *Heads);
Currs = malloc(NumOfNames * sizeof *Currs);
然后你可以通过Heads[NumOfNames - 1]
(含)访问Heads[0]
,当然前提是分配成功。
您拥有的是 static allocation and the memory is allocated on stack (actually not really on stack, since these are global variables - see the comments). It means that the size of the array needs to be known at compile time. On the other hand, you want to be able to allocate memory at run time since you don't know the size (NumOfNames
) earlier. That is called dynamic allocation,为此您需要 malloc
(或 calloc
,...)。这样的内存是在堆上分配的。有关这方面的更多信息,请阅读例如这些:
- Difference between static memory allocation and dynamic memory allocation
- What and where are the stack and heap?
要使用 malloc
做您想做的事,您可以这样做:
struct Node
{
int Line;
struct Node *Next;
} ;
struct Node** Heads;
struct Node** Currs;
int NumOfNames;
int main() {
// ...
NumOfNames = 42;
Heads = malloc(NumOfNames * sizeof(struct Node*));
Currs = malloc(NumOfNames * sizeof(struct Node*));
// if you want to allocate the Nodes too...
int i;
for (i = 0; i < NumOfNames; i++) {
Heads[i] = malloc(sizeof(struct Node));
Currs[i] = malloc(sizeof(struct Node));
}
// ...
}
当您动态分配内存时(使用 malloc
、calloc
、realloc
、...),请务必 free()不再需要它了。
如果您被允许使用 C99(gcc 的标志 -std=c99
),那么您也有 variable length arrays available. Here。
// From the wikipedia page
float read_and_process(int n)
{
float vals[n];
for (int i = 0; i < n; i++)
vals[i] = read_val();
return process(vals, n);
}
然后您的 Heads
和 Currs
可以使用变量作为大小。另一方面,您不能将它们设为全局,您必须将指针和大小传递给子函数。
另请注意,如果您是编程和 C 的新手,那么 malloc
和 free
的使用是您真正想要学习的东西。
另请注意,VLA 是 not very popular,例如,如果您没有足够的内存为它们分配 space,它们不会 return 错误,这可能会导致难以调试和危险的问题。
我觉得我必须写这个替代方案,但我也真的认为你应该选择 malloc
和 free
,并了解这是如何完成的。检查是否 malloc
returned NULL
并优雅地退出 :)
我正在编写一个程序,需要创建可变数量的链表。程序开头全局声明如下:
struct Node
{
int Line;
struct Node *Next;
} ;
struct Node* Heads[5];
struct Node* Currs[5];
int NumOfNames;
main()
中的第一个函数将计算并存储 NumOfNames
的值。然后我需要将 Heads[5]
和 Currs[5]
的大小更改为 Heads[NumOfNames]
和 Currs[NumOfNames]
以创建 NumOfNames
数量的链表。可以按照这些思路做任何事情吗?
我对编程还很陌生,我对 malloc
和 realloc
的了解很差,但我认为我需要以某种方式使用这些功能才能实现我的目标。
感谢您提前提出任何建议。
你需要这个:
struct Node
{
int Line;
struct Node *Next;
} ;
struct Node** Heads;
struct Node** Currs;
int NumOfNames ;
main()
{
int NumOfNames = ... ; // calculate the number of names
...
Heads = malloc(NumOfNames * sizeof (struct Node*));
// now Heads point to an array of pointers to struct Node of length NumOfNames
...
Heads[1] = malloc(sizeof (struct Node));
...
}
停止使用静态数组,改为在运行时分配:
struct Node* Heads;
struct Node* Currs;
Heads = malloc(NumOfNames * sizeof *Heads);
Currs = malloc(NumOfNames * sizeof *Currs);
然后你可以通过Heads[NumOfNames - 1]
(含)访问Heads[0]
,当然前提是分配成功。
您拥有的是 static allocation and the memory is allocated on stack (actually not really on stack, since these are global variables - see the comments). It means that the size of the array needs to be known at compile time. On the other hand, you want to be able to allocate memory at run time since you don't know the size (NumOfNames
) earlier. That is called dynamic allocation,为此您需要 malloc
(或 calloc
,...)。这样的内存是在堆上分配的。有关这方面的更多信息,请阅读例如这些:
- Difference between static memory allocation and dynamic memory allocation
- What and where are the stack and heap?
要使用 malloc
做您想做的事,您可以这样做:
struct Node
{
int Line;
struct Node *Next;
} ;
struct Node** Heads;
struct Node** Currs;
int NumOfNames;
int main() {
// ...
NumOfNames = 42;
Heads = malloc(NumOfNames * sizeof(struct Node*));
Currs = malloc(NumOfNames * sizeof(struct Node*));
// if you want to allocate the Nodes too...
int i;
for (i = 0; i < NumOfNames; i++) {
Heads[i] = malloc(sizeof(struct Node));
Currs[i] = malloc(sizeof(struct Node));
}
// ...
}
当您动态分配内存时(使用 malloc
、calloc
、realloc
、...),请务必 free()不再需要它了。
如果您被允许使用 C99(gcc 的标志 -std=c99
),那么您也有 variable length arrays available. Here。
// From the wikipedia page
float read_and_process(int n)
{
float vals[n];
for (int i = 0; i < n; i++)
vals[i] = read_val();
return process(vals, n);
}
然后您的 Heads
和 Currs
可以使用变量作为大小。另一方面,您不能将它们设为全局,您必须将指针和大小传递给子函数。
另请注意,如果您是编程和 C 的新手,那么 malloc
和 free
的使用是您真正想要学习的东西。
另请注意,VLA 是 not very popular,例如,如果您没有足够的内存为它们分配 space,它们不会 return 错误,这可能会导致难以调试和危险的问题。
我觉得我必须写这个替代方案,但我也真的认为你应该选择 malloc
和 free
,并了解这是如何完成的。检查是否 malloc
returned NULL
并优雅地退出 :)