我写了一个关于图形的程序(在数据结构中),当我完成编码时,我成功编译了它,但是当我运行。out文件时,结果是这些
razrlele@razrlele-ThinkPad:~/work/ds$ ./graph
graph: malloc.c:2372: sysmalloc: Assertion `(old_top == (((mbinptr) (((char *) &((av)- >bins[((1) - 1) * 2])) - __builtin_offsetof (struct malloc_chunk, fd)))) && old_size == 0) || ((unsigned long) (old_size) >= (unsigned long)((((__builtin_offsetof (struct malloc_chunk, fd_nextsize))+((2 *(sizeof(size_t))) - 1)) & ~((2 *(sizeof(size_t))) - 1))) && ((old_top)->size & 0x1) && ((unsigned long) old_end & pagemask) == 0)' failed.
Aborted (core dumped)
我从来没有遇到过这种错误,我该如何处理?以下是我的代码:
#include <iostream>
using namespace std;
#include <stdlib.h>
#define MAX 20
typedef struct graph
{
char v[MAX+1]; //the mark of each vertex
int vexnum, arcnum; //the number of vertex and arc
int w[MAX+1][MAX+1]; //matrix with weight of each arc
}*wgraph;
typedef struct stack
{
char v[MAX+1];
int len;
}*wstack; //stack used to calculate the path
wstack sc;
wstack way;
void creategraph(wgraph &M);
void build(wstack &s);
void clear(wstack &s);
void push(wstack &s, char e);
char pop(wstack &s);
int locate(wgraph M, char e);
int In(wstack s, char e);
void show(wgraph M, wstack s);
int compute(wgraph M, wstack s);
void compare(wgraph M, wstack &s1, wstack &s2);
void calculate(wgraph &M, int i, int j);
int main()
{
char a,b;
int key;
wgraph M;
build(sc);
build(way);
creategraph(M);
while(1)
{
clear(sc);
clear(way);
cout<<"What would you like?"<<endl
<<"1. Find a best path"<<endl
<<"0. Exit"<<endl;
cin>>key;
switch(key)
{
case 1:
cout<<"Please input two vertexs:"<<endl;
cin>>a>>b;
calculate(M, locate(M, a), locate(M, b));
break;
case 0:
cout<<"Thank you, ByeBye!";
return 0;
}
return 0;
}
}
void creategraph(wgraph &M)
{
int i, j, weight ;
char p1, p2;
M = (wgraph )malloc(sizeof(wgraph));
cout<<"Please input the number of vertex and arc"<<endl;
cin>>M->vexnum>>M->arcnum;
for(i = 1; i <= M->vexnum; i++)
for(j = 1; j <= M->vexnum; j++)
{
if(i == j) M->w[i][j] = 0;
else M->w[i][j] = -1;
} // initiate the matrix
cout<<"Vertex: "<<M->vexnum<<" Arc: "<<M->arcnum<<endl;
cout<<"PLease input the mark of each vertex"<<endl;
for(i = 1; i <= M->vexnum; i++)
cin>>M->v[i];
for(i = 1; i <= M->vexnum; i++)
cout<<M->v[i];
cout<<endl<<"please in put each arc like: a b 4"<<endl;
for(i = 1; i <= M->arcnum; i++)
{
cout<<"Please input the "<<i<<" data"<<endl;
cin>>p1>>p2;
M->w[locate(M, p1)][locate(M, p2)] = weight;
M->w[locate(M, p2)][locate(M, p1)] = weight;
}
}//creategraph
void build(wstack &s)
{
s = (wstack )malloc(sizeof(wstack));
s->len = 0;
}//build
void clear(wstack &s)
{
int i;
for(i = 1; i <= s->len; i++)
s->v[i] = ' ';
s->len = 0;
}//clear
void push(wstack &s, char e)
{
if(s->len <= MAX)
s->v[++s->len] = e;
}//push
char pop(wstack &s)
{
if(s->len > 0)
return s->v[s->len--];
return 0;
}//pop
int locate(wgraph M, char e) //locate the position of each vertex
{
int i;
for(i = 1; i <= M->vexnum; i++)
if( e == M->v[i]) return i;
}
int In(wstack s, char e)
{
int i;
for(i = 1; i <= s->len; i++)
if (s->v[i] == e) return 1;
return 0;
}//In
void show(wgraph M, wstack s)
{
int i;
for(i = 1; i<=s->len; i++)
cout<<s->v[i];
cout<<'t'<<compute(M, s)<<endl;
}//show
int compute(wgraph M, wstack s)
{
int sum = 0, i;
for(i = 1; i < s->len; i++)
sum += M->w[locate(M, s->v[i])][locate(M, s->v[i+1])];
return sum;
}//compute
void compare(wgraph M, wstack &s1, wstack &s2)
{
int i;
if(compute(M, s2) == 0 || compute(M, s2) > compute(M, s1))
for(i = 1; i <= s1->len; i++)
s2->v[i] = s1->v[i];
s2->len = s2->len;
}//compare
void calculate(wgraph &M, int i, int j)
{
int k;
push(sc, M->v[i]);
for(k = 0; k < M->vexnum; k++)
{
if((In(sc, M->v[k]) && k != j|| M->w[i][k] <= 0))
continue; // skip two points where there is no arch
else if(k != j)
calculate(M, k, j);
else if(k == j && sc->len == M->vexnum)
{
push(sc, M->v[k]);
show(M, sc);
compare(M, sc, way);
pop(sc);
}
}
pop(sc);
}
这一行
s = (wstack )malloc(sizeof(wstack));
将只分配足够的wstack
,这是一个指针。这将在大多数系统上只分配4个字节。你需要使用
s = (wstack )malloc(sizeof(stack));
分配合适的数量