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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Heap ,Stack allocation question [Solved]

Heap ,Stack allocation question [Solved]

Scheduled Pinned Locked Moved C / C++ / MFC
questiondata-structures
10 Posts 3 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.
  • E Offline
    E Offline
    econy
    wrote on last edited by
    #1

    Please read the pseudo code as follows:

    class A
    {
    CMap myMap;
    A() {
    myMap.InitHashTable(857);
    }
    };

    void test(void)
    {
    A *pa = new A();
    }

    My question is, myMap member in A class is in Heap or Stack? is destructor of A class need to delete myMap object expressly?

    L 1 Reply Last reply
    0
    • E econy

      Please read the pseudo code as follows:

      class A
      {
      CMap myMap;
      A() {
      myMap.InitHashTable(857);
      }
      };

      void test(void)
      {
      A *pa = new A();
      }

      My question is, myMap member in A class is in Heap or Stack? is destructor of A class need to delete myMap object expressly?

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

      The actual memeory used by the Map is allocated internally and dynamically, so it will be on the Heap. You may need to delete the map in your destructor, it depends on the lifetime of your object.

      E 2 Replies Last reply
      0
      • L Lost User

        The actual memeory used by the Map is allocated internally and dynamically, so it will be on the Heap. You may need to delete the map in your destructor, it depends on the lifetime of your object.

        E Offline
        E Offline
        econy
        wrote on last edited by
        #3

        So, the myMap memory allocation is dominated by it's container class A, right? for pseudo code like the following:

        void test() //A global function
        {
        CMap myMap;

        }

        then I dont need to delete myMap expressly, right?myMap should be in function stack.

        L 1 Reply Last reply
        0
        • E econy

          So, the myMap memory allocation is dominated by it's container class A, right? for pseudo code like the following:

          void test() //A global function
          {
          CMap myMap;

          }

          then I dont need to delete myMap expressly, right?myMap should be in function stack.

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

          That is correct, as far as I recall; MFC classes clean up after themselves.

          1 Reply Last reply
          0
          • L Lost User

            The actual memeory used by the Map is allocated internally and dynamically, so it will be on the Heap. You may need to delete the map in your destructor, it depends on the lifetime of your object.

            E Offline
            E Offline
            econy
            wrote on last edited by
            #5

            I try to use delete myMap, IDE shows cannot convert from 'CMap' to 'Void*'.

            L 1 Reply Last reply
            0
            • E econy

              I try to use delete myMap, IDE shows cannot convert from 'CMap' to 'Void*'.

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

              You need to show us your code, we cannot guess what you have done.

              E 1 Reply Last reply
              0
              • L Lost User

                You need to show us your code, we cannot guess what you have done.

                E Offline
                E Offline
                econy
                wrote on last edited by
                #7

                class A
                {
                private:
                A m_Map;
                public :
                A();
                ~A();
                };

                //Constructor
                A::A()
                {
                m_Map.InitHashTable(MAP_LEN);
                }

                A:~A()
                {
                delete m_ConfigMap; //This statement triggered error message
                }

                error C2440: 'delete' : cannot convert from 'CMapStringToString' to 'void *'

                Richard Andrew x64R L 2 Replies Last reply
                0
                • E econy

                  class A
                  {
                  private:
                  A m_Map;
                  public :
                  A();
                  ~A();
                  };

                  //Constructor
                  A::A()
                  {
                  m_Map.InitHashTable(MAP_LEN);
                  }

                  A:~A()
                  {
                  delete m_ConfigMap; //This statement triggered error message
                  }

                  error C2440: 'delete' : cannot convert from 'CMapStringToString' to 'void *'

                  Richard Andrew x64R Offline
                  Richard Andrew x64R Offline
                  Richard Andrew x64
                  wrote on last edited by
                  #8

                  I see two issues here: 1. Where is m_ConfigMap declared? 2. You have m_Map declared as an A. Is that what you meant?

                  The difficult we do right away... ...the impossible takes slightly longer.

                  E 1 Reply Last reply
                  0
                  • Richard Andrew x64R Richard Andrew x64

                    I see two issues here: 1. Where is m_ConfigMap declared? 2. You have m_Map declared as an A. Is that what you meant?

                    The difficult we do right away... ...the impossible takes slightly longer.

                    E Offline
                    E Offline
                    econy
                    wrote on last edited by
                    #9

                    sorry, yes, m_configMap should be m_Map.

                    1 Reply Last reply
                    0
                    • E econy

                      class A
                      {
                      private:
                      A m_Map;
                      public :
                      A();
                      ~A();
                      };

                      //Constructor
                      A::A()
                      {
                      m_Map.InitHashTable(MAP_LEN);
                      }

                      A:~A()
                      {
                      delete m_ConfigMap; //This statement triggered error message
                      }

                      error C2440: 'delete' : cannot convert from 'CMapStringToString' to 'void *'

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

                      That code makes no sense. You declare m_Map as an object of class A. You then try to call InitHashTable on it in the constructor, even though that function is not declared anywhere. Finally you try to delete m_ConfigMap which also is not declared anywhere.

                      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