【发布时间】:2011-10-10 14:12:24
【问题描述】:
我有两个非常相似的程序,如下。
程序A:运行时没问题,
#include <string.h>
#include <stdio.h>
typedef struct p_struct{
unsigned char* pulist;
int length;
} list_type;
int get_struct(list_type* l)
{
memset(l->pulist, 0, 4);
l->length=4;
}
int main ()
{
list_type str;
get_struct(&str);
}
程序 B:有一个额外的函数调用,仍然可以编译,但由于 gcc 的运行时错误“分段错误”而崩溃。
#include <string.h>
#include <stdio.h>
typedef struct p_struct{
unsigned char* pulist;
int length;
} list_type;
int get_struct(list_type* l)
{
memset(l->pulist, 0, 4);
l->length = 4;
}
int get_struct_a()
{
list_type str;
get_struct(&str);
}
int main ()
{
get_struct_a();
}
我真的很难找出这里的问题。谁能告诉我是什么导致“分段错误”?另外,为什么程序 B 给出“分段错误”错误,而程序 A 没有?
【问题讨论】:
-
他老派又没用,
typedef struct构造怎么了? -
@John Dibling:这是 C,而不是 C++,它是定义类型的一种完全有效且有用的方法。
-
它最初被标记为 C++。
标签: c function pointers segmentation-fault