Why does the following code cause segmentation fault?
#include <string>
union U {
bool boolean;
double number;
std::string* str_ptr;
};
int main()
{
U u{new std::string("A")};
delete u.str_ptr;
return 0;
// return u.str_ptr->compare("A");
}
I should say that it does not seem to matter if I instead of the return statement try to access the pointer in some other way. E.g. replace delete u.str_ptr; return 0;
with return u.str_ptr->compare("A");
I still get a segmentation fault.
In case this is compiler specific I am using g++ (Ubuntu 5.4.0-6ubuntu1~16.04.2) 5.4.0 20160609
It is not legal to access u.str_ptr
unless it was the last member set in the union. But the last member set in the union is boolean
, since an initializer list with one undesignated value sets the first member.
See "member lifetime" under struct and union initialization.
U u{new std::string("A")};
doesn't make what you think, as you said nothing special it initializes the first member with the value of the call to new
. If you want to set the right member use the following:
U u{.str_ptr=new std::string("A")};
---EDIT---
This is gcc
extension, standard says When a union is initialized with a brace-enclosed initializer, the braces shall only contain an initializer-clause for the first non-static data member of the union.. Then with standard you can only do :
U u;
u.str_ptr = new std::string("A");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With