Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] main function is called inside the body of the for loop

Status
Not open for further replies.

RAHUL_KUMAR

Member level 5
Member level 5
Joined
Jun 5, 2017
Messages
90
Helped
4
Reputation
8
Reaction score
4
Trophy points
8
Location
Bhubaneswar,Odisha,India
Activity points
780
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?
 

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.
 

Hi,

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

Klaus
 

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/4238179/calling-main-in-main-in-c

either way, there is little to no good reason to keep calling main again and again
 

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???
 

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'
 

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?
 

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.
 

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.
 

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.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top