Hello. At the moment I am at the initial stage of learning C ++, so I would like to ask a more experienced question. I wrote a simple console program and tried to implement an elementary dialogue with the user by creating a special method in one of the classes:

class MyClass { public : void method() { cout << endl << "Method used" << endl; } void menu() { int* choice = new int; cout << "1. Use method;" << endl << "2. Stop;" << endl << "set : "; cin >> *choice; switch(*choice) { case 1 : delete choice; method(); menu(); break; case 2 : delete choice; exit(1); break; } } }; 

In fact, it is a bit bigger, but the concept is the same. Actually I wanted to ask about the application of this principle: is it ok to do this? I mean, I’m a little worried that each new entry into the menu () method after entering "1" will suspend the active call. Therefore, if you continue for so long, you will get a rather long chain of suspended calls.

Or is it more expedient to endure all this disgrace, for example, in main ()? :

 int main() { MyClass* my_obj = new MyClass; int* choice = new int; while(true) { cout << "1. Use method;" << endl << "2. Stop;" << endl << "set : "; cin >> *choice; switch(*choice) { case 1 : my_obj->method(); break; case 2 : delete choice; delete my_obj; exit(1); break; } } return 0; } 

When using the menu () method, as well as main (), all allocated memory is freed. However, the long tail of the suspended calls remains. Can such a chain, for example, sooner or later take a lot of memory or something like that? In general, I would like to receive a reasoned answer, thanks =)

  • no matter how ironic it sounds, but such a recursion as in the first example will sooner or later lead to StackOverflow , that is, a stack overflow. An infinite loop is a common approach in development. For example, the life of any windows-application is in the loop window processing. - teran
  • And why are you putting choice in a pile? Why not just int choice and go everywhere without * ? - VladD
  • The problem with such a recursion is that it cannot be used everywhere , because quite often the method should return something. And if he does not return anything from you and cannot return in principle. It is better to manage the program execution flow with the help of specially designed control structures - while / for / if / ... - VladD
  • Vlad, I warned that a newcomer. For the time being, I don’t distinguish between putting data onto the stack and into a heap just because I haven’t touched on this topic yet. - Princess_York
  • Thank you all for the comments, I got useful knowledge =) - Princess_York

1 answer 1

Well, actually you will be calling menu() very long time until the stack is exhausted, but just why do this? you can get a bad habit :) - especially since you wrote the second example quite correctly.

I have only two comments:
1. There’s absolutely no need to make a choice pointer and allocate memory for it. It is better to make it a simple int variable. Of course, if it's just for training, then okay :)
2. exit(1) is not the best way to exit, especially since a non-zero code usually indicates some kind of error. Just exit main() - that's enough. And, by the way, break after exit(1) can not write. I hope you understand why? :)

  • On absence of break compiler most likely swears. - VladD
  • Thanks for the answer! Regarding pointers / non-indicators, I really recently quite thoroughly studied this topic and now I shove them wherever I can just to consolidate what has been covered =) By the way, I’m writing just because the perfectionist inside requires the completion of all branching paths with this instruction: D - Princess_York