main() 内的结构定义导致分段错误
是否无法在 main() 内部定义结构? 我尝试了以下方法只是为了得到分段错误:
#include <stdio.h>
#include <unistd.h>
#include <strings.h>
#define TRUE 1
void main(int argc,char **argv)
{
struct test_struct
{
char test_name[50];
char summary_desc[200];
char result[50];
};
struct suite_struct
{
char suite_name[50];
struct test_struct test[500];
int test_count;
int passed;
int failed;
int unresolved;
int notrun;
}suite[500];
int a,b;
for (a=0;a<500;a++)
{
strcpy(suite[a].suite_name,"");
for (b=0;b<500;b++)
{
strcpy(suite[a].test[b].test_name,"");
strcpy(suite[a].test[b].summary_desc,"");
strcpy(suite[a].test[b].result,"");
}
suite[a].test_count=0;
suite[a].passed=0;
suite[a].failed=0;
suite[a].unresolved=0;
suite[a].notrun=0;
}
}
但是当我将结构定义放在外面时,它就起作用了:
#include <stdio.h>
#include <unistd.h>
#include <strings.h>
#define TRUE 1
struct test_struct
{
char test_name[50];
char summary_desc[200];
char result[50];
};
struct suite_struct
{
char suite_name[50];
struct test_struct test[500];
int test_count;
int passed;
int failed;
int unresolved;
int notrun;
}suite[500];
void main(int argc,char **argv)
{
int a,b;
for (a=0;a<500;a++)
{
strcpy(suite[a].suite_name,"");
for (b=0;b<500;b++)
{
strcpy(suite[a].test[b].test_name,"");
strcpy(suite[a].test[b].summary_desc,"");
strcpy(suite[a].test[b].result,"");
}
suite[a].test_count=0;
suite[a].passed=0;
suite[a].failed=0;
suite[a].unresolved=0;
suite[a].notrun=0;
}
}
不确定为什么会发生这种情况。 为此,我使用 Solaris SunStudio 编译器。
Is it not possible to define structure inside main() .
I tried the following only to get a Segmentation Fault:
#include <stdio.h>
#include <unistd.h>
#include <strings.h>
#define TRUE 1
void main(int argc,char **argv)
{
struct test_struct
{
char test_name[50];
char summary_desc[200];
char result[50];
};
struct suite_struct
{
char suite_name[50];
struct test_struct test[500];
int test_count;
int passed;
int failed;
int unresolved;
int notrun;
}suite[500];
int a,b;
for (a=0;a<500;a++)
{
strcpy(suite[a].suite_name,"");
for (b=0;b<500;b++)
{
strcpy(suite[a].test[b].test_name,"");
strcpy(suite[a].test[b].summary_desc,"");
strcpy(suite[a].test[b].result,"");
}
suite[a].test_count=0;
suite[a].passed=0;
suite[a].failed=0;
suite[a].unresolved=0;
suite[a].notrun=0;
}
}
But the moment I take the struct definition outside it works:
#include <stdio.h>
#include <unistd.h>
#include <strings.h>
#define TRUE 1
struct test_struct
{
char test_name[50];
char summary_desc[200];
char result[50];
};
struct suite_struct
{
char suite_name[50];
struct test_struct test[500];
int test_count;
int passed;
int failed;
int unresolved;
int notrun;
}suite[500];
void main(int argc,char **argv)
{
int a,b;
for (a=0;a<500;a++)
{
strcpy(suite[a].suite_name,"");
for (b=0;b<500;b++)
{
strcpy(suite[a].test[b].test_name,"");
strcpy(suite[a].test[b].summary_desc,"");
strcpy(suite[a].test[b].result,"");
}
suite[a].test_count=0;
suite[a].passed=0;
suite[a].failed=0;
suite[a].unresolved=0;
suite[a].notrun=0;
}
}
Not sure why this is happening.
I am using the Solaris SunStudio compiler for this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在第一个示例中,
suite
位于堆栈中,而在第二个示例中,它位于数据段中。由于
suite
相当大(~75MB),因此段错误几乎肯定是由于您的程序耗尽了堆栈空间。在大多数情况下,最好在堆上分配大型数据结构(使用
malloc()
等)。这也使得您可以只分配所需的空间量,而不是总是为 500 个元素分配空间。In the first example,
suite
lives on the stack, and in the second it lives on the data segment.Since
suite
is quite large (~75MB), the segfault is almost certainly due to your program running out of stack space.In most cases, it is best to allocate large data structures on the heap (using
malloc()
et al). This will also make it possible to allocate just the amount of space you require instead of always allocating space for 500 elements.在 main 中声明一个结构体是可以的。但在您的程序中,问题与您在 main 函数内创建该结构的 500 个对象有关。每个对象的大小约为 15 KB。因此,500 个对象大约需要 75 MB。尝试 printf("size: %lu\n", sizeof suite);。
默认情况下,您没有那么多可用的堆栈。您可以使用命令
ulimit -s
查找可用堆栈。它以 KB 为单位打印可用堆栈。您可以使用
ulimit
命令来增加堆栈。例如ulimit -s 100000
。更好的方法是使用
malloc()
动态分配所需的内存。It is okay to declare a structure inside main. But in your program, the problem has got to do with the fact that you are creating 500 objects of that structure inside main function. Each object is about 15 KB in size. So, 500 objects require about 75 MB. Try
printf("size: %lu\n", sizeof suite);
.You don't have that much of stack available by default. You can find the available stack using the command
ulimit -s
. It prints the available stack in KBs.You can use the
ulimit
command to increase the stack. e.g.ulimit -s 100000
.A better approach is to dynamically allocate the memory you require using
malloc()
.在任何函数(包括
main
)内定义struct
并声明该struct
的局部变量都是合法的。但是代码在语法上可能是合法的,但在运行时会崩溃(例如,因为根据 C 标准,它具有未定义的行为,或者因为它遇到了某些系统限制,例如调用堆栈的限制)。
It is legal to define a
struct
and to declare a local variable of thatstruct
inside any function, includingmain
.But a code can be syntactically legal and crash at runtime (e.g. because it has an undefined behavior, per the C standard, or because it hits some system limitation, like a limit on the call stack).
您在 main 外部定义的结构是全局的且未初始化,因此它将进入 .bss 段并在执行开始时初始化为 0。您在 main 中定义的结构体非常巨大,超过了最大堆栈大小(在 Linux 上约为 1-2MB,Solaris 上也可能如此)。由于 main 之外的那个不在堆栈上,因此它似乎在这种情况下工作,而不是在另一种情况下工作。
The struct you define outside of main is global and uninitialized so it will go into the .bss segment and be initialized to 0 at the start of execution. The struct you define inside main is huge and exceeds the maximum stack size (which is about 1-2MB on Linux and probably Solaris too). Since the one outside of main is not on the stack, it appears to work in that case and not the other.
除了有关堆栈空间、malloc 和未定义行为的答案之外。 。 。
当我尝试编译你的代码时,我收到了 3 个警告。
返回 main 的 int,而不是 void。
在 C 中,strcpy 的标头是 string.h,而不是 strings.h。
In addition to answers about stack space, malloc, and undefined behavior . . .
When I tried to compile your code, I got 3 warnings.
Return an int for main, not void.
In C, the header for strcpy is string.h, not strings.h.