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下时就报错了 展开
{
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下时就报错了 展开
1个回答
展开全部
我没看完,就看了几行,这里就有错误
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 这个指针指向谁?我没看见你给这个指针赋值或者是分配空间
如果还有错误的话你还可以再追问
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 这个指针指向谁?我没看见你给这个指针赋值或者是分配空间
如果还有错误的话你还可以再追问
追答
看起来没什么问题啊,你确定就是这个函数吗?
内村访问越界不一定就是立刻 segment fault 的
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |