gpt4 book ai didi

c - 代码块(GCC编译器)上的段错误,但是在Linux(GNU GCC)中可以

转载 作者:行者123 更新时间:2023-11-30 19:51:07 25 4
gpt4 key购买 nike

在我的代码中,我有以下结构:

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中运行的代码块中出现SEGMENTATION FAULT:

0 0x40171e  jaExisteVertice(g=0x692238, idVertice=2)

1 0x4015b6 inserirVertice(g=0x692238, id=2)

2 0x401415 main()


inserirVertice(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);
}


jaExisteVertice(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-g 6.4.0-5.x86_64 / src / gcc-6.4.0 / configure --srcdir = / cygdrive / i / szsz / tmpp / gcc / gcc-6.4.0-5.x86_64 / src / gcc-6.4.0- -prefix = / usr --exec-prefix = / usr --localstatedir = / var --sysconfdir = / etc --docdir = / usr / share / doc / gcc --htmldir = / usr / share / doc / gcc / html -C --build = x86_64-pc-cygwin --host = x86_64-pc-cygwin --target = x86_64-pc-cygwin --without-libiconv-prefix --without-libintl-prefix --libexecdir = / usr / lib --enable-shared --enable-shared-libgcc --enable-static --enable-version-specific-runtime-libs --enable-bootstrap --enable -__ cxa_atexit --with-dwarf2 --with-tune = generic --enable-languages = ada,c,c ++,fortran,lto,objc,obj-c ++ --enable-graphite --enable-threads = posix --enable-libatomic --enable-libcilkrts --enable-libgomp -- -enable-libitm --enable-libquadmath --enable-libquadmath-support --disable-libssp --enable-libada --disable-symvers --with-gnu-ld --with-gnu-as --with-cloog -include = / usr / include / cloog-isl --without-libiconv-prefix --without-libintl-prefix --with-system-zlib --enable-linker-build-id --with-default-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-wrapper
目标: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,m64,mx32 --enable-multilib --with-tune = generic --enable-checking = release --build = x86_64 -linux-gnu --host = x86_64-linux-gnu --target = x86_64-linux-gnu
螺纹型号:posix
gcc版本5.4.0 20160609(Ubuntu 5.4.0-6ubuntu1〜16.04.9)

最佳答案

我不知道main()的样子,但是在jaExisteVertice中,您需要确保已初始化g-> ini以避免未定义的行为。

关于c - 代码块(GCC编译器)上的段错误,但是在Linux(GNU GCC)中可以,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50752802/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com