Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

C++ list STL

  • 12-12-2009 10:45am
    #1
    Registered Users, Registered Users 2 Posts: 58 ✭✭


    Hi,

    I have a few questions on how memory is managed in a linked list in the C++ STL

    Each list element has 3 fields, a time_t, int and string. In C I would use a struct. Is a class the best thing to use for c++?

    Should I allocate memory using the new operator for the elements that I add to the linked list or when i call the push_back() function is a copy of the object placed on the linked list? The elements are added to the list inside a function in a class.

    If elements were dynamically allocated do i need to iterate through the linked list in the destructor and use the delete operator for each element.

    Is the destructor called when the progam ends or do I need to call the clear() function of the list to call the destructor?


Comments

  • Registered Users, Registered Users 2 Posts: 2,082 ✭✭✭Tobias Greeshman


    jlynch wrote: »
    Each list element has 3 fields, a time_t, int and string. In C I would use a struct. Is a class the best thing to use for c++?
    The only difference in C++ between a struct and a class, is that a struct has public access by default. So I'd go with a struct unless you explicitly need to use encapsulation.
    jlynch wrote: »
    Should I allocate memory using the new operator for the elements that I add to the linked list or when i call the push_back() function is a copy of the object placed on the linked list? The elements are added to the list inside a function in a class.
    Overriding the new() operator sounds like a lot of over-kill, unless you plan on doing custom allocation of memory. From what I can remember using the push_back() method will cause a copy of the element to be put into the linked list.


  • Registered Users, Registered Users 2 Posts: 9,579 ✭✭✭Webmonkey


    Yeah as far as I remember, the pus_back method makes a copy thus you need to provide a copy constructor.


  • Closed Accounts Posts: 18 Jiri


    The only difference in C++ between a struct and a class, is that a struct has public access by default. So I'd go with a struct unless you explicitly need to use encapsulation.


    Overriding the new() operator sounds like a lot of over-kill, unless you plan on doing custom allocation of memory. From what I can remember using the push_back() method will cause a copy of the element to be put into the linked list.

    I guess he meant not overriding new operator, but just dynamically alocate each item in list.
    class A
    {
    int a;
    double b;
    };

    int main()
    {
    std::list<A*> mList;
    A* item;

    item = new A;
    item.a = 1;
    mList.push_back(item);


    item = new A;
    item.a = 2;
    mList.push_back(item);

    ...

    ... and finally dealocate...

    std::list<A*>::iterator it = mList.begin();
    while (it!=mList.end()) {
    delete (*it);
    }


  • Registered Users, Registered Users 2 Posts: 2,082 ✭✭✭Tobias Greeshman


    Jiri wrote: »
    I guess he meant not overriding new operator, but just dynamically alocate each item in list.
    On re-reading that there, I'd say you're correct.

    Really shouldn't be replying to programming posts so late on a Sunday night! :)


Advertisement