core dump segmentation fault with C++ -
i newbie c/cpp application , analysing issue piece of c/cpp code. came across segmentation fault error , not identify root cause of segmentation fault.
please find 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 reference union value. }
when application makes use of piece of code, generates segmentation fault @ runtime , terminates. placed few console output statements , understood segmentation fault may due
*(value.value_string) = value_string_arg;
line.
could please validate identification of segmentation fault? also, not pretty sure around issue. please let me know if has got thoughts on same.
any appreciated. thanks
~jegan
you want like:
value.value_string = new rwcstring(value_string_arg);
in code, if value.value_string
uninitialised pointer, assignment you're doing try write random part of memory, causing segmentation value. in code above, new
operator allocates new block of memory rwcstring
, calls copy constructor copy value_string_arg
. assigns pointer newly allocated block of memory value.value_string
.
don't forget delete value.value_string
later when you're done avoid memory leak!
Comments
Post a Comment