By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,188 Members | 977 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,188 IT Pros & Developers. It's quick & easy.

STL inside a structure (I'm stuck!)

P: 4
I'm REALLY stuck on this:

Expand|Select|Wrap|Line Numbers
  1. // data
  2. typedef struct {
  3.   int foo;
  4.   list<int> mylist;
  5. } MyStructRec, *MyStruct; // MyStruct it's a pointer
  6.  
  7. // code
  8. MyStruct struct = (MyStruct) malloc(sizeof(MyStructRec));
  9.  
  10. struct->mylist.push_back(123); // mylist is not initialized!!!
  11.  
...How do I initialize (allocate) the STL list???
Do I have to mess with allocators?
Really, no clue.
Thanks!
Jun 7 '07 #1
Share this Question
Share on Google+
3 Replies


AdrianH
Expert 100+
P: 1,251
I'm REALLY stuck on this:

Expand|Select|Wrap|Line Numbers
  1. // data
  2. typedef struct {
  3.   int foo;
  4.   list<int> mylist;
  5. } MyStructRec, *MyStruct; // MyStruct it's a pointer
  6.  
  7. // code
  8. MyStruct struct = (MyStruct) malloc(sizeof(MyStructRec));
  9.  
  10. struct->mylist.push_back(123); // mylist is not initialized!!!
  11.  
...How do I initialize (allocate) the STL list???
Do I have to mess with allocators?
Really, no clue.
Thanks!
Try not using malloc. Use new.

Expand|Select|Wrap|Line Numbers
  1. // code
  2. MyStruct struct = new MyStructRec();
  3.  
Also, your use of typedef is one I've only seen M$ do. I'd get out of that habbit. Or atleast use the naming convention that M$ does. It is confusing.


Adrian
Jun 7 '07 #2

P: 4
Sga
Try not using malloc. Use new.
Thanks A LOT!
I also understood that I had some holes in my new/delete knowledge.
...Now I'm up to some code cleaning...

Also, your use of typedef is one I've only seen M$ do. I'd get out of that habbit.
It is an old habit... But Linux seems to cope well with it, so...
Cheers!
Jun 8 '07 #3

AdrianH
Expert 100+
P: 1,251
Thanks A LOT!
I also understood that I had some holes in my new/delete knowledge.
...Now I'm up to some code cleaning...



It is an old habit... But Linux seems to cope well with it, so...
Cheers!
Good luck.


Adrian
Jun 8 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.