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. Hashtables

Hashtables

Scheduled Pinned Locked Moved C / C++ / MFC
questionperformancehelplearning
16 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.
  • J Offline
    J Offline
    jblau
    wrote on last edited by
    #1

    I'm a beginner programmer with a question about hashtables. when I create a hashtable like so: Hashtable *hashTable = new Hashtable; Do I need to delete the hastahble in my destructor? It was my understanding from the stuff I read that you should always have a DELETE for every NEW that you use. But when I try to delete the hashtable, I receive an error message about an illegal delete. So do I need to delete them somehow to avoid a memory leak? Thanks, Jody Blau

    R M J P J 6 Replies Last reply
    0
    • J jblau

      I'm a beginner programmer with a question about hashtables. when I create a hashtable like so: Hashtable *hashTable = new Hashtable; Do I need to delete the hastahble in my destructor? It was my understanding from the stuff I read that you should always have a DELETE for every NEW that you use. But when I try to delete the hashtable, I receive an error message about an illegal delete. So do I need to delete them somehow to avoid a memory leak? Thanks, Jody Blau

      R Offline
      R Offline
      RChin
      wrote on last edited by
      #2

      jblau wrote:

      It was my understanding from the stuff I read that you should always have a DELETE for every NEW that you use.

      That's right, if you are programming in C++! But I have a suspicious feeling that you are using C#, or a language that uses the .NET framework. These manages there own memory stack/heap, so deletes are redundant.


      I Dream of Absolute Zero

      J 1 Reply Last reply
      0
      • J jblau

        I'm a beginner programmer with a question about hashtables. when I create a hashtable like so: Hashtable *hashTable = new Hashtable; Do I need to delete the hastahble in my destructor? It was my understanding from the stuff I read that you should always have a DELETE for every NEW that you use. But when I try to delete the hashtable, I receive an error message about an illegal delete. So do I need to delete them somehow to avoid a memory leak? Thanks, Jody Blau

        M Offline
        M Offline
        Michael Dunn
        wrote on last edited by
        #3

        Are you using normal or managed C++? Are you getting a compiler error? Post your code and the errors. --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ "Just because the box has 2 gigabytes of memory doesn't mean you get to use it all!"   -- Rico Mariani, CLR perf guy

        P 1 Reply Last reply
        0
        • M Michael Dunn

          Are you using normal or managed C++? Are you getting a compiler error? Post your code and the errors. --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ "Just because the box has 2 gigabytes of memory doesn't mean you get to use it all!"   -- Rico Mariani, CLR perf guy

          P Offline
          P Offline
          Prakash Nadar
          wrote on last edited by
          #4

          Michael Dunn wrote:

          Are you using normal or managed C++?

          I would like to see the abnormal ones :laugh:


          -Prakash

          M 1 Reply Last reply
          0
          • R RChin

            jblau wrote:

            It was my understanding from the stuff I read that you should always have a DELETE for every NEW that you use.

            That's right, if you are programming in C++! But I have a suspicious feeling that you are using C#, or a language that uses the .NET framework. These manages there own memory stack/heap, so deletes are redundant.


            I Dream of Absolute Zero

            J Offline
            J Offline
            John R Shaw
            wrote on last edited by
            #5

            Hold it right there! Delete is not redundant. Mangaged just means that if you do not clean up after your self, then it will eventualy be clean up for you. Not cleaning up after your self, is a very bad idea. INTP Every thing is relative...

            1 Reply Last reply
            0
            • J jblau

              I'm a beginner programmer with a question about hashtables. when I create a hashtable like so: Hashtable *hashTable = new Hashtable; Do I need to delete the hastahble in my destructor? It was my understanding from the stuff I read that you should always have a DELETE for every NEW that you use. But when I try to delete the hashtable, I receive an error message about an illegal delete. So do I need to delete them somehow to avoid a memory leak? Thanks, Jody Blau

              J Offline
              J Offline
              John R Shaw
              wrote on last edited by
              #6

              It is true that for every NEW you need to call DELETE. The only reason that you should receive an error, is that it was already deleted. That implies an error in your coding, becuase if the the pointer to the hash-table is a member of your class, then the memory-block should exist until the class object is destoryed. If there is the slitest chance that the memory pointed-to by the pointer might be be dealocated by another function, then you should set the pointer to NULL after you deallocate it (delete does nothing if the pointer is NULL). The only problem with this, is that it hides the fact that you are trying to delete the same memory-block two or more times. My recommendation is that you put a trace (or out-put it to a file) statement after every NEW and DELETE associated with the the hash-table. That way you will know when it is allocated and when it is destoryed. If the NEWs and DELETEs do not match up, then you will know that some thing else is going on. INTP Every thing is relative...

              1 Reply Last reply
              0
              • J jblau

                I'm a beginner programmer with a question about hashtables. when I create a hashtable like so: Hashtable *hashTable = new Hashtable; Do I need to delete the hastahble in my destructor? It was my understanding from the stuff I read that you should always have a DELETE for every NEW that you use. But when I try to delete the hashtable, I receive an error message about an illegal delete. So do I need to delete them somehow to avoid a memory leak? Thanks, Jody Blau

                P Offline
                P Offline
                Prakash Nadar
                wrote on last edited by
                #7

                I hope u didnt say delete Hashtable it should be delete hashTable its kinda not rite to name the variable name same as the datatype with difference in the case, Sometimes it creates hard to understand compiler errors, like this one.


                -Prakash

                1 Reply Last reply
                0
                • J jblau

                  I'm a beginner programmer with a question about hashtables. when I create a hashtable like so: Hashtable *hashTable = new Hashtable; Do I need to delete the hastahble in my destructor? It was my understanding from the stuff I read that you should always have a DELETE for every NEW that you use. But when I try to delete the hashtable, I receive an error message about an illegal delete. So do I need to delete them somehow to avoid a memory leak? Thanks, Jody Blau

                  J Offline
                  J Offline
                  jblau
                  wrote on last edited by
                  #8

                  wow, I am surprised at how many people responded, thanks everyone! Here is some more information which might allow you guys to set me straight. I am using Visual Studio.net 2003 The type of project that I opened is a "Windows Form Application (.Net)" (so I don't know if that means that I am using "Normal" or "Managed" C++ ?) My code is something like this: void LoadLists() { Hashtable *hashSelectedItems = new Hashtable; ......(code working with the hashtable) delete hashSelectedItems; hashSelectedItems = NULL; } When I try to build the project, I get this error message: error C3841: illegal delete expression: managed type 'System::Collections::Hashtable' does not have a destructor defined I'm thinking that perhaps it is, as one of you mentioned, being automatically managed, but I'm not certain. It was my impression that if I wanted to use a Managed object I would specifiy so in the declaration, for example, in declaring an array, if I wanted it to be a managed array I would declare it like so: int c __gc[] = new int __gc[12]; but if I didn't want it Managed, I would leave out the __gc part. ************So given all of this, what do I need to know about declaring Hashtables without leaving any memory leaks? Thanks, Jody Blau

                  J P M 3 Replies Last reply
                  0
                  • J jblau

                    wow, I am surprised at how many people responded, thanks everyone! Here is some more information which might allow you guys to set me straight. I am using Visual Studio.net 2003 The type of project that I opened is a "Windows Form Application (.Net)" (so I don't know if that means that I am using "Normal" or "Managed" C++ ?) My code is something like this: void LoadLists() { Hashtable *hashSelectedItems = new Hashtable; ......(code working with the hashtable) delete hashSelectedItems; hashSelectedItems = NULL; } When I try to build the project, I get this error message: error C3841: illegal delete expression: managed type 'System::Collections::Hashtable' does not have a destructor defined I'm thinking that perhaps it is, as one of you mentioned, being automatically managed, but I'm not certain. It was my impression that if I wanted to use a Managed object I would specifiy so in the declaration, for example, in declaring an array, if I wanted it to be a managed array I would declare it like so: int c __gc[] = new int __gc[12]; but if I didn't want it Managed, I would leave out the __gc part. ************So given all of this, what do I need to know about declaring Hashtables without leaving any memory leaks? Thanks, Jody Blau

                    J Offline
                    J Offline
                    John R Shaw
                    wrote on last edited by
                    #9

                    I do not work with managed any thing, but I can tell you where you made a mistake. Do not allocate an object and delete an object in the same function, unless you have to. Instead you should just declare it as a stack object and let it clean up after its self.

                    void LoadLists()
                    {
                    Hashtable hashSelectedItems;
                    ......(code working with the hashtable)
                    }

                    The above code does not require a delete, becuase no new was called. The 'hashSelectedItems' variable is on the stack and will clean up after its self when the function returns. INTP Every thing is relative...

                    G 1 Reply Last reply
                    0
                    • J John R Shaw

                      I do not work with managed any thing, but I can tell you where you made a mistake. Do not allocate an object and delete an object in the same function, unless you have to. Instead you should just declare it as a stack object and let it clean up after its self.

                      void LoadLists()
                      {
                      Hashtable hashSelectedItems;
                      ......(code working with the hashtable)
                      }

                      The above code does not require a delete, becuase no new was called. The 'hashSelectedItems' variable is on the stack and will clean up after its self when the function returns. INTP Every thing is relative...

                      G Offline
                      G Offline
                      Gary R Wheeler
                      wrote on last edited by
                      #10

                      John R. Shaw wrote:

                      Do not allocate an object and delete an object in the same function, unless you have to

                      Why? That's a perfectly normal thing to do, especially when the object in question has some runtime-determined characteristic (e.g. a non-trivial constructor). It's appropriate if you don't need the object for the entire life of the function. Also, if the object is large, you tend to get better working set behavior if you allocate it from the heap rather than the stack.


                      Software Zen: delete this;

                      Fold With Us![^]

                      J 1 Reply Last reply
                      0
                      • J jblau

                        wow, I am surprised at how many people responded, thanks everyone! Here is some more information which might allow you guys to set me straight. I am using Visual Studio.net 2003 The type of project that I opened is a "Windows Form Application (.Net)" (so I don't know if that means that I am using "Normal" or "Managed" C++ ?) My code is something like this: void LoadLists() { Hashtable *hashSelectedItems = new Hashtable; ......(code working with the hashtable) delete hashSelectedItems; hashSelectedItems = NULL; } When I try to build the project, I get this error message: error C3841: illegal delete expression: managed type 'System::Collections::Hashtable' does not have a destructor defined I'm thinking that perhaps it is, as one of you mentioned, being automatically managed, but I'm not certain. It was my impression that if I wanted to use a Managed object I would specifiy so in the declaration, for example, in declaring an array, if I wanted it to be a managed array I would declare it like so: int c __gc[] = new int __gc[12]; but if I didn't want it Managed, I would leave out the __gc part. ************So given all of this, what do I need to know about declaring Hashtables without leaving any memory leaks? Thanks, Jody Blau

                        P Offline
                        P Offline
                        Prakash Nadar
                        wrote on last edited by
                        #11

                        check http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemcollectionshashtableclasstopic.asp[^] how the hashtable is used.


                        -Prakash

                        1 Reply Last reply
                        0
                        • P Prakash Nadar

                          Michael Dunn wrote:

                          Are you using normal or managed C++?

                          I would like to see the abnormal ones :laugh:


                          -Prakash

                          M Offline
                          M Offline
                          Michael Dunn
                          wrote on last edited by
                          #12

                          Some people considered the "Managed Extensions for C++" to be abnormal. ;) --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ You cannot stop me with paramecium alone!

                          1 Reply Last reply
                          0
                          • J jblau

                            wow, I am surprised at how many people responded, thanks everyone! Here is some more information which might allow you guys to set me straight. I am using Visual Studio.net 2003 The type of project that I opened is a "Windows Form Application (.Net)" (so I don't know if that means that I am using "Normal" or "Managed" C++ ?) My code is something like this: void LoadLists() { Hashtable *hashSelectedItems = new Hashtable; ......(code working with the hashtable) delete hashSelectedItems; hashSelectedItems = NULL; } When I try to build the project, I get this error message: error C3841: illegal delete expression: managed type 'System::Collections::Hashtable' does not have a destructor defined I'm thinking that perhaps it is, as one of you mentioned, being automatically managed, but I'm not certain. It was my impression that if I wanted to use a Managed object I would specifiy so in the declaration, for example, in declaring an array, if I wanted it to be a managed array I would declare it like so: int c __gc[] = new int __gc[12]; but if I didn't want it Managed, I would leave out the __gc part. ************So given all of this, what do I need to know about declaring Hashtables without leaving any memory leaks? Thanks, Jody Blau

                            M Offline
                            M Offline
                            Michael Dunn
                            wrote on last edited by
                            #13

                            jblau wrote:

                            illegal delete expression: managed type 'System::Collections::Hashtable' does not have a destructor defined

                            OK, that shows that you're creating a managed object. Don't delete it, the CLR garbage collector will take care of that for you. The reason you don't need __gc in the declaration is the compiler infers it from the context - Hashtable is a managed object so __gc is the only possibility. Yes, this is confusing and it's the main reason why the MC++ syntax was replaced with C++/CLI in VC8. --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ "That probably would've sounded more commanding if I wasn't wearing my yummy sushi pajamas."   -- Buffy

                            1 Reply Last reply
                            0
                            • G Gary R Wheeler

                              John R. Shaw wrote:

                              Do not allocate an object and delete an object in the same function, unless you have to

                              Why? That's a perfectly normal thing to do, especially when the object in question has some runtime-determined characteristic (e.g. a non-trivial constructor). It's appropriate if you don't need the object for the entire life of the function. Also, if the object is large, you tend to get better working set behavior if you allocate it from the heap rather than the stack.


                              Software Zen: delete this;

                              Fold With Us![^]

                              J Offline
                              J Offline
                              John R Shaw
                              wrote on last edited by
                              #14

                              Gary R. Wheeler wrote:

                              Why?

                              It is like doing this: char* buffer = new char[32]; when you should be doing this: char buffer[32]; The allocation in this case is not a normal thing to do. Another example:

                              class MyClass
                              {
                              int n1,n2;
                              public:
                              // member operations
                              ......
                              };
                              // function using class
                              void MyFunc1()
                              {
                              // this makes no since (sizeof(MyClass) is only 8 bytes)
                              MyClass* mc = new MyClass;
                              ......
                              delete mc;
                              }
                              // function using class
                              void MyFunc2()
                              {
                              // this makes more since
                              MyClass mc;
                              ......
                              }

                              Gary R. Wheeler wrote:

                              It's appropriate if you don't need the object for the entire life of the function.

                              Insted of allocating it, you can use scoping. The pratice of using scoping to limit an objects life time is common and is used in the MFC library its self.

                              void MyFunc()
                              {
                              ......
                              {
                              MyClass mc;
                              ......
                              } // class is destoyed automatically here
                              ......
                              }

                              When you allocate unnecessarily, you introduce one more factor that can go wrong. INTP Every thing is relative...

                              1 Reply Last reply
                              0
                              • J jblau

                                I'm a beginner programmer with a question about hashtables. when I create a hashtable like so: Hashtable *hashTable = new Hashtable; Do I need to delete the hastahble in my destructor? It was my understanding from the stuff I read that you should always have a DELETE for every NEW that you use. But when I try to delete the hashtable, I receive an error message about an illegal delete. So do I need to delete them somehow to avoid a memory leak? Thanks, Jody Blau

                                J Offline
                                J Offline
                                jblau
                                wrote on last edited by
                                #15

                                Thanks for all of the helpful information. As I understand it now, the way my project is setup, the hashtable is a managed object, so I can't delete it mannualy, (as I get errors when I try). So my follow up question is this: If I store an object that was declared with a NEW, and store it in the hashtable, do I need to go back through the hashtable and delete those objects, or does the managed code do that as well? Example: MyObject *tempObject = new MyObject; Hashtable *hashObjects = new Hashtable; hashObjects->Add(key, tempObject); So if I store several tempObjects in my hashtable, do I need to traverse through the hashtable and DELETE the tempObjects, or does the garbage collector handle this as well as the hashtable itself? If I do need to delete them, will a call to hashObjects->clear(); delete the objects? Thanks, Jody Blau -- modified at 15:53 Sunday 1st January, 2006

                                P 1 Reply Last reply
                                0
                                • J jblau

                                  Thanks for all of the helpful information. As I understand it now, the way my project is setup, the hashtable is a managed object, so I can't delete it mannualy, (as I get errors when I try). So my follow up question is this: If I store an object that was declared with a NEW, and store it in the hashtable, do I need to go back through the hashtable and delete those objects, or does the managed code do that as well? Example: MyObject *tempObject = new MyObject; Hashtable *hashObjects = new Hashtable; hashObjects->Add(key, tempObject); So if I store several tempObjects in my hashtable, do I need to traverse through the hashtable and DELETE the tempObjects, or does the garbage collector handle this as well as the hashtable itself? If I do need to delete them, will a call to hashObjects->clear(); delete the objects? Thanks, Jody Blau -- modified at 15:53 Sunday 1st January, 2006

                                  P Offline
                                  P Offline
                                  Prakash Nadar
                                  wrote on last edited by
                                  #16

                                  Try posting this as a new question in the forum so that others can also look at it.


                                  -Prakash

                                  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