core dump segmentation fault with C++
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 pl开发者_如何学Goease 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
You probably want to do something like:
value.value_string = new RWCString(value_string_arg);
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, the new
operator allocates a new block of memory for a RWCString
and calls the copy constructor to copy the value_string_arg
. Then it assigns the pointer to the newly allocated block of memory to value.value_string
.
Don't forget to delete value.value_string
later when you're done with it to avoid a memory leak!
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 by value.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 you delete
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.
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.
(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 use union
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 a RWCString*
pointer: Are you sure it points at a valid object? When was the RWCString
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.
精彩评论