| | |
BIG problem with STL List Container
![]() |
•
•
Join Date: Aug 2006
Posts: 2
Reputation:
Solved Threads: 0
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.
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).
C Syntax (Toggle Plain Text)
#include <stdio.h> #include <list> typedef struct { std::list<int> b; } data; int main(int argc, char **argv) { data *tmp = (data*)malloc(sizeof(data)); printf("Hello1\n"); tmp->b.push_back(3); printf("Hello2\n"); return 0; }
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).
•
•
Join Date: Jul 2006
Posts: 65
Reputation:
Solved Threads: 14
> 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
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.
![]() |
Similar Threads
- Link list taking an object... (C)
- Big Problem!!!!!!!!!!! (Viruses, Spyware and other Nasties)
- Big Problem, Generic Error (VB.NET)
- Using the STL LIst Container, how do I create, write,read, and store in file. (C++)
Other Threads in the C Forum
| Thread Tools | Search this Thread |
* ansi api array arrays binarysearch calculate centimeter changingto char character convert copyanyfile copypdffile creafecopyofanytypeoffileinc createcopyoffile createprocess() database directory dynamic execv fflush file floatingpointvalidation fork forloop frequency function getlasterror getlogicaldrivestrin givemetehcodez grade graphics gtkgcurlcompiling gtkwinlinux highest histogram homework i/o inches include infiniteloop input interest intmain() iso keyboard km linked linkedlist linux list looping loopinsideloop. lowest matrix microsoft mysql oddnumber open opendocumentformat openwebfoundation pdf pointer posix power program programming pyramidusingturboccodes radix read recursion recv recvblocked repetition reversing scanf scheduling segmentationfault send sequential shape single socketprogramming stack standard strchr string suggestions test threads turboc unix urboc user variable whythiscodecausesegmentationfault win32api windows.h windowsapi






