用vs编译c时出现错误: 0xC0000005: 读取位置 0xCDCDCDCD 时发生访问冲突

34818060 2016-11-06 12:41:19
 #include<stdio.h>
#include<malloc.h>
#include"graph.h"
extern void mattolist(mgraph, algraph *);
extern void dispadj(algraph *);
int visited[MAXV];
void pathall1(algraph *G, int u, int v, int path[], int i){
arcnode *p;
int j, n;
visited[u] = 1;
p = G->adjlist[u].firstarc;
while (p != NULL){
n= p->adjvex;
if (n == v){
path[i + 1] = v;
for (j = 0; j <= i + 1; j++)
printf("%3d", path[j]);
printf("\n");
}
else if (visited[n] == 0){
path[i + 1] = n;
pathall1(G, n, v, path, i + 1);


}
p = p->nextarc;
}

visited[u] = 0;

}
void pathall2(algraph *G, int u, int v, int l, int path[], int d){
int m, i;
arcnode*p;
visited[u] = 1;
d++;
path[d] = u;
if (u == v&&d == 1){
for (i = 0; i <= d; i++)
printf("%3d", path[i]);
printf("\n");
}
p = G->adjlist[u].firstarc;
while (p != NULL){
m = p->adjvex;
if (visited[m] == 0)
pathall2(G, m, v, l, path, d);
p = p->nextarc;
}
visited[u] = 0;



}
int shortpath(algraph *G, int u, int v, int path[]){
struct{
int vno;
int level;
int parent;

}qu[MAXV];
int front = -1, rear = -1, k, lev, i, j;
arcnode *p;
visited[u] = 1;
rear++;
qu[rear].vno = u;
qu[rear].level = 0;
qu[rear].parent = -1;
while (front<rear){
front++;
k = qu[front].vno;
lev = qu[front].level;
if (k == v){
i = 0;
j = front;
while (j != -1){
path[lev - i] = qu[j].vno;
j = qu[j].parent;
i++;
}
return lev;

}
p = G->adjlist[k].firstarc;
while (p != NULL){
if (visited[p->adjvex] == 0){
visited[p->adjvex] = 1;
rear++;
qu[rear].vno = p->adjvex;
qu[rear].level = lev + 1;
qu[rear].parent = front;

}
p = p->nextarc;
}
}
return -1;
}
void main(){
int i, j;
int u = 5, v = 2, d = 3;
int path[MAXV];
mgraph g;
algraph *G;
int A[MAXV][6] = { { 0, 1, 0, 1, 0,0 }, { 0, 0, 1, 0, 0,0 }, { 1, 0, 0, 0, 0,1 }, { 0, 0, 1, 0, 0,1 }, { 0, 0, 0, 1, 0,0 }, {1,1,0,1,1,0} };
g.n = 6; g.e = 10;
for (i = 0; i<g.n; i++)
for (j = 0; j<g.n; j++)
g.edges[i][j] = A[j][j];
G = (algraph *)malloc(sizeof (algraph));
mattolist(g, G);
printf("邻接表\n");
dispadj(G);
for (i = 0; i<g.n; i++)
visited[i] = 0;
printf("从顶点%d到%d的所有路径:\n", u, v);
path[0] = u;
visited[u] = 1;
pathall1(G, u, v, path, 0);
printf("从顶点%d到%d的所有长度为%d的路径:\n", u, v, d);
pathall2(G, u, v, d, path, -1);
printf("\n");
for (i = 0; i<g.n; i++)
visited[i] = 0;
printf("从顶点%d到%d的最短路径:\n", u, v);
for (i = 0; i<g.n; i++)
visited[i] = 0;
j = shortpath(G, u, v, path);
for (i = 0; i <= j; i++)
printf("%3d", path[i]);
printf("\n");


}


 #include<stdio.h>
#include<malloc.h>
#include"graph.h"
//不带权图的算法
void mattolist(mgraph g, algraph *G){
int i, j;
arcnode *p;
G = (algraph *)malloc(sizeof (algraph));
for (i = 0; i<g.n; i++)
G->adjlist[i].firstarc = NULL;
for (i = 0; i<g.n; i++)
for (j = g.n - 1; j >= 0; j--)
if (g.edges[i][j] != 0){
p = (arcnode *)malloc(sizeof(arcnode));
p->adjvex = j;
p->nextarc = G->adjlist[i].firstarc;
G->adjlist[i].firstarc = p;

}
G->n = g.n; G->e = g.e;
}
void listtomat(algraph *G, mgraph g){
int i,j;
arcnode *p;
for (i = 0; i<G->n; i++)
for (j = 0; j<G->n; j++)
g.edges[i][j] = 0;
for (i = 0; i<G->n; i++){
p = G->adjlist[i].firstarc;
while (p != NULL){
g.edges[i][p->adjvex] = 1;
p = p->nextarc;
}

}
g.n = G->n; g.e = G->e;

}
void dispmat(mgraph g){
int i, j;
for (i = 0; i<g.n; i++){
for (j = 0; j<g.n; j++)
printf("%3d", g.edges[i][j]);
printf("\n");
}

}
void dispadj(algraph *G){
int i;
arcnode *p;
for (i = 0; i<G->n; i++){
p = G->adjlist[i].firstarc;
printf("%3d", i);
while (p != NULL){
printf("%3d", p->adjvex);
p = p->nextarc;
}
printf("\n");
}

}

//graph.h头文件
typedef int InfoType;
#define MAXV 100//最大顶点个数
#define INF 32767
//定义邻接矩阵类型
typedef struct
{
int no;//顶点标号
InfoType info;//顶点其他信息,这里用于存放权值
}VertexType;//顶点类型
typedef struct//图的定义
{
int edges[MAXV][MAXV];//邻接矩阵
int n, e;//顶点数,弧数
VertexType vexs[MAXV];//存放顶点信息
}mgraph;//图的邻接矩阵类型
//以下定义邻接表类型
typedef struct ANode//弧的节点结构类型
{
int adjvex;//该弧的终点位置
struct ANode *nextarc;//指向下一条弧的指针
InfoType info;//该弧的相关信息,这里用于存放权值
}arcnode;
typedef int Vertex;
typedef struct Vnode//邻接表节点的类型
{
Vertex data;//顶点信息
arcnode * firstarc;//指向第一条弧
}VNode;
typedef VNode AdjList[MAXV];//AdjList 是邻接表类型
typedef struct
{
AdjList adjlist;//邻接表
int n, e;//图的顶点数n和边数e
}algraph;//图的邻接表类型


三个代码片段如上,我想输出一个有向图最短路径,编译没有错误,但是程序在箭头所指地方出现异常
运行就出现这个报错0xC0000005: 读取位置 0xCDCDCDCD 时发生访问冲突,请教一下到底哪里有问题?

...全文
1090 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2016-11-11
  • 打赏
  • 举报
回复
不要依赖调试器输出复杂数据结构!而要将复杂数据结构的整个内容在处理它的每一步使用一小段代码按自己很容易理解的格式输出,非常有助于调试!或者可以说是“基础设施” 仅供参考: 十字链表交换任意两个节点C源代码(C指针应用终极挑战)http://download.csdn.net/detail/zhao4zhong1/5532495
赵4老师 2016-11-08
  • 打赏
  • 举报
回复
崩溃的时候在弹出的对话框按相应按钮进入调试,按Alt+7键查看Call Stack即“调用堆栈”里面从上到下列出的对应从里层到外层的函数调用历史。双击某一行可将光标定位到此次调用的源代码或汇编指令处,看不懂时双击下一行,直到能看懂为止
34818060 2016-11-07
  • 打赏
  • 举报
回复
引用 1 楼 northwesternwind 的回复:
首先这是运行问题i,不是编译问题。 其次,如果提示是0xCDCDCDCD这样的数据,原因是你的变量没有赋值。 自己读代码吧。
但是编译通过了啊,不知道怎么改
34818060 2016-11-07
  • 打赏
  • 举报
回复
引用 5 楼 qq423399099 的回复:
windows里常见的内存填充数据含义 * 0xABABABAB : Used by Microsoft's HeapAlloc() to mark "no man's land" guard bytes after allocated heap memory * 0xABADCAFE : A startup to this value to initialize all free memory to catch errant pointers * 0xBAADF00D : Used by Microsoft's LocalAlloc(LMEM_FIXED) to mark uninitialised allocated heap memory * 0xBADCAB1E : Error Code returned to the Microsoft eVC debugger when connection is severed to the debugger * 0xBEEFCACE : Used by Microsoft .NET as a magic number in resource files * 0xCCCCCCCC : Used by Microsoft's C++ debugging runtime library to mark uninitialised stack memory * 0xCDCDCDCD : Used by Microsoft's C++ debugging runtime library to mark uninitialised heap memory * 0xDEADDEAD : A Microsoft Windows STOP Error code used when the user manually initiates the crash * 0xFDFDFDFD : Used by Microsoft's C++ debugging heap to mark "no man's land" guard bytes before and after allocated heap memory * 0xFEEEFEEE : Used by Microsoft's HeapFree() to mark freed heap memory 要在函数内改变指针的指向,必须传二级指针或者一级指针的引用才行
但是我不知道更改什么地方
AlbertS 2016-11-07
  • 打赏
  • 举报
回复
有未初始化的内存或变量
小灸舞 版主 2016-11-07
  • 打赏
  • 举报
回复
windows里常见的内存填充数据含义 * 0xABABABAB : Used by Microsoft's HeapAlloc() to mark "no man's land" guard bytes after allocated heap memory * 0xABADCAFE : A startup to this value to initialize all free memory to catch errant pointers * 0xBAADF00D : Used by Microsoft's LocalAlloc(LMEM_FIXED) to mark uninitialised allocated heap memory * 0xBADCAB1E : Error Code returned to the Microsoft eVC debugger when connection is severed to the debugger * 0xBEEFCACE : Used by Microsoft .NET as a magic number in resource files * 0xCCCCCCCC : Used by Microsoft's C++ debugging runtime library to mark uninitialised stack memory * 0xCDCDCDCD : Used by Microsoft's C++ debugging runtime library to mark uninitialised heap memory * 0xDEADDEAD : A Microsoft Windows STOP Error code used when the user manually initiates the crash * 0xFDFDFDFD : Used by Microsoft's C++ debugging heap to mark "no man's land" guard bytes before and after allocated heap memory * 0xFEEEFEEE : Used by Microsoft's HeapFree() to mark freed heap memory 要在函数内改变指针的指向,必须传二级指针或者一级指针的引用才行
YXTS122 2016-11-06
  • 打赏
  • 举报
回复
看了半天,表示没看懂
paschen 2016-11-06
  • 打赏
  • 举报
回复
你代码中肯定使用了未初始化的堆内存 出错时点中断,通过调用堆栈定位到相关位置观察分析原因
northwesternwind 2016-11-06
  • 打赏
  • 举报
回复
引用 1 楼 northwesternwind 的回复:
首先这是运行问题i,不是编译问题。 其次,如果提示是0xCDCDCDCD这样的数据,原因是你的变量没有赋值。 自己读代码吧。
引用 1 楼 northwesternwind 的回复:
首先这是运行问题i,不是编译问题。 其次,如果提示是0xCDCDCDCD这样的数据,原因是你的变量没有赋值。 自己读代码吧。
你的这个函数:

void mattolist(mgraph g, algraph *G)
在里面给G分配内存,根本带不回去。应该这样:

void mattolist(mgraph g, algraph **G)
另外,一般为了保险,malloc之后,先用memset把内存清空。防止忘记给里面的变量赋值。

memset(p, sizeof (algraph),0);
我没仔细读你代码,不知道有没有这样问题。
northwesternwind 2016-11-06
  • 打赏
  • 举报
回复
首先这是运行问题i,不是编译问题。 其次,如果提示是0xCDCDCDCD这样的数据,原因是你的变量没有赋值。 自己读代码吧。

69,373

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧