CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    May 2009
    Location
    Boston
    Posts
    364

    delete vector of objects, single objects, dealocate menory

    I have been reading around a bit and there seems to be no straightforward way to do this.

    I have a global vector of objects and a single object that I need to delete as part of the re-initialization of a function that use the global data. Something like,
    Code:
    class dataSet {
    public:
       // initialize class members
       dataSet ()
          : max(0),
            numA(0),
            numB(0),
            numR(0) { }
    
       int max, numA, numB, numR;
    };
    
    class CurrentInfo {
    public:
       // initialize class members
       CurrentInfo()
          : aES(0.0), aHES(0.0), saES(0.0),
            aNum(0), atNum(0), aType(0), delta(0),
            hbd(false), hba(false) { }
    
       bool hbd, hba;
    
       int aNum, atNum, aType, delta;
    
       float aES, aHES, saES;
    
       string ordLookup;
       vector<int> nNums, nOrds, distance;
    };
    These have global declarations,
    Code:
    extern vector<CurrentInfo> current;
    extern dataSet newSet;
    There is another function that needs to reset these structures in preparation for new data.

    I think I can do something like,
    Code:
       vector<CurrentInfo>().swap(current); // will this work ???
    to get rid of the vector of objects, but I'm not sure how to get rid of the single object.

    It seems as if there should be a simple command to delete and de-allocate memory that would go along with new. These objects will be re-created later, so it is possible that I could just remove the data and leave the object in place to be filled with new data.

    current.erase(current.begin(), current.end());

    I am in a complicated bit of debugging right now and it would be much easier for the present to just make them go away.

    LMHmedchem
    Last edited by LMHmedchem; August 1st, 2012 at 08:27 PM.

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: delete vector of objects, single objects, dealocate menory

    Quote Originally Posted by LMHmedchem
    I think I can do something like,
    Code:
    vector<CurrentInfo>().swap(current); // will this work ???
    to get rid of the vector of objects
    Yes, but that does not get rid of the vector of objects; it just swaps it with an empty vector of objects, hence setting it to the default state.

    Quote Originally Posted by LMHmedchem
    I'm not sure how to get rid of the single object.
    It depends on what you want to do. For example, maybe all you need to do is to assign a default constructed object, e.g.,
    Code:
    x = dataSet();
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    May 2009
    Location
    Boston
    Posts
    364

    Re: delete vector of objects, single objects, dealocate menory

    Quote Originally Posted by laserlight View Post
    It depends on what you want to do. For example, maybe all you need to do is to assign a default constructed object, e.g.,
    Code:
    x = dataSet();
    I think this is what I need to do in the end for the single object. It is declared as a global,
    Code:
    extern dataSet newSet;
    and then data is just assigned,
    Code:
       newSet.max = *MAXNVX;
       newSet.numA = *NAT;
       //etc...
    Is there a way to re-construct this to default initialized values, or does it make more sense to just initialize by assignment,

    Code:
       newSet.max = 0;
       newSet.numA = 0;
       //etc...
    I afraid I don't know much about how the constructors for these things work.

    When the program starts, these global objects will be initialized by the constructor. The individual variables in the objects are all initialized to 0/false. The strings and vectors are empty and have no size. As the program loops, these structures will need to be cleared for the next loop. If they were local scope in a function, they would just go away and I wouldn't worry about it. Since they are global, it seems to be a bit more complicated.

    Right now the plan is to reassign the individual variables to 0/false and use erase on the vectors. That seems to be a bit crude, so I am assuming there is a better way. I feel as if I am being a bit unclear, so please let me know.

    LMHmedchem

  4. #4
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: delete vector of objects, single objects, dealocate menory

    Quote Originally Posted by LMHmedchem
    Is there a way to re-construct this to default initialized values, or does it make more sense to just initialize by assignment,
    Assigning a default constructed objects should be fine here. If you prefer, you could provide a "reset" member function that does the same thing.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  5. #5
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: delete vector of objects, single objects, dealocate menory

    Quote Originally Posted by LMHmedchem View Post
    As the program loops, these structures will need to be cleared for the next loop. If they were local scope in a function, they would just go away and I wouldn't worry about it. Since they are global, it seems to be a bit more complicated.
    It sounds like you have a very bad design. Why not just get rid of the global variables?
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  6. #6
    Join Date
    May 2009
    Location
    Boston
    Posts
    364

    Re: delete vector of objects, single objects, dealocate menory

    Quote Originally Posted by laserlight View Post
    Assigning a default constructed objects should be fine here. If you prefer, you could provide a "reset" member function that does the same thing.
    I am a bit unclear on the syntax for doing this. Do you mean,
    Code:
    // in .h file
    extern dataSet newSet;
    
       // in src file
       dataSet newSet;
    
       // to re-initalize
       dataSet tempSet;
    
       newSet = tempSet;
    This is what is happening by doing newSet = dataSet();, more or less? It seems like this makes more sense than a class function that re-initializes everything, which would make more sense if there was some data that needed to be re-initialized and some that did not.

    Does my copy constructor need to have a default value for all the member variables? What happens to the string and vector variables if they aren't initialized to a default value? It seems like memory would have been allocated to those containers as they were populated. Will assigning a default constructed object clear and de-alocate that memory?

    Quote Originally Posted by D_Drmmr View Post
    It sounds like you have a very bad design. Why not just get rid of the global variables?
    This is a complex program in that it has code in c, c+, and Fortran 77. The main subroutine is in Fortran and I added some c functions to do things that are especially cumbersome in Fortran (??? what isn't). There is allot of data that is processed by the Fortran and there are calls to cpp functions that carry over data while it is in scope. Since these calls are made many times, data cannot be accumulated in the cpp functions. Since Fortran has no notion of objects, the data structures cannot be declared in the Fortran and passed to the cpp code. The only option was to have the cpp code accumulate the data in global variables. This data is accumulated by some cpp functions during part of the Fortran program and then used by other cpp functions later. The data is also used from a number of locations in the program, so I don't know at what point it makes sense to use a global instead of something local that gets passed around. The entire program is single threaded and very serial, so there are no thread safe issues regarding a need to mutex the global data and keep different threads from fighting over it or accessing it in the wrong order.

    LMHmedchem
    Last edited by LMHmedchem; August 2nd, 2012 at 12:41 PM.

  7. #7
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: delete vector of objects, single objects, dealocate menory

    Quote Originally Posted by LMHmedchem
    This is what is happening by doing newSet = dataSet();, more or less?
    Yes, except that having tempSet is unnecessary.

    Quote Originally Posted by LMHmedchem
    Does my copy constructor need to have a default value for all the member variables?
    Your copy constructor? I would expect the copy constructor to copy everything, except in special cases.

    Quote Originally Posted by LMHmedchem
    What happens to the string and vector variables if they aren't initialized to a default value.
    If you did not initialise them explicitly, they will be default constructed to empty string and empty vector, respectively.

    Quote Originally Posted by LMHmedchem
    Will assigning a default constructed object clear and de-alocate that memory?
    If you implemented the copy/move assignment operator correctly, or if the compiler generated one will suffice, yes.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  8. #8
    Join Date
    Apr 1999
    Posts
    27,449

    Re: delete vector of objects, single objects, dealocate menory

    Quote Originally Posted by LMHmedchem View Post
    Will assigning a default constructed object clear
    Yes.
    and de-alocate that memory?
    There is a difference between logically "clearing" an object, and what really happens underneath the hood when you clear an object (by "clearing", I mean removing elements from a container by whatever means, i.e. swap trick, calling clear(), etc.).

    There is no guarantee that any memory is deallocated when you clear an object -- that is the job of the internal heap manager (compiler and OS heap manager) as to what is actually deallocated. All that really matters is that you're using C++ properly.
    It seems as if there should be a simple command to delete and de-allocate memory that would go along with new
    In your first post, there is no call to the new operator, so bringing up "delete" in code that doesn't use "new" is not relevant. In other words, if you aren't doing any manual memory management, then what is there to worry about? Everything should work correctly with respect to memory leakage (meaning no memory leaks).
    The only option was to have the cpp code accumulate the data in global variables.
    If the data must be global, you should prefer using a C++ class of static data members instead of the 'C'-style of using extern. Using the former eliminates the need for extern, thereby you can #include the class in any module and not have the linker stating "multiply defined in module xxxx" errors.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; August 2nd, 2012 at 01:07 PM.

  9. #9
    Join Date
    May 2009
    Location
    Boston
    Posts
    364

    Re: delete vector of objects, single objects, dealocate menory

    Quote Originally Posted by Paul McKenzie View Post
    There is no guarantee that any memory is deallocated when you clear an object -- that is the job of the internal heap manager (compiler and OS heap manager) as to what is actually deallocated. All that really matters is that you're using C++ properly.

    In your first post, there is no call to the new operator, so bringing up "delete" in code that doesn't use "new" is not relevant. In other words, if you aren't doing any manual memory management, then what is there to worry about? Everything should work correctly with respect to memory leakage (meaning no memory leaks).
    I probably need to pay more attention to resource management than I do, but I will leave that for another time.

    Quote Originally Posted by Paul McKenzie View Post
    If the data must be global, you should prefer using a C++ class of static data members instead of the 'C'-style of using extern. Using the former eliminates the need for extern, thereby you can #include the class in any module and not have the linker stating "multiply defined in module xxxx" errors.
    That sounds like an interesting implementation. Do you have a reference for the proper syntax? I have some static maps that get used in more than one place and I think that may be the preferable implementation for those as well. These maps currently have a loading function and a search function,

    Code:
    // map initialization function
    map<string,int> iniStandardMap() {
       map<string,int> standardMap;      //  Z HQ 1234N1234 ppp mmmO124 pp mm
       standardMap.insert(pair<string,int>("07-00-0110-0110-000-000-000-00-00", 84));
       standardMap.insert(pair<string,int>("07-00-0110-0010-000-000-000-00-00", 84));
       standardMap.insert(pair<string,int>("07-00-0110-0100-000-000-000-00-00", 84));
       standardMap.insert(pair<string,int>("07-03-0200-0200-000-010-000-00-00", 84));
       standardMap.insert(pair<string,int>("07-03-0200-0100-000-010-000-00-00", 84));
       standardMap.insert(pair<string,int>("07-00-1200-0000-000-000-020-00-00", 31));
       standardMap.insert(pair<string,int>("07-00-1200-1000-000-000-020-00-00", 31));
       standardMap.insert(pair<string,int>("07-00-1200-1000-100-000-020-00-00", 31));
       standardMap.insert(pair<string,int>("07-03-2100-0000-000-000-110-00-10", 31));
       standardMap.insert(pair<string,int>("07-03-2100-1000-000-000-110-00-10", 31));
       // many more entries
       return standardMap;
    }
    
    // map search function
    int standardLookup(string pattern) {
       // create the map if it doesn't exist
       static const map<string,int> standardMap = iniStandardMap();
      // search the map, return second based on key value
       const map<string,int>::const_iterator it = standardMap.find(pattern);
       if(it != standardMap.end()) {
          return it->second;
       }
    
    return -1; // if pattern is not in the map
    }
    I'm not sure that this is the best implementation of a map, but it seems like another good candidate to be able to include with #include and not extern. I just spend an hour dealing with the "multiply defined in module xxxx" errors you mentioned, so perhaps I should have read your post yesterday.

    LMHmedchem

  10. #10
    Join Date
    Apr 1999
    Posts
    27,449

    Re: delete vector of objects, single objects, dealocate menory

    Quote Originally Posted by LMHmedchem View Post
    That sounds like an interesting implementation. Do you have a reference for the proper syntax? I have some static maps that get used in more than one place and I think that may be the preferable implementation for those as well.
    Code:
    #ifndef SOMEHEADER_H
    #define SOMEHEADER_H
    
    #include <map>
    #include <string>
    
    typedef std::map<std::string, int> StringMap;
    
    struct MyGlobals
    {
        static StringMap standardMap;    
    };
    #endif
    Then in one and only one CPP module:
    Code:
    StringMap MyGlobals::standardMap;
    You can then #include "SomeHeader.h" whenever you need to use standardMap.
    Code:
    #include "SomeHeader.h"
    using namespace std;
    
    void iniStandardMap() 
    {
       MyGlobals::standardMap.insert(make_pair("07-00-0110-0110-000-000-000-00-00", 84));
       MyGlobals::standardMap.insert(make_pair("07-00-0110-0010-000-000-000-00-00", 84));
       //...
       // many more entries
    }
    Regards,

    Paul McKenzie

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured