繁体   English   中英

程序可以正常运行,但最终会“检测到堆栈崩溃”

[英]Program works but get **stack smashing detected** in the end

我已经检查了这个讨论: 在程序结束时检测到堆栈崩溃

但这不是同一个问题。

就我而言。 我的程序可以运行,但是如果我运行10倍:6倍没有错误,而4倍最后给我“检测到堆栈崩溃”,则输出错误并显示错误。 我发现很多人说这是由gcc或linux SO引起的。 在在线编译器中,我运行20倍以上,并且从未遇到此错误。

#include <stdio.h>
#include <stdlib.h>

void bubbleSort(int v[], int tam)
{
    int aux=0, i=0, j=0;
    for (i = 0; i < tam; i++)
    {
        printf("%d\n",i );
        for (j = 0; j < tam - i; j++)
        {
            if (v[j] > v[j + 1])
            {
                printf("a\n");
                aux = v[j];
                printf("b\n");
                v[j] = v[j + 1];
                printf("c\n");
                v[j + 1] = aux;
                printf("d\n");
        }
    }
}
}

int main(void)
{
    int v[10] = {1, 5, 6, 8, 9, 0, 4, 3, 2, 7};
    int n = 10;
    bubbleSort(v, n);

    for (int i = 0; i < n; i++)
    {
        printf("%d,", v[i]);
    }
    printf("\n");
    return 0;
}

我的输出:

0
a
b
c
d
a
b
c
d
a
b
c
d
a
b
c
d
a
b
c
d
a
b
c
d
1
a
b
c
d
a
b
c
d
a
b
c
d
a
b
c
d
a
b
c
d
a
b
c
d
2
a
b
c
d
a
b
c
d
a
b
c
d
a
b
c
d
a
b
c
d
3
a
b
c
d
a
b
c
d
a
b
c
d
a
b
c
d
a
b
c
d
4
a
b
c
d
a
b
c
d
a
b
c
d
a
b
c
d
5
a
b
c
d
a
b
c
d
6
a
b
c
d
7
a
b
c
d
8
a
b
c
d
9
a
b
c
d


-1228779264,0,1,2,3,4,5,6,7,8,
*** stack smashing detected ***

Valgrind日志:

==7890== 
==7890== Process terminating with default action of signal 6 (SIGABRT)
==7890==    at 0x4E6F428: raise (raise.c:54)
==7890==    by 0x4E71029: abort (abort.c:89)
==7890==    by 0x4EB17E9: __libc_message (libc_fatal.c:175)
==7890==    by 0x4F5315B: __fortify_fail (fortify_fail.c:37)
==7890==    by 0x4F530FF: __stack_chk_fail (stack_chk_fail.c:28)
==7890==    by 0x4006EC: main ()
==7890== 
==7890== HEAP SUMMARY:
==7890==     in use at exit: 0 bytes in 0 blocks
==7890==   total heap usage: 1 allocs, 1 frees, 1,024 bytes allocated
==7890== 
==7890== All heap blocks were freed -- no leaks are possible
==7890== 
==7890== For counts of detected and suppressed errors, rerun with: -v
==7890== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
Abortado

-> LINUX:Ubuntu 16.04

- > GCC:

Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/5/lto-wrapper
Target: x86_64-linux-gnu
Configured with: ../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
Thread model: posix
gcc version 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.9)  

bubbleSort ,当i为0时, for (j = 0; j < tam - i; j++)for (j = 0; j < tam - 0; j++) ,所以j迭代到tam-1并包括tam-1

然后,在循环中,将v[j]v[j + 1] 后者可以是v[tam-1 + 1] ,即v[tam] ,在阵列之外。 仅此一项是C标准不允许的参考。 如果比较结果为true,则代码继续将v[j]v[j + 1]交换。 那会在数组v的边界之外写入数据,因此会破坏您的进程中的某些内存。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM