+ Post New Thread
Results 1 to 12 of 12
  1. #1
    Member level 4
    Points: 1,047, Level: 7

    Join Date
    Jun 2017
    Location
    Bhubaneswar,Odisha,India
    Posts
    75
    Helped
    4 / 4
    Points
    1,047
    Level
    7

    main function is called inside the body of the for loop

    what happen when the main() is called inside the body of the for loop
    Code:
    main()
          {
             for(i=0;i<10;i++)
              main();
           }
    here on every true condition body of the for loop executed ,and on satisfying the condition ,main function is called , which again re-initializes the variable i with 0, hence its getting infinite loop.
    my question is -- when the main function is called ,still it is inside the for loop,..because yet there is no false condition arrived ,.hence next time "i" variable must be incremented . but actually " i " is getting re-initialised.

    what are background behaviour of main which let it to do so?

  2. #2
    Super Moderator
    Points: 248,140, Level: 100
    Awards:
    1st Helpful Member

    Join Date
    Jan 2008
    Location
    Bochum, Germany
    Posts
    43,155
    Helped
    13131 / 13131
    Points
    248,140
    Level
    100

    Re: main function is called inside the body of the for loop

    In a first order it's an infinite recursion that causes stack overflow.

    The behavior of variable i depends on the variable type (global static or locally declared), but it's not really a relevant question in this case.



    •   AltAdvertisment

        
       

  3. #3
    Super Moderator
    Points: 66,089, Level: 62
    Achievements:
    7 years registered
    Awards:
    Most Frequent Poster 3rd Helpful Member

    Join Date
    Apr 2014
    Posts
    13,434
    Helped
    3101 / 3101
    Points
    66,089
    Level
    62

    Re: main function is called inside the body of the for loop

    Hi,

    I canīt see why this can be useful.
    Whatīs your idea behind that?

    Klaus
    Please donīt contact me via PM, because there is no time to respond to them. No friend requests. Thank you.



  4. #4
    Advanced Member level 4
    Points: 6,970, Level: 19

    Join Date
    Apr 2016
    Posts
    1,483
    Helped
    255 / 255
    Points
    6,970
    Level
    19

    Re: main function is called inside the body of the for loop

    Quote Originally Posted by RAHUL_KUMAR View Post
    what happen when the main() is called inside the body of the for loop
    Code:
    main()
          {
             for(i=0;i<10;i++)
              main();
           }
    here on every true condition body of the for loop executed ,and on satisfying the condition ,main function is called , which again re-initializes the variable i with 0, hence its getting infinite loop.
    my question is -- when the main function is called ,still it is inside the for loop,..because yet there is no false condition arrived ,.hence next time "i" variable must be incremented . but actually " i " is getting re-initialised.

    what are background behaviour of main which let it to do so?
    not all languages accept this. C does, C++ doesn't. see https://stackoverflow.com/questions/...n-in-main-in-c

    either way, there is little to no good reason to keep calling main again and again
    Really, I am not Sam.



  5. #5
    Member level 4
    Points: 1,047, Level: 7

    Join Date
    Jun 2017
    Location
    Bhubaneswar,Odisha,India
    Posts
    75
    Helped
    4 / 4
    Points
    1,047
    Level
    7

    Re: main function is called inside the body of the for loop

    Quote Originally Posted by FvM View Post
    In a first order it's an infinite recursion that causes stack overflow.

    The behavior of variable i depends on the variable type (global static or locally declared), but it's not really a relevant question in this case.
    yes i agree ,this will arise the condition of stack overflow.
    but question is not in that direction. let me clarify the question once again...
    In the execution of for loop ,initialization takes place for only once ,.then it only check the condition ,if condition is true ,.it will execute the body of loop and then updation takes place right !!. means if a condition is true it will never goes to re initialization rather it goes for updation.

    but here the condition is true...and main function is called. then why it is re initializing rather than updating ,coz though main function is called but the still it has not came out of for loop. coz it has never get false condition???



    •   AltAdvertisment

        
       

  6. #6
    Advanced Member level 4
    Points: 6,970, Level: 19

    Join Date
    Apr 2016
    Posts
    1,483
    Helped
    255 / 255
    Points
    6,970
    Level
    19

    Re: main function is called inside the body of the for loop

    Quote Originally Posted by RAHUL_KUMAR View Post
    yes i agree ,this will arise the condition of stack overflow.
    but question is not in that direction. let me clarify the question once again...
    In the execution of for loop ,initialization takes place for only once ,.then it only check the condition ,if condition is true ,.it will execute the body of loop and then updation takes place right !!. means if a condition is true it will never goes to re initialization rather it goes for updation.

    but here the condition is true...and main function is called. then why it is re initializing rather than updating ,coz though main function is called but the still it has not came out of for loop. coz it has never get false condition???
    what a word salad

    I suggest you look at what the compiled code for this snippet of code would look like. your notion of 'update' doesn't take into account that there are multiple version of 'i'
    Really, I am not Sam.



  7. #7
    Member level 4
    Points: 1,047, Level: 7

    Join Date
    Jun 2017
    Location
    Bhubaneswar,Odisha,India
    Posts
    75
    Helped
    4 / 4
    Points
    1,047
    Level
    7

    Re: main function is called inside the body of the for loop

    Quote Originally Posted by FvM View Post
    In a first order it's an infinite recursion that causes stack overflow.

    The behavior of variable i depends on the variable type (global static or locally declared), but it's not really a relevant question in this case.
    yes i agree ,this will arise the condition of stack overflow.
    but question is not in that direction. let me clarify the question once again...
    In the execution of for loop ,initialization takes place for only once ,.then it only check the condition ,if condition is true ,.it will execute the body of loop and then updation takes place right !!. means if a condition is true it will never goes to re initialization rather it goes for updation.

    but here the condition is true...and main function is called. then why it is re initializing rather than updating ,coz though main function is called but the still it has not came out of for loop. coz it has never get false condition?



  8. #8
    Super Moderator
    Points: 66,089, Level: 62
    Achievements:
    7 years registered
    Awards:
    Most Frequent Poster 3rd Helpful Member

    Join Date
    Apr 2014
    Posts
    13,434
    Helped
    3101 / 3101
    Points
    66,089
    Level
    62

    Re: main function is called inside the body of the for loop

    Hi,

    In my eyes this is a useless discussion, because the code makes no sense.
    Itīs similar to: When I drive with my car, can I continiously overtake myself?

    ***
    Please answer the question of post#3: "Whatīs your idea behind that?"

    Klaus

    Added:
    In the execution of for loop ,initialization takes place for only once ,
    True, because a loop usually is started, and it runs until it ends.

    But in your case you endlessly start new loops before you finsh the other one.
    Please donīt contact me via PM, because there is no time to respond to them. No friend requests. Thank you.



  9. #9
    Member level 4
    Points: 1,047, Level: 7

    Join Date
    Jun 2017
    Location
    Bhubaneswar,Odisha,India
    Posts
    75
    Helped
    4 / 4
    Points
    1,047
    Level
    7

    Re: main function is called inside the body of the for loop

    it seems to be useless if i only intend to know the things on only application level. My intention is to know the background behaviour of main function by raising this question.
    i would be obliged to u if i get satisfactory answer.



  10. #10
    Super Moderator
    Points: 73,854, Level: 66
    Achievements:
    7 years registered
    Awards:
    2nd Helpful Member
    betwixt's Avatar
    Join Date
    Jul 2009
    Location
    Aberdyfi, West Wales, UK
    Posts
    12,094
    Helped
    4006 / 4006
    Points
    73,854
    Level
    66

    Re: main function is called inside the body of the for loop

    FvM answered this in post #2. It depends on what kind of variable 'i' is and possibly whether the compiler allocates stack space for parameters passed into main() or expects main to return a value.

    In general though, each call to a function reserves stack space for the return address so it will keep allocating space until either the stack runs out or the memory runs out, which depends on the system has fixed stack size or uses system memory. A good compiler would trap this as a coding error.

    It would pay you to search for 'iterative routines' which should explain the process of a function calling itself and why an 'escape route' is required.

    Brian.
    PLEASE - no friends requests or private emails, I simply don't have time to reply to them all.
    It's better to share your questions and answers on Edaboard so we can all benefit from each others experiences.



    •   AltAdvertisment

        
       

  11. #11
    Junior Member level 3
    Points: 402, Level: 4

    Join Date
    Jun 2017
    Location
    Bhubaneswar, India
    Posts
    27
    Helped
    4 / 4
    Points
    402
    Level
    4

    Re: main function is called inside the body of the for loop

    The main() is calling itself again and again but never return any value.
    Last edited by MD_SHAHRUKH; 9th August 2018 at 12:38.


    1 members found this post helpful.

  12. #12
    Super Moderator
    Points: 248,140, Level: 100
    Awards:
    1st Helpful Member

    Join Date
    Jan 2008
    Location
    Bochum, Germany
    Posts
    43,155
    Helped
    13131 / 13131
    Points
    248,140
    Level
    100

    Re: main function is called inside the body of the for loop

    The loop is infinite in any case, independent of the i variable type.


    1 members found this post helpful.

--[[ ]]--