C++: program crashes on exit on std::string destructor
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
C++: program crashes on exit on std::string destructor
Hi everybody,
i am wondering why the following simple code causes a segfault on exit,when the std::string destructor is called.
Quote:
#include <iostream>
#include <string>
using namespace std;
struct myStruct
{
int a;
string b;
int c;
};
int main()
{
myStruct st;
st.a=2;
st.c=4;
memset(&st,0,sizeof(myStruct));
cout << "Hello world!" << endl;
return 0;
}
I guess the memset messes with some internal std::string's data structures/pointers. I personally am anti-memset,and always avoid them,but i'm really curious to know this....
The typical implementation of an STL object is to allocate space on the heap for the actual data. So if you zero the structure that contains the pointer to that data, you will get a segment violation when it tries to use the pointer (probably when it calls the destructor at the end of the program).
Anyway, in C++ you could have written a constructor inside your struct, rather than zeroing it externally.
I have a further question,though....
The project i "inherited" makes a terribly large use of memset to zero of various structures. Replacing all of them is a daunting task, and, besides,i can't even do it for some of them,so i ask:
1)What about structures composed ONLY by native integral data types? I guess i can leave "their" memsets with no harm... Is it right?
2)What about structures with methods? Is it ok to memset them to zero?
1)What about structures composed ONLY by native integral data types? I guess i can leave "their" memsets with no harm... Is it right?
Yes, this won't cause problems in a normal scenario. It is not very portable (for example, floating point representations might be different on other processors), but you could live with it.
You are correct in avoiding memset in typical applications, because such functions effectively break the strong type checking, which results in a whole class of potential bugs that the compiler cannot pick up at compile time.
Quote:
Originally Posted by Accinson
2)What about structures with methods? Is it ok to memset them to zero?
In most circumstances, adding methods does not affect the way the structure is represented in memory. C++ was designed this way, both for efficiency and to increase compatibility with C.
The only caveat is for virtual methods, because these are typically implemented using a vtable pointer at the beginning of the object (instance of a struct or class). This also applies if any objects in the parent hierarchy have a virtual method.
Last edited by neonsignal; 09-26-2010 at 05:10 PM.
I guess the memset messes with some internal std::string's data structures/pointers
...
Why does it segfault?
Do you need a more detailed answer than the one you gave yourself?
If so, you would need to look at the source code of the specific implementation of std::string.
Quote:
Originally Posted by Accinson
1)What about structures composed ONLY by native integral data types? I guess i can leave "their" memsets with no harm... Is it right?
I think you can go quite a bit wider than that. If no members of the struct have defined constructors (or have members of their own with defined constructors) then a memset as you described should not be able to do harm. Nothing should be assuming the unconstructed members would not be zero filled.
So ints, floats, pointers, etc. and substructures of those should all be OK.
I would have warned you about virtual methods in the class or any base class or any member or base class of member, etc. But neonsignal gave a good enough warning on that topic. Don't do any memset that would overwrite any object's vtable pointer.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.