linux下c语言编程出现段错误

linux下c语言编程出现段错误,第1张

简单看了一下你的程序

首先,你没有给出完整的程序,这个让大伙看起来确实不方便,因为没有main函数,可能踩内存的地方并不是这个函数本身,是main函数

其次说你的这个函数,貌似确实有点问题,但是由于没有完整的程序,我只说出我的理解,如果有错,直接忽视就好

sprintf(flag_height,"%03d",i+1)

sprintf(flag_width,"%03d",j+1)

我觉得问题出在这里,你查下

sprintf

的用法,第一个参数应该是一个字符串的首地址,你写的确实没有错,参数给入的是一个地址flag_height,但是纵观你的程序,你并没有给这个指针赋值,程序运行到这里,估计在默认创建指针的时候应该赋值为NULL(貌似根据编译器不同而有不同),那个寻址后报出段错误,也是意料之中了

由于没有你完整的代码,不知道你要做什么,修改意见不好提

给出个人意见,仅供参考

如果要打印flag_height中的值,首先应该给flag_height指针分配一片内存,使用malloc函数,其次在向内从中写入值,然后调用sprintf,就应该没有问题了

1. 段错误是什么

一句话来说,段错误是指访问的内存超出了系统给这个程序所设定的内存空间,例如访问了不存在的内存地址、访问了系统保护的内存地址、访问了只读的内存地址等等情况。这里贴一个对于“段错误”的准确定义(参考Answers.com):

A segmentation fault (often shortened to segfault) is a particular error condition that can occur during the operation of computer software. In short, a segmentation fault occurs when a program attempts to access a memory location that it is not allowed to access, or attempts to access a memory location in a way that is not allowed (e.g., attempts to write to a read-only location, or to overwrite part of the operating system). Systems based on processors like the Motorola 68000 tend to refer to these events as Address or Bus errors.

Segmentation is one approach to memory management and protection in the operating system. It has been superseded by paging for most purposes, but much of the terminology of segmentation is still used, "segmentation fault" being an example. Some operating systems still have segmentation at some logical level although paging is used as the main memory management policy.

On Unix-like operating systems, a process that accesses invalid memory receives the SIGSEGV signal. On Microsoft Windows, a process that accesses invalid memory receives the STATUS_ACCESS_VIOLATION exception.

2. 段错误产生的原因

2.1 访问不存在的内存地址

#include<stdio.h>

#include<stdlib.h>

void main()

{

int *ptr = NULL

*ptr = 0

}

2.2 访问系统保护的内存地址

#include<stdio.h>

#include<stdlib.h>

void main()

{

int *ptr = (int *)0

*ptr = 100

}

2.3 访问只读的内存地址

#include<stdio.h>

#include<stdlib.h>

#include<string.h>

void main()

{

char *ptr = "test"

strcpy(ptr, "TEST")

}

2.4 栈溢出

#include<stdio.h>

#include<stdlib.h>

void main()

{

main()

}

等等其他原因。

3. 段错误信息的获取

程序发生段错误时,提示信息很少,下面有几种查看段错误的发生信息的途径。

3.1 dmesg

dmesg可以在应用程序crash掉时,显示内核中保存的相关信息。如下所示,通过dmesg命令可以查看发生段错误的程序名称、引起段错误发生的内存地址、指令指针地址、堆栈指针地址、错误代码、错误原因等。以程序2.3为例:

panfeng@ubuntu:~/segfault$ dmesg

[ 2329.479037] segfault3[2700]: segfault at 80484e0 ip 00d2906a sp bfbbec3c error 7 in libc-2.10.1.so[cb4000+13e000]

3.2 -g

使用gcc编译程序的源码时,加上-g参数,这样可以使得生成的二进制文件中加入可以用于gdb调试的有用信息。以程序2.3为例:

panfeng@ubuntu:~/segfault$ gcc -g -o segfault3 segfault3.c

3.3 nm

使用nm命令列出二进制文件中的符号表,包括符号地址、符号类型、符号名等,这样可以帮助定位在哪里发生了段错误。以程序2.3为例:

panfeng@ubuntu:~/segfault$ nm segfault3

08049f20 d _DYNAMIC

08049ff4 d _GLOBAL_OFFSET_TABLE_

080484dc R _IO_stdin_used

w _Jv_RegisterClasses

08049f10 d __CTOR_END__

08049f0c d __CTOR_LIST__

08049f18 D __DTOR_END__

08049f14 d __DTOR_LIST__

080484ec r __FRAME_END__

08049f1c d __JCR_END__

08049f1c d __JCR_LIST__

0804a014 A __bss_start

0804a00c D __data_start

08048490 t __do_global_ctors_aux

08048360 t __do_global_dtors_aux

0804a010 D __dso_handle

w __gmon_start__

0804848a T __i686.get_pc_thunk.bx

08049f0c d __init_array_end

08049f0c d __init_array_start

08048420 T __libc_csu_fini

08048430 T __libc_csu_init

U __libc_start_main@@GLIBC_2.0

0804a014 A _edata

0804a01c A _end

080484bc T _fini

080484d8 R _fp_hw

080482bc T _init

08048330 T _start

0804a014 b completed.6990

0804a00c W data_start

0804a018 b dtor_idx.6992

080483c0 t frame_dummy

080483e4 T main

U memcpy@@GLIBC_2.0

3.4 ldd

使用ldd命令查看二进制程序的共享链接库依赖,包括库的名称、起始地址,这样可以确定段错误到底是发生在了自己的程序中还是依赖的共享库中。以程序2.3为例:

panfeng@ubuntu:~/segfault$ ldd ./segfault3

linux-gate.so.1 => (0x00e08000)

libc.so.6 =>/lib/tls/i686/cmov/libc.so.6 (0x00675000)

/lib/ld-linux.so.2 (0x00482000)

我猜想错误的原因是:

char * c = "hello"//该c是一个指针,指向“hello”

reverse(c)

函数

void reverse(char * a)

{

int len = strlen(a)//该strlen函数是计算a的长度,以'\n'为结束,所以len可能不是5;


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/8719220.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-20
下一篇 2023-04-20

发表评论

登录后才能评论

评论列表(0条)

保存