LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-24-2010, 10:13 AM   #1
Accinson
LQ Newbie
 
Registered: Feb 2009
Posts: 22

Rep: Reputation: 6
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....

Why does it segfault?
 
Old 09-24-2010, 10:28 AM   #2
neonsignal
Senior Member
 
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Bookworm (Fluxbox WM)
Posts: 1,391
Blog Entries: 54

Rep: Reputation: 360Reputation: 360Reputation: 360Reputation: 360
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.
 
1 members found this post helpful.
Old 09-26-2010, 04:16 PM   #3
Accinson
LQ Newbie
 
Registered: Feb 2009
Posts: 22

Original Poster
Rep: Reputation: 6
Thank you for the reply :-)

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?
 
Old 09-26-2010, 04:59 PM   #4
neonsignal
Senior Member
 
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Bookworm (Fluxbox WM)
Posts: 1,391
Blog Entries: 54

Rep: Reputation: 360Reputation: 360Reputation: 360Reputation: 360
Quote:
Originally Posted by Accinson View Post
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 View Post
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.
 
1 members found this post helpful.
Old 09-26-2010, 05:24 PM   #5
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by Accinson View Post
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 View Post
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.

Last edited by johnsfine; 09-26-2010 at 05:30 PM.
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Template algorithm for std::vector and std::string CartmanYO Programming 4 09-08-2010 09:56 AM
Assigning custom string to std::string in C++ jh7777 Programming 4 10-13-2009 12:02 AM
int to std::string lucky6969b Programming 1 06-05-2006 07:17 AM
SIGSEGV for std::string dragondad Programming 2 02-05-2006 11:26 AM
C++ std::string to int Slaxx Programming 1 10-30-2004 10:03 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:52 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration