c++ - How to free a pointer immediately after returning it? -
hey first post , homework assignment theoretical info structures class. question i'm asking isn't apart of assignment create code cleaner if possible. (if of concern assignment create reverse polish notation calculator using linked list stack.)
what want this
char* posteval(char* equation) { char* curchar = equation; struct stacknode* stack = null; /*alot of processing (hint: stack gets initialized during this)*/ /****is there way free node after returning it?? -- pseudo-code!*****/ homecoming freethenreturn(stack->data); } i know sprintf() or strcpy() temporary variable free node homecoming temp varaible but, there improve way??
edit: i'm unsure unclear yes i'm trying homecoming re-create of 'data' fellow member (which char*) calling function. problematic because need free memory of stack , send re-create of info stored there. know seems weird way how teacher told so. cannot edit struct stacknode definition nor prototype of posteval() @ all. struct stacknode definition follows:
struct stacknode { char* data; struct stacknode *next; } furthermore, function posteval() loop switch statement parses each char of variable 'equation' , solves equation returns reply char*. our teacher simply wants see if can solve using stacks...that's point. know it'd easier not doing way meh didn't create assignment.
i think need along lines of this:
data *data = stack->data; // or whatever type stack->data free(stack); homecoming data; but agree comments question doesn't create clear.
either thing want free , thing thing want homecoming same thing (in case plan flawed -- there's no utilize returning that's freed, because caller can't utilize it), or else they're different (in case need separate them somehow, can't tell how because haven't told plenty them).
c++ c stack
No comments:
Post a Comment