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

    quick question about deleteing an object

    I have a bunch of objects that I am creating in a function. There will be allot of them created before the function returns and the objects are destructed when they go out of scope. I would like to delete the objects after I am done with them, but before the function returns. It doesn't seem as if I can just do delete[] objectName. I get an error that delete is expecting a pointer.

    Something like this will compile,
    Code:
    myClass * object_pointer;
    myClass myFirstObject;
    object_pointer = &myFirstObject;
    delete [] object_pointer;
    but I'm not really sure that is right. If objects are on the stack, which I assume they would be when declared in {}, I don't know if delete is the right way to de-allocate.

    LMHmedchem

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: quick question about deleteing an object

    delete is never to be used except on addresses that you allocated with new.

    If you wish something on the stack to go out of scope at a particular time, you can limit its scope with {}:
    Code:
    int func(int f)
    {
        string mywholefuncstring = "This string will remain in scope for the entire function.";
    
        {
            string mytempstring = "This string will go out of scope immediately.";
        }
    
        string mylaststring = "mytempstring is now out of scope. mylaststring was not in scope at the same time as mytempstring because it is only now being constructed. However, it will remain in scope for the remainder of the function.";
    }

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

    Re: quick question about deleteing an object

    Thanks, that works fine. I was thinking that delete[] was for heap memory, is that right, or is it more complicated than that?

    I have some code that I am running,
    Code:
       {
         modelNet netObject;
         loadNet_af1(netObject);
         activity_NS.push_back( netObject.calcAct(netObject, normScaleInpt) );
       }
    This code is repeated many time, so I would like it to be a function. The problem is that the function being called in the code is different for each repetition.
    Code:
       { modelNet netObject;
         loadNet_af1(netObject);
         activity_NS.push_back( netObject.calcAct(netObject, normScaleInpt) ); }
    
       { modelNet netObject;
         loadNet_af2(netObject);
         activity_NS.push_back( netObject.calcAct(netObject, normScaleInpt) ); }

    Is there a way to write a function with code like the above and pass it a pointer to the function I want to use and have it point to a different function on each call? I don't know if I am explaining that clearly. The netObject obj is modified by the function loadNet_ so it would have to stay in scope after it is returned so the modifications remain intact.

    LMHmedchem

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

    Re: quick question about deleteing an object

    Quote Originally Posted by LMHmedchem
    Is there a way to write a function with code like the above and pass it a pointer to the function I want to use and have it point to a different function on each call?
    Yes, e.g., you move the code into a function that has a function pointer parameter.
    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
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: quick question about deleteing an object

    If loadNet_ are non-member functions you can pass a function pointer http://www.newty.de/fpt/index.html or maybe even iterate over a std::vector of function pointers. If they are member functions you can do pretty much the same thing but the syntax is a bit different. The link I posted explains that as well.

    As Lindley said you only use delete for memory that you have new:ed. See this http://cplusplus.com/reference/std/n...%20delete/ regarding delete and delete[]
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  6. #6
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: quick question about deleteing an object

    Quote Originally Posted by LMHmedchem View Post
    The netObject obj is modified by the function loadNet_ so it would have to stay in scope after it is returned so the modifications remain intact.
    You can instantiate it outside of the functions that get called via function pointers and pass a non-const reference to it.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  7. #7
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: quick question about deleteing an object

    Quote Originally Posted by LMHmedchem View Post
    Thanks, that works fine. I was thinking that delete[] was for heap memory, is that right, or is it more complicated than that?
    In the majority of cases, that's correct. new and new[] allocate memory from the heap, and only addresses returned from these functions are valid things to pass to delete and delete[].

    You can avoid having to call delete or delete[] yourself on new'd memory by storing it in a smart pointer. Make sure you choose one which is documented to delete the memory the appropriate way. In most cases, that means anything allocated with new (as opposed to new[]); dynamic array should be managed by vectors, not smart pointers.

    The exception is that it is possible to overload operator new and operator delete. If these are overloaded, then the memory they return could come from wherever you specify----not necessarily the heap. There are not many good reasons to overload these, however, as it is usually more intuitive to use an allocator object instead.

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

    Re: quick question about deleteing an object

    Quote Originally Posted by S_M_A View Post
    If loadNet_ are non-member functions you can pass a function pointer or maybe even iterate over a std::vector of function pointers.
    I've been working through the posted tutorial. It is a nice resource, so thanks.

    I am making some progress, but I'm getting stuck on the syntax.
    Code:
    // prototypes
       // generic loader function
       load_netObject(int (*loadModelNet)(modelNet&), modelNet& netObject);
    
       // individual functions passed to loader as pointer
       int loadModelNet_af0_RI3_v33p16p1d1_11d09d05(modelNet& modelNet_tmp);
       int loadModelNet_af1_RI3_v33p16p1d1_11d09d05(modelNet& modelNet_tmp);
    
       // pointer to loadModelNet_
       int (*loadModelNet)(modelNet&) = NULL;
    
    
    // code in line
    {
       // create an empty object
       modelNet netObject;
    
       // assign address to function pointer
       loadModelNet = &loadModelNet_af0_RI3_v33p16p1d1_11d09d05;
    
       // call function to load netObject using function pointed to
       load_netObject(loadModelNet, &netObject);
    
       // do stuff with the object after it has been loaded
       activity_NS.push_back( netObject.calcAct(netObject, normScaleInpt) );
    }
    
    // generic loading function
    void load_netObject(int (*loadModelNet)(modelNet&), modelNet& netObject) {
       (*loadModelNet)(netObject); // call whatever function *loadModelNet points to
    }
    I get the compiler errors,
    error: invalid initialization of non-const reference of type 'modelNet&' from a temporary of type 'modelNet*'
    referring to the call: load_netObject(loadModelNet, &netObject);

    error: in passing argument 2 of `void load_netObject(int (*)(modelNet&), modelNet&)'
    referring to the prototype: load_netObject(int (*loadModelNet)(modelNet&), modelNet& netObject);

    I don't know if I just have the syntax wrong for the call, or if I have set something else up wrong.

    This technique doesn't help me much yet in the way of compacting code, but I think I can get this into a vector of pointers once I get the syntax set up right.

    LMHmedchem

  9. #9
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: quick question about deleteing an object

    I really hate the function pointer declaration syntax and am not quite familiar with it, but...

    Quote Originally Posted by LMHmedchem View Post
    Code:
       // call function to load netObject using function pointed to
       load_netObject(loadModelNet, &netObject);
    This is wrong, independent of function pointers: The second parameter is declared as a reference, but you're taking the address of the object and pass that, just like passing to a pointer parameter. Remove the &.

    This is at least the culprit for the first error, but maybe the second one is a consequential error to this and may be eliminated by that fix as well.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

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

    Re: quick question about deleteing an object

    Quote Originally Posted by LMHmedchem View Post
    I am making some progress, but I'm getting stuck on the syntax.
    It would be a lot easier if you used typedefs instead of all of those definitions:
    Code:
    typedef int (*INTLOADERFN)(modelNet&);
    //...
    
    {
       // create an empty object
       modelNet netObject;
    
       // pointer to loadModelNet_
       INTLOADERFN loadModelNet = NULL;
    
       // assign address to function pointer
       loadModelNet = &loadModelNet_af0_RI3_v33p16p1d1_11d09d05;
    
       // call function to load netObject using function pointed to
       load_netObject(loadModelNet, netObject);
    
       // do stuff with the object after it has been loaded
       activity_NS.push_back( netObject.calcAct(netObject, normScaleInpt) );
    }
    
    // generic loading function
    void load_netObject(INTLOADERFN loadModelNet, modelNet& netObject) 
    {
       (*loadModelNet)(netObject); 
    }
    Also, be aware that a pointer to a non-static class member function is not the same as the function pointers you're using or seeing here. The syntax to set one up and to make the call is much different than these examples.

    Regards,

    Paul McKenzie

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

    Re: quick question about deleteing an object

    Thanks for all the help, this is what I ended up with.
    Code:
    // pointer to function
    typedef int (*INTLOADERFN)(modelNet&);
    
    // prototypes
    int loadModelNet_af0_RI3_v33p16p1d1_11d09d05(modelNet& modelNet_tmp);
    int loadModelNet_af1_RI3_v33p16p1d1_11d09d05(modelNet& modelNet_tmp);
    int loadModelNet_af2_RI3_v33p16p1d1_11d09d05(modelNet& modelNet_tmp);
    int loadModelNet_af3_RI3_v33p16p1d1_11d09d05(modelNet& modelNet_tmp);
    int loadModelNet_af4_RI3_v33p16p1d1_11d09d05(modelNet& modelNet_tmp);
    int loadModelNet_af5_RI3_v33p16p1d1_11d09d05(modelNet& modelNet_tmp);
    int loadModelNet_af6_RI3_v33p16p1d1_11d09d05(modelNet& modelNet_tmp);
    int loadModelNet_af7_RI3_v33p16p1d1_11d09d05(modelNet& modelNet_tmp);
    int loadModelNet_af8_RI3_v33p16p1d1_11d09d05(modelNet& modelNet_tmp);
    int loadModelNet_af9_RI3_v33p16p1d1_11d09d05(modelNet& modelNet_tmp);
    
    
    // generic loading function
    void load_net_calc_activity(INTLOADERFN loadModelNet, vector<float>& normScaleInpt, vector<double>& activity_NS) {
    
       // create net object
       modelNet netObject2;
    
       // load net object using function passed as pointer
       (*loadModelNet)(netObject2);
    
       // calculate activity using net object and normScaleInpt
       activity_NS.push_back( netObject2.calcAct(netObject2, normScaleInpt) );
    }
    
     //...
    
       vector<INTLOADERFN> list_of_nets;
       vector<float> normScaleInpt;
       vector<double> activity_NS;
    
       INTLOADERFN loadModelNet = NULL;
    
       list_of_nets.push_back(&loadModelNet_af0_RI3_v33p16p1d1_11d09d05); // net_af0
       list_of_nets.push_back(&loadModelNet_af1_RI3_v33p16p1d1_11d09d05); // net_af1
       list_of_nets.push_back(&loadModelNet_af2_RI3_v33p16p1d1_11d09d05); // net_af2
       list_of_nets.push_back(&loadModelNet_af3_RI3_v33p16p1d1_11d09d05); // net_af3
       list_of_nets.push_back(&loadModelNet_af4_RI3_v33p16p1d1_11d09d05); // net_af4
       list_of_nets.push_back(&loadModelNet_af5_RI3_v33p16p1d1_11d09d05); // net_af5
       list_of_nets.push_back(&loadModelNet_af6_RI3_v33p16p1d1_11d09d05); // net_af6
       list_of_nets.push_back(&loadModelNet_af7_RI3_v33p16p1d1_11d09d05); // net_af7
       list_of_nets.push_back(&loadModelNet_af8_RI3_v33p16p1d1_11d09d05); // net_af8
       list_of_nets.push_back(&loadModelNet_af9_RI3_v33p16p1d1_11d09d05); // net_af9
    
       for(i=0; i<list_of_nets.size(); i++) {
          loadModelNet = list_of_nets[i];
          load_net_calc_activity(loadModelNet, normScaleInpt, activity_NS);
       }
    This does a nice job of looping through all of the functions. Since netObject2 is created in the loading function, it goes out nicely when the function returns and I don't get a blow up on the stack. Everything I need accumulates in activity_NS an it seems pretty fast.

    I need to remember to make use of typedef, since I seem to need a 30" monitor to see some of my lines of code.

    LMHmedchem

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