C++ 的核心转储分段错误
我是 C/CPP 应用程序的新手,正在分析一段 C/CPP 代码的问题。我遇到了分段错误错误,但我无法确定分段错误的根本原因。
请找到以下场景:
union Value
{
int value_int; float value_float; RWCString *value_string;
}
void setValueString(const RWCString &value_string_arg) { *(value.value_string) = value_string_arg; //value is a reference to UNION Value. }
当应用程序使用这段代码时,它会在运行时生成分段错误并终止。我放置了一些控制台输出语句,并了解到分段错误可能是由于
*(value.value_string) = value_string_arg;
线路造成的。
有人可以验证我对分段错误的识别吗?另外,我不太确定是否可以解决这个问题。如果有人有同样的想法,请告诉我。
非常感谢任何帮助。谢谢
~杰根
I am a newbie to the C/CPP application and analysing an issue with a piece of C/CPP code. I came across a Segmentation Fault error and I am not to identify the root cause of the segmentation fault.
Please find the scenario below:
union Value
{
int value_int; float value_float; RWCString *value_string;
}
void setValueString(const RWCString &value_string_arg) { *(value.value_string) = value_string_arg; //value is a reference to UNION Value. }
when an application makes use of this piece of code, then it generates a segmentation fault at runtime and terminates. I placed few console output statements and understood that the segmentation fault may be due to
*(value.value_string) = value_string_arg;
line.
Could anyone please validate my identification of the segmentation fault? Also, I am not pretty sure as to get around this issue. Please let me know if a anyone has got thoughts on the same.
Any help is much appreciated. Thanks
~Jegan
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可能想做类似的事情:
在您的代码中,如果
value.value_string
是一个未初始化的指针,那么您正在执行的分配将尝试写入内存的某个随机部分,从而导致分段值。在上面的代码中,new
运算符为RWCString
分配一个新的内存块,并调用复制构造函数来复制value_string_arg
。然后它将指向新分配的内存块的指针分配给value.value_string
。使用完毕后,请不要忘记
删除 value.value_string
以避免内存泄漏!You probably want to do something like:
In your code, if
value.value_string
is an uninitialised pointer, then the assignment you're doing will try to write to some random part of memory, causing the segmentation value. In my code above, thenew
operator allocates a new block of memory for aRWCString
and calls the copy constructor to copy thevalue_string_arg
. Then it assigns the pointer to the newly allocated block of memory tovalue.value_string
.Don't forget to
delete value.value_string
later when you're done with it to avoid a memory leak!当您执行
*(value.value_string)
时,您取消引用指针,即您要求编译器设置指向的事物的值代码>value.value_string。您需要确保它指向有效的内存片段。如果不这样做,那么当您分配给它时,您最终将写入内存中的随机位置,从而出现分段错误。在 C++ 中,您通常可以通过事先执行类似
value.value_string = new RWCString;
的操作来获取有效的内存(并确保在完成后删除
它) 。但是,在您的情况下这很危险,因为您已经创建了指针的联合。一旦你写入例如value.value_float
,你就会丢失指针值,并且会出现内存泄漏。When you do
*(value.value_string)
, you are dereferencing the pointer, i.e. you are asking the compiler to set the value of the thing pointed to byvalue.value_string
. You need to make sure that it points at a valid piece of memory. If you don't, then when you assign to it, you will end up writing to a random location in memory, hence the segmentation fault.In C++, you would typically get a valid piece of memory by doing something like
value.value_string = new RWCString;
beforehand (and making sure youdelete
it when you are done). However, this is dangerous in your situation, because you have created a union of the pointer. As soon as you write to e.g.value.value_float
, you will have lost the pointer value, and you'll have a memory leak.尝试使用 gdb 分析核心转储。核心转储应该准确地显示段错误发生的位置。您需要在启用调试的情况下进行编译。
Try analyzing the core dump with gdb. The core dump should show you exactly where the seg fault is occurring. You will need to compile with debugging enabled.
(您使用的语言是 C++。不是 C/CPP 或 C/C++。)
这里的主要问题:您正在使用
union
。初学者可能不应该使用union,因为如果你初始化一个成员然后尝试使用另一个成员,你就会崩溃。因此,假装没有
union
并且您确实有一个RWCString*
指针:您确定它指向一个有效的对象吗?RWCString
何时构建,它的生命周期是多长?当然,分段错误并不总是发生在无效代码处。在此之前你可能会发生其他不好的事情,然后再咬你。
(The language you are using is C++. Not C/CPP or C/C++.)
The main problem here: You are using a
union
. Beginners should probably not useunion
s, because if you initialize one member and then try to use another, you blow up.So pretending there's no
union
and you really do have aRWCString*
pointer: Are you sure it points at a valid object? When was theRWCString
constructed, and what is its lifetime?Of course, segmentation faults don't always happen right at the invalid code. You might have something else bad happening before that, then biting you later.