Is this code correct?
int *set_new (int choice) {
int *new_choice {new int};
*new_choice = choice;
return new_choice;
}
Should I delete *new_choice
after the value is returned, because *new_choice
is on the memory heap?
int *set_new (int choice) {
int *new_choice {new int};
*new_choice = choice;
return new_choice;
delete new_choice;
}
Or, do I need to delete it where the function is finished calling? if not deleted, will it cause a memory leak?
I learned on a website if you use dynamic memory (new) immediately after that delete (delete) so there is no memory leak.
The function is called in main
:
int main () {
int *b {new_choice (23)};
printf ("% d", *b);
delete b;
}