2D char数组的全局和局部声明

时间:2021-09-03 21:34:39

I have very strange problem while I am submitting a practice problem on codechef. Where the solution " having global declaring of 2D char array" is getting accepted while the one with "Declaration inside the main function" is getting rejected as wrong answer.

当我在codechef上提交练习题时,我有一个非常奇怪的问题。解决方案“具有2D char数组的全局声明”被接受,而具有“主要功能内的声明”的解决方案被拒绝作为错误答案。

Below are links for solution.

以下是解决方案的链接。

1) Having global declaration : http://www.codechef.com/viewsolution/1138654
2) Having Delaration inside main() : http://www.codechef.com/viewsolution/1138660

1)获得全球声明:http://www.codechef.com/viewsolution/1138654 2)在main()内部进行分层:http://www.codechef.com/viewsolution/1138660

PS1: I am not using the 2D char array outside the main function.
PS2: I talking about the array " char boardString[1000][1000] ";

PS1:我没有在主函数之外使用2D char数组。 PS2:我在谈论数组“char boardString [1000] [1000]”;

2 个解决方案

#1


3  

Because you probably run out of stack space.

因为你可能用完了堆栈空间。

When you declare an array globally it gets allocated in the data/Bss segment(Note this is implementation detail)
While, when you declare a array in main() it gets created locally on the stack(Again a implementation detail)

当你全局声明一个数组时,它会在data / Bss段中分配(注意这是实现细节)当你在main()中声明一个数组时,它会在堆栈上本地创建(同样是一个实现细节)

Since the array you are allocating is huge(1000 X 10000) you might run out of stack space.
Codechef is intelligent enough to detect this problem and hence it rejects the code with array in main() as a wrong answer.

由于您要分配的数组很大(1000 X 10000),因此可能会耗尽堆栈空间。 Codechef足够智能来检测这个问题,因此它拒绝使用main()中的数组作为错误答案的代码。

#2


1  

As Als correctly remarks the problem is most probably due to stack space limits. If you prefer to encapsulate your variable inside main but not to allocate it on the stack you'd just have to declare it static, there.

正如Als正确评论该问题很可能是由于堆栈空间限制。如果您希望将变量封装在main中,而不是将其分配到堆栈上,则只需将其声明为静态。

#1


3  

Because you probably run out of stack space.

因为你可能用完了堆栈空间。

When you declare an array globally it gets allocated in the data/Bss segment(Note this is implementation detail)
While, when you declare a array in main() it gets created locally on the stack(Again a implementation detail)

当你全局声明一个数组时,它会在data / Bss段中分配(注意这是实现细节)当你在main()中声明一个数组时,它会在堆栈上本地创建(同样是一个实现细节)

Since the array you are allocating is huge(1000 X 10000) you might run out of stack space.
Codechef is intelligent enough to detect this problem and hence it rejects the code with array in main() as a wrong answer.

由于您要分配的数组很大(1000 X 10000),因此可能会耗尽堆栈空间。 Codechef足够智能来检测这个问题,因此它拒绝使用main()中的数组作为错误答案的代码。

#2


1  

As Als correctly remarks the problem is most probably due to stack space limits. If you prefer to encapsulate your variable inside main but not to allocate it on the stack you'd just have to declare it static, there.

正如Als正确评论该问题很可能是由于堆栈空间限制。如果您希望将变量封装在main中,而不是将其分配到堆栈上,则只需将其声明为静态。