What will happen if I remove the "for( ;; );" from the main?

Actually I read from the Mastering in the FreeRTOS documentation that,

/* If all is well then main() will never reach here as the scheduler will
now be running the tasks. If main() does reach here then it is likely that
there was insufficient heap memory available for the idle task to be created.
Chapter 2 provides more information on heap memory management. */
for( ;; );

what if I removed for( ;; ); from the main? I mean in case if anything happens in the project and if the control goes to that infinite loop then the product will hang forever.

So my question is why we use this?? can we remove it??

If you omit the infinite loop there, then the program will just exit main, and you will in all likelihood end up in a similar loop in the run time, as embedded programs have nothing to exit to.

Since the loop in the runtime could also be gotten to by the program calling exit(), or abort(), you have lost information about why the program isn’t running.

1 Like

lets say if we have code
void main(void)

at the end contains a return statement although we didn’t explicitly put it there ourselves. When main returns the CPU executes the next instruction which is simply a GOTO to go back to the beginning of the code. main() is simply called over and over again. So why we preffer to stuck in while loop (at the end of the main) rather than calling main function again and again.

Sir please can you elaborate what you are saying. I really want to clear this concept in my mind.

What happens when you exit an embedded program is only defined by a particular implementation. Some may restart the program, but many just loop to stall. In any case, your program had a major failure where the system didn’t start up, and retrying is unlikely to change that. Ideally you want it easy to see what happened when debugging, so having the loop right there is helpfull.

1 Like

Ohh yess, now i got it it is for us to debug peacefully. Thank you so much sir.