找出分段故障问题 [英] Finding out Segmentation Fault Problem

查看:73
本文介绍了找出分段故障问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的字节社区,


我想就如何在我的代码中识别用C语言编写的分段错误问题发表意见。我会分享我的代码以获得宝贵的意见,


亲切的问候。

Dear Bytes community,

I would like to get your opinion on how to identify a segmentation fault problem in my code which is written in C language. I would share my code for your valuable comments,

Kindly regards.

展开 | 选择 < span class =codeDivider> | Wrap | 行号

推荐答案

找到这类问题的唯一方法是使用你的调试器并逐步执行代码验证变量和内存内容的每一步都是你期望的。


我注意到使用了memcpy,我从来没有建议过。 mem函数是我能想到的破坏内存的最快方法。


我还注意到你的memcpy从char * 1292字节复制到指向cloudRANMessage的地址。 1)char *指向1292字节而不是少一个字节,2)目标cloudRANMessage指针指向1292字节,3)是char *指向的内存实际指向cloudRANMessage?我希望这里有编译器警告。
The only way to find these kinds of problems is to use your debugger and step through the code verifying every step of the way that your variable and memory contents are what you expect.

I do notice use of memcpy which I never advise. The mem functions are the quickest way to corrupt memory that I can think of.

I also notice that your memcpy copies from a char* 1292 bytes to a address pointing to a cloudRANMessage. 1) does the char* point to 1292 bytes and not one byte less, 2) does the target cloudRANMessage pointer point to 1292 bytes, 3) is the memory pointed at by the char* actually pointing at a cloudRANMessage? I would expect compiler warnings here.


第38行使用"%s" 来打印 message-> counterRedis 。但是, counterRedis unsigned int 而不是指针。


还有什么可能出错?

函数 printMyMessage 在解除引用之前不检查消息是否 NULL

函数 printMyMessage 打印 message->命令字符串,而不知道字符串是否正确终止。

函数 printMyMessage 打印< b> message-> clientHostName 字符串,不知道字符串是否正确终止。


函数序列化不检查消息在解除引用之前 NULL

函数序列化确实知道 *数据是否小于< b> *消息的。为什么不将数据声明为指向 cloudRANMessage ?如果两者都是相同的类型,那么你可以简单地使用赋值语句而不是 memcpy


函数 deserializeLocal 不检查是否<在取消引用它们之前,b> data 和 tempMessage NULL

函数反序列化不知道是否数据足以将那么多字节拉出来。


函数 getCallback 不会检查 r NULL

函数 getCallback 打印 reply-> str 而不知道字符串是否正确终止。


等等。分段错误的一些常见原因是解除引用 NULL 指针,读取或写入变量或缓冲区的末尾,并在释放后访问内存。
Line 38 uses "%s" to print message->counterRedis. However, counterRedis is an unsigned int not a pointer.

What else could go wrong?

Function printMyMessage does not check if message is NULL before dereferencing it.
Function printMyMessage prints the message->command string without knowing if the string is properly terminated.
Function printMyMessage prints the message->clientHostName string without knowing if the string is properly terminated.

Function serialize does not check if message is NULL before dereferencing it.
Function serialize does know if *data is smaller than *message. Why not declare data as pointing to cloudRANMessage too? If both are the same type then you can simply use assignment statement instead of memcpy.

Function deserializeLocal does not check if data and tempMessage are NULL before dereferencing them.
Function deserialize does not know if data is big enough to pull that many bytes out of.

Function getCallback does not check if r is NULL before dereferencing it.
Function getCallback prints reply->str without knowing if the string is properly terminated.

And so on. Some of the more common causes of segmentation faults are dereferencing a NULL pointer, reading or writing past the end of a variable or buffer, and accessing memory after it has been freed.


我非常感谢您的答案,这些答案有助于理解。最后我意识到要找出程序给内存转储的位置,它在这里:

I really appreciate your answers that helped to understand a lot. Finally i realized to find out where the program gives memory dump it is here:

展开 | 选择 | Wrap | 行号


这篇关于找出分段故障问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆