-1

When I have a main() function that calls another function, i.e.,

...
void somef(int);

int main() {
int a {};
std::cin>>a;
somef(a)

return 0; // never returns. }
...

Moreover, if the function I call is something like this:

...
void somef(int a) {
main()
return; // to make the not-returning behaviour more clear.
}
...

Can I make the memory where the activation records stored overflow since the program gets stuck between main() and somef(int) functions without returning? I am currently writing a program that makes calls like this and it seems to work just for now. However, I wonder if my program crashes if I could somehow put too many inputs successively. To test this, I have tried the below code:

#include <iostream>
  
using namespace std;

int somef(int);
  
int main()
{
    long long int a;
    while (true) {
           
           somef(a);
    }
    return 0;
}

int somef(int a)
{
long long int b;

main();

return 1; // never enters return.
}

I put long long ints to, hopefully, take up more memory in the activation records. The result is that the program terminates without any errors after approximately half a second. However, I expected that the while(true) condition makes the program crash somehow. Is this a some kind of crash? If so, does this mean that the program would also crash if I somehow took too many inputs in the first part of the code I have shared?

Thanks =)

  • 7
    _"The [`main function`](https://en.cppreference.com/w/cpp/language/main_function) has several special properties: 1) It cannot be used anywhere in the program, a) in particular, it cannot be called recursively…"_ – heap underrun Mar 17 '23 at 19:39
  • your code has undefined behavior. Thats about all one can say about it with certainty – 463035818_is_not_an_ai Mar 17 '23 at 19:40
  • https://stackoverflow.com/questions/27208890/cant-call-int-main-to-repeat-program-c – 463035818_is_not_an_ai Mar 17 '23 at 19:44
  • @heapunderrun https://godbolt.org/z/K4zfzddEe I have updated the code upon your reference that the main function cannot be called recursively. I have added two functions that call each other but it still terminates without no errors- no sign of overflow etc. – ConventionalProgrammer Mar 17 '23 at 19:46
  • @463035818_is_not_a_number The thing is my program calls main() each time I take an input and there is no problem. My question is the possibility to obtain an overflow behavior such as in infinite recursion. – ConventionalProgrammer Mar 17 '23 at 19:48
  • 1
    The problem is that your program does not qualify as a valid C++ program according to the language standard (because you call main recursively, which is prohibited). So, anything is allowed to happen. What else you are trying to prove? – heap underrun Mar 17 '23 at 19:53
  • 1
    The updated program hits another UB: https://en.cppreference.com/w/cpp/language/memory_model <- Forward progress -- the compiler is allowed to assume that progress will happen and ignore your effect-free infinite loop – teapot418 Mar 17 '23 at 19:54
  • @heapunderrun I am trying to catch an overflow error such as in infinite recursion. Even if I don't call main recursively, instead call two functions that have long long local variables in them repeatedly, the program still terminates with no errors. No difference than the main() case. – ConventionalProgrammer Mar 17 '23 at 19:55
  • 1
    If `b` is never used, the compiler is allowed to optimize it out. – user4581301 Mar 17 '23 at 19:59
  • 1
    Side note: Never expect a crash. A crash is only one of the more common of an infinite number of possible outcomes when you invoke undefined behaviour, and exhausting memory through infinite recursions is invoking undefined behaviour. – user4581301 Mar 17 '23 at 20:02
  • 1
    whatever you are trying to achieve can be done without recursively calling main. You can rename your current `main` to `mymain` then write a main that does nothing but calls `mymain`. Though then you still have an infinite recursion – 463035818_is_not_an_ai Mar 17 '23 at 20:10
  • @teapot418 Hi, thank you for your answer. Is the associated line: "This allows the compilers to remove all loops that have no observable behavior, without having to prove that they would eventually terminate because it can assume that no thread of execution can execute forever without performing any of these observable behaviors." ? – ConventionalProgrammer Mar 17 '23 at 20:11
  • 1
    Yes, that was what I was pointing to. Also: [whatever this is](https://old.reddit.com/r/ProgrammerHumor/comments/10wur63/isnt_c_fun/) – teapot418 Mar 17 '23 at 20:41
  • @463035818isnotanumber Could you please provide me a simple example please =) What do I get from using this method? Thanks... – ConventionalProgrammer Mar 17 '23 at 20:50

1 Answers1

-1

This is not a properly written C++ program. I understand some beginner programmers have trouble with loops and such, but you'll learn over time.

  1. Don't call main again. Main function should be unique and only called once at compile time.
  2. You have a while loop. The condition is set to true which means that it's almost expected to run endlessly (probably not your goal to get an infinite loop).
  3. To stop your while loop eventually, you can change your condition from true to something like (someVariable == true or someVariable equals some value) and then be sure there's a stopping point. You can also do it in the body of the while loop. Put an if statement that checks for something specific (a variable value) and if true or false (up to you) then use a break statement or use that as your stopping condition to stop the looping.
Cornel
  • 180
  • 2
  • 4
  • 13