BIG problem with STL List Container

Reply

Join Date: Aug 2006
Posts: 2
Reputation: cgerber is an unknown quantity at this point 
Solved Threads: 0
cgerber cgerber is offline Offline
Newbie Poster

BIG problem with STL List Container

 
0
  #1
Aug 3rd, 2006
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).
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 65
Reputation: GloriousEremite will become famous soon enough GloriousEremite will become famous soon enough 
Solved Threads: 14
GloriousEremite GloriousEremite is offline Offline
Junior Poster in Training

Re: BIG problem with STL List Container

 
0
  #2
Aug 3rd, 2006
> 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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 2,039
Reputation: Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice 
Solved Threads: 139
Team Colleague
Rashakil Fol's Avatar
Rashakil Fol Rashakil Fol is offline Offline
Super Senior Demiposter

Re: BIG problem with STL List Container

 
1
  #3
Aug 3rd, 2006
Also, if you're using C++, there's no reason you should be writing typedef struct { ... } foo;. Just write struct foo { ... };.
All my posts may be redistributed under the GNU Free Documentation License.
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 2
Reputation: cgerber is an unknown quantity at this point 
Solved Threads: 0
cgerber cgerber is offline Offline
Newbie Poster

Re: BIG problem with STL List Container

 
1
  #4
Aug 4th, 2006
Cool stuff, thank you very much.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: BIG problem with STL List Container

 
0
  #5
Aug 4th, 2006
And, cout is the C++ output command, not printf(), so you don't need stdio.h any longer.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC