-
December 11th, 2011, 10:52 PM
#1
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
-
December 11th, 2011, 11:07 PM
#2
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.";
}
-
December 12th, 2011, 12:32 AM
#3
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
-
December 12th, 2011, 03:30 AM
#4
Re: quick question about deleteing an object
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.
-
December 12th, 2011, 03:56 AM
#5
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[]
-
December 12th, 2011, 05:42 AM
#6
Re: quick question about deleteing an object
Originally Posted by LMHmedchem
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.
-
December 12th, 2011, 11:05 AM
#7
Re: quick question about deleteing an object
Originally Posted by LMHmedchem
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.
-
December 12th, 2011, 01:30 PM
#8
Re: quick question about deleteing an object
Originally Posted by S_M_A
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
-
December 12th, 2011, 03:34 PM
#9
Re: quick question about deleteing an object
I really hate the function pointer declaration syntax and am not quite familiar with it, but...
Originally Posted by LMHmedchem
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.
-
December 12th, 2011, 03:34 PM
#10
Re: quick question about deleteing an object
Originally Posted by LMHmedchem
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
-
December 13th, 2011, 12:36 PM
#11
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|