简体   繁体   English

整个代码执行后出现“段错误”

[英]Getting “segmentation fault” after the execution of whole code

i am getting segementation fault after the execution of all my C code. 我所有的C代码执行完后,出现段错误。 It generates the rquired output with zero byte memory leak. 它生成所需的输出,零内存泄漏。 After that, it shows "segmentation fault".Following is the gdb output. 之后,显示“ segmentation fault”。以下是gdb输出。

 Program received signal SIGSEGV, Segmentation fault.
 0x08060f90 in _GLOBAL_OFFSET_TABLE_ ()
(gdb) bt 
#0  0x08060f90 in _GLOBAL_OFFSET_TABLE_ ()
#1  0xffbecd18 in ?? ()  
#2  0x15048815 in ?? ()
#3  0xcd0fbecd in ?? ()
#4  0x0610ffbe in ?? () 
#5  0xffbecd08 in ?? ()
#6  0xf7f79ff4 in ?? () from /lib/tls/libc.so.6
#7  0x00000000 in ?? ()
(gdb)

EDIT 编辑

    char **Connections,**Doors,**Zones;
    char *s1,*s2;
    char con[] = "c_";
    char zon[] = "z_";
    char dor[] = "d_";


   for (i=0; i<nc ; i++){
    s1 = con;
    s2 = string_IntToString(i);
    Connections[i]= string_Conc(s1,s2);  
    string_StringFree(s2);     
            }

EDIT 编辑

      char* string_Conc(const char* s1, const char* s2)
      { 
      char* dst;

    dst = memory_Malloc(strlen(s1) + strlen(s2) + 1);
    strcpy(dst, s1);
    return strcat(dst,s2);
      }

As commenters have pointed out, this sounds like a buffer overrun or some other event that leads to a corrupted heap, call stack, or similar. 正如评论者所指出的那样,这听起来像是缓冲区溢出或其他导致堆,调用栈或类似内容损坏的事件。

You could try running your code through Valgrind , it can often catch mistakes like those. 您可以尝试通过Valgrind运行代码,它通常会捕获类似的错误。

Segfault at the end of the program could also be a sign of memory management which hasn't been fully designed. 程序结尾处的Segfault也可能是尚未完全设计的内存管理的标志。 You might not have designed which object owns which object and in which order they will be deleted. 您可能没有设计出哪个对象拥有哪个对象以及删除对象的顺序。 For example there might be some singletons or similar that never get deleted and others that do, and some of them expect that others still exist when they really have been deleted, and then everything falls apart at the exit. 例如,可能有一些单身人士或类似的人永远不会被删除,而有的却确实会删除,其中一些人希望当其他人确实被删除后,其他人仍然存在,然后一切都在出口处瓦解。

The _GLOBAL_OFFSET_TABLE_ symbol is filled-in by the dynamic linker at runtime. 动态链接器在运行时会填充_GLOBAL_OFFSET_TABLE_符号。 A corruption at that location points to a possible toolchain issue. 该位置的损坏表示可能存在工具链问题。

A few things to consider: 需要考虑的几件事:

  • Try installing the packages that contain the debugging symbols for the GCC libraries ( libgcc and friends) and for the C library ( glibc on most Linux distributions). 尝试安装包含GCC库( libgcc和friends)和C库(在大多数Linux发行版上为glibc )调试符号的软件包。 This may allow the GDB backtrace to be more detailed. 这可以使GDB回溯更加详细。

  • If you are creating a library, you need the -fpic or -fPIC options to create position-independent code. 如果要创建库,则需要-fpic-fPIC选项来创建与位置无关的代码。 Without one of them you will see behaviour that can seem very weird. 没有它们之一,您将看到看起来奇怪的行为。

  • Verify that you are not mixing & matching resources from different toolchain versions. 确认您没有混合使用来自不同工具链版本的资源。

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

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