Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. CStringList on heap

CStringList on heap

Scheduled Pinned Locked Moved C / C++ / MFC
data-structuresquestion
13 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • _ _Flaviu

    Is there any point to create a CStringList class member on heap (with new) if this member has a lot of elements ? Or is enough to declare it on stack ?

    CPalliniC Offline
    CPalliniC Offline
    CPallini
    wrote on last edited by
    #2

    Sorry? What are you trying to do, really? Could you please provide an example?

    In testa che avete, signor di Ceprano?

    _ 1 Reply Last reply
    0
    • _ _Flaviu

      Is there any point to create a CStringList class member on heap (with new) if this member has a lot of elements ? Or is enough to declare it on stack ?

      M Offline
      M Offline
      Maximilien
      wrote on last edited by
      #3

      no point, really. Anyway, stop using CStringList and use a std::vector instead.

      I'd rather be phishing!

      _ 1 Reply Last reply
      0
      • CPalliniC CPallini

        Sorry? What are you trying to do, really? Could you please provide an example?

        _ Offline
        _ Offline
        _Flaviu
        wrote on last edited by
        #4

        // header
        protected:
        CStringList m_List1;
        CStringList m_List2;
        CStringList m_List3;
        CStringList m_List4;

        COutputWnd::COutputWnd() noexcept
        {
        m_List1.AddTail(_T("Item 1"));
        m_List1.AddTail(_T("Item 1"));
        m_List1.AddTail(_T("Item 1"));
        m_List1.AddTail(_T("Item 1"));
        m_List1.AddTail(_T("Item 1"));
        m_List1.AddTail(_T("Item 1"));

        m\_List2.AddTail(\_T("Item 2"));
        m\_List2.AddTail(\_T("Item 2"));
        m\_List2.AddTail(\_T("Item 2"));
        m\_List2.AddTail(\_T("Item 3"));
        m\_List2.AddTail(\_T("Item 55"));
        m\_List2.AddTail(\_T("Item 44"));
        

        ....
        }

        or should I do a CSringList* m_pList1, and in c-tor I create m_pList1 = new CStringList ? That was the question.

        CPalliniC 1 Reply Last reply
        0
        • M Maximilien

          no point, really. Anyway, stop using CStringList and use a std::vector instead.

          I'd rather be phishing!

          _ Offline
          _ Offline
          _Flaviu
          wrote on last edited by
          #5

          Thank you. I would let this CStringList as it is now. I still have a question: Why should I use std::vector instead of CStringList ? Is there a plus ? Please note that my app is VC++/MFC, and CStringList is part of MFC.

          L S CPalliniC 3 Replies Last reply
          0
          • _ _Flaviu

            Thank you. I would let this CStringList as it is now. I still have a question: Why should I use std::vector instead of CStringList ? Is there a plus ? Please note that my app is VC++/MFC, and CStringList is part of MFC.

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #6

            If you are using MFC then it is fine to use its classes. However for most cases it is better to allocate most objects on the heap. Save the stack for values, pointers and very short term objects,

            1 Reply Last reply
            0
            • _ _Flaviu

              Thank you. I would let this CStringList as it is now. I still have a question: Why should I use std::vector instead of CStringList ? Is there a plus ? Please note that my app is VC++/MFC, and CStringList is part of MFC.

              S Offline
              S Offline
              Stefan_Lang
              wrote on last edited by
              #7

              In my experience, if there is a class for your purpose to be found in STL, then it's much better to use that than a corresponding MFC class. The only exception is when you need the MFC structure to interface with a function from a third party library. (e. g. to pass it as a function argument) The 'plus' is that - you could port the code to any platform that does not have or use MFC - you can use other third party libraries that don't use MFC (e. g. most third party libraries using strings in their API will use std::string, not CString) - it's easy to find plenty of documentation and tutorials on the web, and they are all accurate, because existing behaviour of STL types never changes - the types and functions will work very efficiently and not carry around needless ballast related to Windows stuff - your global namespace doesn't get cluttered with symbols and macros - you don't suffer from header dependencies, and don't need to care about order of include statements The 'minus' is that - for some types the STL equivalent provides less utility functions (e. g. std::string vs. CString) - if you're already using MFC types elsewhere and don't intend to change all of them, you may need to convert data between MFC- and non-MFC types. As for using std::vector instead of std::list, the reason is that as long as you don't delete items in the mid or at the start of the vector, it is much faster than a list.

              GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)

              J 1 Reply Last reply
              0
              • _ _Flaviu

                // header
                protected:
                CStringList m_List1;
                CStringList m_List2;
                CStringList m_List3;
                CStringList m_List4;

                COutputWnd::COutputWnd() noexcept
                {
                m_List1.AddTail(_T("Item 1"));
                m_List1.AddTail(_T("Item 1"));
                m_List1.AddTail(_T("Item 1"));
                m_List1.AddTail(_T("Item 1"));
                m_List1.AddTail(_T("Item 1"));
                m_List1.AddTail(_T("Item 1"));

                m\_List2.AddTail(\_T("Item 2"));
                m\_List2.AddTail(\_T("Item 2"));
                m\_List2.AddTail(\_T("Item 2"));
                m\_List2.AddTail(\_T("Item 3"));
                m\_List2.AddTail(\_T("Item 55"));
                m\_List2.AddTail(\_T("Item 44"));
                

                ....
                }

                or should I do a CSringList* m_pList1, and in c-tor I create m_pList1 = new CStringList ? That was the question.

                CPalliniC Offline
                CPalliniC Offline
                CPallini
                wrote on last edited by
                #8

                There is no point (as Maximilen alrady stated) in using the heap in such a scenario.

                In testa che avete, signor di Ceprano?

                1 Reply Last reply
                0
                • _ _Flaviu

                  Thank you. I would let this CStringList as it is now. I still have a question: Why should I use std::vector instead of CStringList ? Is there a plus ? Please note that my app is VC++/MFC, and CStringList is part of MFC.

                  CPalliniC Offline
                  CPalliniC Offline
                  CPallini
                  wrote on last edited by
                  #9

                  Because STL containers are much better than the MFC ones.

                  In testa che avete, signor di Ceprano?

                  1 Reply Last reply
                  0
                  • _ _Flaviu

                    Is there any point to create a CStringList class member on heap (with new) if this member has a lot of elements ? Or is enough to declare it on stack ?

                    S Offline
                    S Offline
                    Stefan_Lang
                    wrote on last edited by
                    #10

                    _Flaviu wrote:

                    ... if this member has a lot of elements

                    Just to make a point: the elements of a container do not affect how the container itself looks like, or how it should be allocated. This is true for all STL containers, and also for CStringList. Containers are typically designed like this: one central data structure to store the basic info about the container, and maybe it's state, and some kind of reference (or two) to the data structure(s) containing the elements. The container will take care about allocating memory for the elements on the heap. But the central structure will never need to be reallocated, nor is it very large. Therefore it is no problem at all to allocate a container on the stack. That said, if you pass a container to a function, you should normally pass it by reference. If you don't, the container will be copied as a whole, including all the elements!

                    GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)

                    _ 1 Reply Last reply
                    0
                    • S Stefan_Lang

                      _Flaviu wrote:

                      ... if this member has a lot of elements

                      Just to make a point: the elements of a container do not affect how the container itself looks like, or how it should be allocated. This is true for all STL containers, and also for CStringList. Containers are typically designed like this: one central data structure to store the basic info about the container, and maybe it's state, and some kind of reference (or two) to the data structure(s) containing the elements. The container will take care about allocating memory for the elements on the heap. But the central structure will never need to be reallocated, nor is it very large. Therefore it is no problem at all to allocate a container on the stack. That said, if you pass a container to a function, you should normally pass it by reference. If you don't, the container will be copied as a whole, including all the elements!

                      GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)

                      _ Offline
                      _ Offline
                      _Flaviu
                      wrote on last edited by
                      #11

                      Thank you for your explanation !

                      1 Reply Last reply
                      0
                      • S Stefan_Lang

                        In my experience, if there is a class for your purpose to be found in STL, then it's much better to use that than a corresponding MFC class. The only exception is when you need the MFC structure to interface with a function from a third party library. (e. g. to pass it as a function argument) The 'plus' is that - you could port the code to any platform that does not have or use MFC - you can use other third party libraries that don't use MFC (e. g. most third party libraries using strings in their API will use std::string, not CString) - it's easy to find plenty of documentation and tutorials on the web, and they are all accurate, because existing behaviour of STL types never changes - the types and functions will work very efficiently and not carry around needless ballast related to Windows stuff - your global namespace doesn't get cluttered with symbols and macros - you don't suffer from header dependencies, and don't need to care about order of include statements The 'minus' is that - for some types the STL equivalent provides less utility functions (e. g. std::string vs. CString) - if you're already using MFC types elsewhere and don't intend to change all of them, you may need to convert data between MFC- and non-MFC types. As for using std::vector instead of std::list, the reason is that as long as you don't delete items in the mid or at the start of the vector, it is much faster than a list.

                        GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)

                        J Offline
                        J Offline
                        jschell
                        wrote on last edited by
                        #12

                        Stefan_Lang wrote:

                        - you could port the code to any platform that does not have or use MFC

                        In my experience in non-trivial (legacy) applications that possibility does not exist unless one refactors the entire code base. That of course precludes those cases where the application, from its inception, was written to support multiple platforms.

                        S 1 Reply Last reply
                        0
                        • J jschell

                          Stefan_Lang wrote:

                          - you could port the code to any platform that does not have or use MFC

                          In my experience in non-trivial (legacy) applications that possibility does not exist unless one refactors the entire code base. That of course precludes those cases where the application, from its inception, was written to support multiple platforms.

                          S Offline
                          S Offline
                          Stefan_Lang
                          wrote on last edited by
                          #13

                          Nowadays it's not nearly as bad as it used to be. The biggest culprit was and still is the UI, and in fact any graphics or sound functionality, simply because there are no C++ standard libraries for that. However, there are OS independend libraries around for that purpose. Everything that is in the C++ standard libraries, including STL and BOOST, should work out of the box on any OS without changing the code, unless the compiler you're using is not adhering to the standard (in which case you should consult your compilers compatibility documentation).

                          GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups