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. Declare member variable on stack or heap

Declare member variable on stack or heap

Scheduled Pinned Locked Moved C / C++ / MFC
data-structures
10 Posts 4 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.
  • _ Offline
    _ Offline
    _Flaviu
    wrote on last edited by
    #1

    How is more efficient to have, a CMap member variable created on the stack, or on the heap, even the values of CMap are just pointers:

    CMap m_MapContactInfo;

    or

    CMap* m_pMapContactInfo;

    knowing that this CMap could contain thousands of elements ...

    L V 2 Replies Last reply
    0
    • _ _Flaviu

      How is more efficient to have, a CMap member variable created on the stack, or on the heap, even the values of CMap are just pointers:

      CMap m_MapContactInfo;

      or

      CMap* m_pMapContactInfo;

      knowing that this CMap could contain thousands of elements ...

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

      Probably best on the heap if it contains large amounts of data. You also need to consider scope, i.e. how long will the object need to remain in memory?

      _ 1 Reply Last reply
      0
      • L Lost User

        Probably best on the heap if it contains large amounts of data. You also need to consider scope, i.e. how long will the object need to remain in memory?

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

        "how long will the object need to remain in memory?" As long as object that contain m_pMapContactInfo are alive ... I mean:

        CMyObject::CMyObject()
        {
        m_pMapContactInfo = new CMap;
        }

        and

        CMyObject::~CMyObject()
        {
        if(NULL != m_pMapContactInfo)
        delete m_pMapContactInfo;
        }

        L 1 Reply Last reply
        0
        • _ _Flaviu

          "how long will the object need to remain in memory?" As long as object that contain m_pMapContactInfo are alive ... I mean:

          CMyObject::CMyObject()
          {
          m_pMapContactInfo = new CMap;
          }

          and

          CMyObject::~CMyObject()
          {
          if(NULL != m_pMapContactInfo)
          delete m_pMapContactInfo;
          }

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

          Flaviu2 wrote:

          As long as object that contain m_pMapContactInfo are alive

          Only you know how long that will be.

          _ 1 Reply Last reply
          0
          • _ _Flaviu

            How is more efficient to have, a CMap member variable created on the stack, or on the heap, even the values of CMap are just pointers:

            CMap m_MapContactInfo;

            or

            CMap* m_pMapContactInfo;

            knowing that this CMap could contain thousands of elements ...

            V Offline
            V Offline
            Victor Nijegorodov
            wrote on last edited by
            #5

            CMap class creates its content data in the heap. It is by design. So you can create your CMap instance on the stack and not worry!

            _ H 2 Replies Last reply
            0
            • V Victor Nijegorodov

              CMap class creates its content data in the heap. It is by design. So you can create your CMap instance on the stack and not worry!

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

              "CMap class creates its content data in the heap. It is by design." This is a good news ! (I didn't read that by now) ... thank you.

              1 Reply Last reply
              0
              • L Lost User

                Flaviu2 wrote:

                As long as object that contain m_pMapContactInfo are alive

                Only you know how long that will be.

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

                In fact, the m_pMapContactInfo are member variable into CView derived class ... so, as long the view are opened, the m_pMapContactInfo exist ... but what is matter how long the CMap object are exist ?

                L 1 Reply Last reply
                0
                • _ _Flaviu

                  In fact, the m_pMapContactInfo are member variable into CView derived class ... so, as long the view are opened, the m_pMapContactInfo exist ... but what is matter how long the CMap object are exist ?

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

                  It only matters in that you need to place the CMap object, and all its members, in the correct place to ensure it does not get destroyed at the wrong time. The fact that you posted this question suggests that you were not sure.

                  1 Reply Last reply
                  0
                  • V Victor Nijegorodov

                    CMap class creates its content data in the heap. It is by design. So you can create your CMap instance on the stack and not worry!

                    H Offline
                    H Offline
                    Hometurph Indi
                    wrote on last edited by
                    #9

                    i am still not having a perfect solution with both options Deep Cleaning Delhi Home Cleaning Delhi

                    V 1 Reply Last reply
                    0
                    • H Hometurph Indi

                      i am still not having a perfect solution with both options Deep Cleaning Delhi Home Cleaning Delhi

                      V Offline
                      V Offline
                      Victor Nijegorodov
                      wrote on last edited by
                      #10

                      Hometurph Indi wrote:

                      i am still not having a perfect solution with both options

                      Define perfect.:cool:

                      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