944,044 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 3252
  • C RSS
Aug 3rd, 2006
0

BIG problem with STL List Container

Expand Post »
I'm either missing something small or theres a BIG problem somewhere. The following code compiles sucessfully with g++ 4.1.0 under SuSE 10.1 i586 (Linux 2.6.16.13-4-default i686) but it gives a Segmentation Fault when I run it.

  1.  
  2. #include <stdio.h>
  3. #include <list>
  4.  
  5. typedef struct {
  6. std::list<int> b;
  7. } data;
  8.  
  9. int main(int argc, char **argv) {
  10. data *tmp = (data*)malloc(sizeof(data));
  11.  
  12. printf("Hello1\n");
  13. tmp->b.push_back(3);
  14. printf("Hello2\n");
  15.  
  16. return 0;
  17. }

Output:
Hello1
Segmentation fault

In fact, any operation on the list (and only the list, everything else works fine, even other members inside the same struct) results in some kind of segmentation fault as if the list needs initialization.

Any help will be appreciated (a lot).
Similar Threads
Reputation Points: 13
Solved Threads: 0
Newbie Poster
cgerber is offline Offline
2 posts
since Aug 2006
Aug 3rd, 2006
0

Re: BIG problem with STL List Container

> as if the list needs initialization.

I'd say that's dead on. If you use malloc the list's constructor isn't going to be called. Since this is C++ you should be using new/delete. You should also avoid using naked pointers most of the time, in favor of smart pointers like std::auto_ptr or a smart pointer provided by the boost library, which will automatically free resources through their destructor
Last edited by GloriousEremite; Aug 3rd, 2006 at 8:12 pm.
Reputation Points: 108
Solved Threads: 14
Junior Poster in Training
GloriousEremite is offline Offline
65 posts
since Jul 2006
Aug 3rd, 2006
3

Re: BIG problem with STL List Container

Also, if you're using C++, there's no reason you should be writing typedef struct { ... } foo;. Just write struct foo { ... };.
Team Colleague
Reputation Points: 1135
Solved Threads: 172
Super Senior Demiposter
Rashakil Fol is offline Offline
2,479 posts
since Jun 2005
Aug 4th, 2006
1

Re: BIG problem with STL List Container

Cool stuff, thank you very much.
Reputation Points: 13
Solved Threads: 0
Newbie Poster
cgerber is offline Offline
2 posts
since Aug 2006
Aug 4th, 2006
0

Re: BIG problem with STL List Container

And, cout is the C++ output command, not printf(), so you don't need stdio.h any longer.
Moderator
Reputation Points: 3278
Solved Threads: 894
Posting Sage
WaltP is offline Offline
7,747 posts
since May 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Confused
Next Thread in C Forum Timeline: Swapping





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC