typedef struct aresta {
int idAresta;
struct aresta *prox;
} aresta;
typedef struct vertice {
int idVertice;
struct vertice* prox;
aresta* ini;
} vertice;
typedef struct grafo {
vertice* ini;
int tamanho;
} grafo;
在windows中运行的代码块中
当我插入第二个垂直
我有分段错误:
0 0x40171e jaExisteVertice(g=0x692238, idVertice=2)
1 0x4015b6 inserirVertice(g=0x692238, id=2)
2 0x401415 main()
插入语(g,tmp);
void inserirVertice(grafo *g, int id)
{
if (jaExisteVertice(g, id)) //nao deixa inserir dois vertices iguais
printf("Ja existe um vértice com esse ID, vértice não inserido.\n");
else if (!jaExisteVertice(g, id)) //se nao existe vertice igual. Redundante mas garantido
{
vertice *v = (vertice*) malloc(sizeof(vertice));
if (g->ini != NULL)
{
vertice *aux = g->ini;
while (aux->prox != NULL)
{
aux = aux->prox;
}
aux->prox = v;
}
else { // quando for o primeiro vertice do grafo
g->ini = v;
}
v->idVertice = id;
g->tamanho++;
}
printGrafo(g);
}
JayeSisteStices(G,ID);
int jaExisteVertice(grafo *g, int idVertice)
{
vertice *v = g->ini;
while (v != NULL)
{
if (v->idVertice == idVertice)
return 1;
v = v->prox;
}
return 0;
}
我不明白为什么会有这个错误,我的代码看起来很好,我已经逐行检查了几次。唯一的问题是没有进行malloc验证。
如果列表不为空,我会重新检查我的算法,所以它会插入到动态列表的末尾,并且看起来没有代码问题,理论上,如果代码正常,那么在不同的操作系统中应该没有问题。
Windows 10,编译器选项中的代码块16.01:GNU GCC-
使用内置规格。COLLECT_GCC=GCC COLLECT_LTO_WRAPPER=/usr/lib/GCC/x86_64-pc-cygwin/6.4.0/LTO-WRAPPER.exe目标:x86_64-pc-cygwin配置为/cygdrive/i/szsz/tmpp/GCC/GCC-6.4.0-5.x86_64/src/GCC-6.4.0/configure--iconv前缀——不带libintl前缀--LIBReExdidir//Ur/LIB--启用共享-启用共享LIBGCC——启用静态-启用版本特定的运行时LIBS——启用Bootstrap -使能-YX-CXAYAutExtU-THYTFF2-用Type =通用语言——使能语言=艾达、C、C++、FORTRAN、LTO、Objc,obj-c++--enable graphite--enable threads=posix--enable libatomic--enable libcilkrts--enable libgomp--enable libitm--enable libquadmath--enable libssp--enable libada--disable symvers--with gnu ld--with gnu as--with cloog include=/usr/include/cloog isl--without libiconv prefix--without libintl prefix--使用系统zlib——启用链接器构建id——使用默认libstdcxx abi=gcc4兼容的线程模型:posix gcc版本6.4.0(gcc)
Linux Ubuntu-Codeblocks 13.12,相同的编译器选项-
没问题
Linux Ubuntu-用终端编译-
使用内置规格。
COLLECT_GCC=GCC
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/5/LTO包装器
目标:x86_64-linux-gnu
配置为:../src/configure-v--with pkgversion==Ubuntu 5.4.0-6ubuntu1~16.04.9'--with bugurl=file:///usr/share/doc/gcc-5/README.Bugs--enable languages=c,ada,c++,java,go,d,fortran,objc,obj-c++--prefix=/usr--program suffix=-5--enable shared--enable linker build id--libexecdir=/usr/lib--without included gettext--enable threads=posix--libdir=/usr/lib--enable nls--with sysroot=/--enable clocale=gnu--enable libstdcxx debug--enable libstdcxx time=yes--with default libstdcxx abi=new--enable gnu unique object--disable vtable verify--enable libmpx--enable plugin--with system zlib--disable browser plugin--enable java awt=gtk--enable gtk cairo--with java home=/usr/lib/jvm/java-1.5.0-gcj-5-amd64/jre--enable java home--with jvm root dir=/usr/lib/jvm/java-1.5.0-gcj-5-amd64--with jvm jar dir=/usr/lib/jvm exports/java-1.5.0-gcj-5-amd64--with arch directory=amd64--with ecj jar=/usr/share/java/eclipse-ecj.jar--enable objc gc--enable multiarch--disable werror--with-arch-32=i686--with abi=m64--with multilib list=m32,m32--enable multilib--with tune=generic--enable checking=release--build=x86_-linux-gnu--host=x86_-linux-gnu--target=x86_-linux-gnu
线程模型:posix