I am a c++ programmer coming from C# background and still have some confusions about memory management in c++
I have a class with three functions
1st returns a pointer to a local variable
2nd returns a pointer to a list of pointers
3rd returns a pointer to a list of lists of pointers
consider this code:
AugmentedActivePastConjugator.h
class AugmentedActivePastConjugator {
public:
AugmentedActivePastVerb* createVerb(AugmentedTrilateralRoot* root, int pronounIndex, int formulaNo);
vector<AugmentedActivePastVerb*>* createVerbList(AugmentedTrilateralRoot* root, int formulaNo);
vector<vector<AugmentedActivePastVerb*>*>* createAllVerbList(AugmentedTrilateralRoot* root) ;
};
AugmentedActivePastConjugator.cpp
AugmentedActivePastVerb* AugmentedActivePastConjugator::createVerb(AugmentedTrilateralRoot* root, int pronounIndex, int formulaNo) {
string lastDpa = PastConjugationDataContainer.getLastDpa(pronounIndex);
string connectedPronoun = PastConjugationDataContainer.getConnectedPronoun(pronounIndex);
AugmentedActivePastVerb verb (root,lastDpa,connectedPronoun,formulaNo);
return &verb ;
}
vector<AugmentedActivePastVerb*>* AugmentedActivePastConjugator::createVerbList(AugmentedTrilateralRoot* root, int formulaNo) {
vector<AugmentedPastVerb *> result;
for (int i = 0; i < 13; i++) {
AugmentedActivePastVerb* verb = createVerb(root, i, formulaNo);
result.push_back(verb);
}
return &result;
}
vector<vector<AugmentedActivePastVerb*>*>* AugmentedActivePastConjugator::createAllVerbList(AugmentedTrilateralRoot* root) {
vector<vector<AugmentedActivePastVerb*>*> result;
vector<AugmentationFormula*>::iterator begin = root->getAugmentationList().begin();
vector<AugmentationFormula*>::iterator end = root->getAugmentationList().end();
while (begin !=end) {
AugmentationFormula* formula = *begin;
vector<AugmentedActivePastVerb*>* formulaVerbList = createVerbList(root, formula->getFormulaNo());
result.push_back(formulaVerbList);
}
return &result;
}
if I want to create a list of verbs returned by this class, for example
// general variable
list<AugmentedActivePastVerb *> verbslist;
AugmentedActivePastConjugator conjugator;
void createverbslist()
{
for (int i=0; i < 20; i++) {
AugmentedActivePastVerb* verbs = conjugator.createVerb(someroot, pIndex, fNo);
verbslist.push_back(verb)
}
}
what should I do after I have finished using verbslist , should I delete all the pointers inside it or will it free them automatically
I have read an article that says:
don't free memory which was not dynamically allocated
so if I shouldn't free it, what happens to those pointers inside verbslist
AugmentedActivePastVerb verb (root,lastDpa,connectedPronoun,formulaNo);
return &verb ;
Never ever do this. verb
is allocated on the stack and gets destroyed as soon as the method call is removed from the stack. And voila, your applications points to a variable that's no longer in its place.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.