Thread: How to instantiate a std::list contained in a struct.

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    269

    How to instantiate a std::list contained in a struct.

    I'm new to C++ (math major here). Here's a simple example of what I'm trying to do.

    Suppose I have some struct, we'll call it simple.

    Code:
    typedef struct simple
    {
      int a;
    }simple;
    Now, suppose I have another struct that contains some data, and an std::list that will hold type simple:

    Code:
    typedef struct _tx_info
    {
      int something;
      list<simple*> simple_list;
    }tx_info;
    Now, in main, if I want to create a type of tx_info, and use that list, I'm not sure what the
    syntax is just to instantiate the list..

    Code:
    int main(int argc, char ** argv)
    {
      tx_info * info = (tx_info*)malloc(sizeof(tx_info));
      simple * s = (simple*)malloc(sizeof(simple));
      s->a = 10;
      info->simple_list.push_front(s);                                            
      return 0;
    }
    Obviously, this will segfault because I never instantiate the list.

    What's confusing to me, is if at the top of the C++ file, I do

    Code:
    list<simple*> simple_list;
    that will both declare and instantiate a list. I'm not sure how to instantiate it when my list is
    declared within a struct.

    Thanks so much for the help!

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    269
    I think I got it.

    In my struct, I make it a pointer to a list.

    And before I call push_front, I do

    Code:
    info->simple_list = new list<simple*>();

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    This is exactly why people here tell everyone NEVER to use malloc in C++. Use "new" and "delete"; then it should at least call the list constructor, and then it ought to work.

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    269
    Quote Originally Posted by EVOEx View Post
    This is exactly why people here tell everyone NEVER to use malloc in C++. Use "new" and "delete"; then it should at least call the list constructor, and then it ought to work.
    are you talking about my tx_info struct? I should be calling new instead of malloc?

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Since you want to instantiate a list in a struct, I would write a constructor for the struct. The only real difference between struct and class in C++ is that struct makes everything public by default, where class makes everything private by default.

  6. #6
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    Code:
    tx_info * info = (tx_info*)malloc(sizeof(tx_info));
    If you were using `new', the default constructor for `_tx_info' would construct the `std::list<simple*>' data member.

    As EVOEx said, `malloc' and `free' really don't belong in C++ code.

    Soma

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by dayalsoap View Post
    I think I got it.

    In my struct, I make it a pointer to a list.

    And before I call push_front, I do

    Code:
    info->simple_list = new list<simple*>();
    No no NO! That is very very wrong.
    You do not do anything to instantiate the list. The code creating an instance of the struct doesn't even have to know that it contains a list.
    It doesn't work simply because you're using malloc. Do not use malloc in C++! Only use the 'new' keyword, and only in the places where you are currently using malloc.
    It will just work without you having to do anything.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by whiteflags View Post
    Since you want to instantiate a list in a struct, I would write a constructor for the struct.
    You don't need to write a consturctor here, and for this example one certainly shouldn't.
    A list knows how to construct itself. It will all just work if the OP doesn't ever use malloc in a C++ program.

    malloc, memset, memcpy etc should all be banned from any C++ program. They outright destroy the ability for things to work properly.
    Last edited by iMalc; 06-23-2011 at 03:37 AM.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  9. #9
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Quote Originally Posted by iMalc View Post
    You don't need to write a consturctor here, and for this example one certainly shouldn't.
    A list knows how to construct itself. It will all just work if the OP doesn't ever use malloc in a C++ program.

    malloc, memset, memcpy etc should all be banned from any C++ program. They outright destroy the ability for things to work properly.
    I certainly agree with you here, but there may be a reason to write a constructor after all:
    Quote Originally Posted by dayalsoap View Post
    I think I got it.

    In my struct, I make it a pointer to a list.

    And before I call push_front, I do

    Code:
    info->simple_list = new list<simple*>();
    Now, a pointer to a list containing simple is list<simple>*. In which case we'd want to write

    Code:
     list = new list<simple>;
    in a constructor and clean it up with RAII.

    Even if the type is correct by OP as written, meaning that he actually wants a list of simple pointers ( list<simple*> ) there is still some work to be done in a constructor/destructor pair. The constructor might be empty in that case actually but the destructor would need to free the nodes in the list,
    Code:
    list<simple*>::iterator node;
    while ( iter < list.end() ) {
       node = iter++;
       delete *node;
    }
    since all the elements would be new'd pointers.

    In general the idea is a mess since simple isn't even a base class, but if you're going to do something like this....

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    There is also no need for typedef.
    struct simple
    {
    int a;
    };
    As simple as that.
    I think you are poisoned by C. I would recommend you get a C++ book and start from the beginning. Forget C. Learn C++.
    A good book is Accelerated C++.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by whiteflags View Post
    Now, a pointer to a list containing simple is list<simple>*. In which case we'd want to write
    Whilst that is technically true, this is not a pointer to a list, and most people will probably never come across a case where their struct should actually contain a pointer to a list. It should just be a list, as it is already.
    Even if the type is correct by OP as written, meaning that he actually wants a list of simple pointers ( list<simple*> ) there is still some work to be done in a constructor/destructor pair. The constructor might be empty in that case actually but the destructor would need to free the nodes in the list
    If the list contains dynamically allocated items then a destructor is required (amoung other things), but a constructor (other than a copy-constructor) is not required, and should only be used if there are other reason to have it.

    We'd probably find though that if we got to see more of the code, that the list would be best to contain the actual objects, rather than pointers anyway.

    Quote Originally Posted by Elysia View Post
    I think you are poisoned by C.
    agreed
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. struct holding data inside a linked list struct
    By icestorm in forum C Programming
    Replies: 2
    Last Post: 10-06-2009, 12:49 PM
  2. how to see if one string is contained in the other
    By bigal1496 in forum C Programming
    Replies: 1
    Last Post: 08-02-2009, 05:23 PM
  3. Question about list (struct {..struct *next}
    By jmzl666 in forum C Programming
    Replies: 1
    Last Post: 10-23-2002, 01:34 AM
  4. Self contained help
    By loggjam in forum C Programming
    Replies: 4
    Last Post: 08-05-2002, 11:20 AM
  5. How to instantiate classes on cue.
    By sean in forum C++ Programming
    Replies: 4
    Last Post: 02-04-2002, 11:36 PM