linux下运行c程序报段错误 5

intcreateList(FILE*fp,structRoad**roadlist){longnodeNum=0;structRoad*Head,*CurNode,*N... int createList(FILE *fp, struct Road **roadlist)
{
long nodeNum = 0;
struct Road *Head, *CurNode, *NexNode;
unsigned short int roadSize = 0, roadNameSize = 0;
unsigned long roadLinkID = 0, roadInfo = 0;
unsigned char roadInfoBuff[50];
Head = NULL;
CurNode = NULL;
NexNode = NULL;
Head = malloc(sizeof(struct Road));
NexNode = Head;
while (1)
{
CurNode = malloc(sizeof(struct Road));

fread(&roadSize, sizeof(short int), 1, fp);
roadSize = M_CHG_INT16(roadSize);
CurNode->one_Road.RoadSize = roadSize;

fread(&roadLinkID, sizeof(long), 1, fp);
roadLinkID = M_CHG_INT32(roadLinkID);
CurNode->one_Road.RoadLinkID = roadLinkID;

fread(&roadNameSize, sizeof(short int), 1, fp);
roadNameSize = M_CHG_INT16(roadNameSize);
CurNode->one_Road.RoadNameSize = roadNameSize;

fread(&roadInfo, sizeof(long), 1, fp);
roadInfo = M_CHG_INT32(roadInfo);
CurNode->one_Road.RoadStaNodeInfo = roadInfo;

if (feof(fp))
break;

if (1 == GET_FLAG(roadInfo))
{
CurNode->one_Road.RoadName = malloc(roadNameSize + 2);
fread(roadInfoBuff, roadNameSize + 2, 1, fp);
strcpy(CurNode->one_Road.RoadName, roadInfoBuff);
}
else
{
CurNode->one_Road.RoadName = malloc(sizeof("暂无名称"));
strcpy(CurNode->one_Road.RoadName, "暂无名称");
}
CurNode->nextRoadNode = NULL;
NexNode->nextRoadNode = CurNode;
NexNode = CurNode;
nodeNum++;
}
*roadlist = Head;
return nodeNum;
}
就是这个函数运行时,在windows下能正常运行,拿到linux下时就报错了
展开
 我来答
jxc6698
2014-04-25
知道答主
回答量:32
采纳率:0%
帮助的人:16.6万
展开全部
我没看完,就看了几行,这里就有错误
CurNode = malloc(sizeof(struct Road));

fread(&roadSize, sizeof(short int), 1, fp);
roadSize = M_CHG_INT16(roadSize);
CurNode->one_Road.RoadSize = roadSize;
这里 CurNode 是刚刚申请的吧, CurNode->one_Road 这个指针指向谁?我没看见你给这个指针赋值或者是分配空间

如果还有错误的话你还可以再追问
追问


定义的两个结构体是这样的,第一个存储道路信息类似与Node,第二个用于链表类似于Link

追答
看起来没什么问题啊,你确定就是这个函数吗?
内村访问越界不一定就是立刻 segment fault 的
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式