entering main loop crashes the program

Hello, I have a long program, too long and boring. I can write two version of it

p1

void setup(){
  setup stuff
}
loop(){
  loop stuff
}

and p2

void setup(){
  setup stuff
  while true{
    loop stuff
  }
}

void loop(){
  ;
}

Now, If I add in p1 the livelock

while(true);

before closing brace of setup, all is perfect if I add the livelock instead after the opening brace of the loop the program restarts forever.

If I do the same with p2, a livelock after the setup stuff or before the loop stuff in the while then all is ok

Thanks for your time, in desperate need for help Guy

in desperate need for help

Post your REAL code. There is almost never a reason to use an infinite loop in setup() OR in loop(). Usually, doing so means that you don't comprehend the fact that loop() loops.

I use the infinite loop as a break point so i can tel where exactly the code starts to misbehave.

Since 'p1' does nothing, how can you tell it "restarts forever"?

so i can tel where exactly the code starts to misbehave.

So, why is it you are here? Obviously, something you are doing is causing unintended behavior. But, all you are doing here is waving your arms and huffing and puffing. Post your code or get over it.

Most program crashes are due to endless loops. What can you expect if you deliberately create an endless loop.

…R

@johnwasser
p1 does not do much but it does do setup stuff and i know it misbehaves because the crash result in restart=>redo the setup stuff

@PaulS ill post p1 and p2 codes when back home. Note that my prgramming classes are ~50 years old and in fortran

Do have enough RAM left?

Happened to me once when I was programming an attiny. After refactoring the tiny restarted over and over. I figured out it was out of free RAM, so I reduced the call stack depth and it worked again.

Does anyone know the events that cause an avr to restart?

Can someone tell why p1 and p2 should not be equivalent? After all, what the machine does is setup and then, forever, loop. This is exactly what p2 does except that the loop is repeated by a while at the end of setup

@thomai Interesting. Yet the two version p1 and p2 should consume ~ the same amount of RAM. Furthermore, if I reduce in p1 a global vector by 450 bytes the crashes still occurs. As for your pertinent question: I think that there are two possible sw resets 1) jump to zero (whatever the assembly syntax is) 2) enable the watchdog and forget to arm it in due time

@PaulS I am not sure what huffing and puffing means. I am attaching the codes p1.ino and p2.ino, outputs in corresponding .txt files. In both i inserted the endless loop for the sake of simplicity: I am only concerned by the difference that the two versions exhibit UP TO THE LIVELOCK. I repeat that I am far from being a software guru and my 'c' "handwriting" is that of a primary school piuple

Thanks again for your time

guy_c, meet the F() macro

I just know you're going to get on.

@AWOL I know it but, I have so many problems that I must postpone optimisation. I should not make all these Serial.printf but rather channelize it all to a 'report(text, verbosityLevel,output) so I can also redirect to e.g. a server on my pc.

It seems that I got just every possible problem including shaky hardware my logic and syntactic faults as well as strange behaviour of the IDE who wil often go on strike for loading the code

Those two output files show that neither is restarting. The first output from setup() is not repeated. The p1 output shows an odd character after "success" where p2 shows:

success
SD card initialized ok.
Found index.htm file.
Setup ended

Sorry, I unplugged the power to early to avoid flooding phant here is p1

Starting vers_2_16Apr2016 Record Pointer=64/511 (R)eset eprom ptr (D)ump (H)Halt (G)o)? Running Starting Ethernet Attempt #1 to get epoch... accessing NTP server sending ntp packet NTP success Inet= 22:43:24 local= 22:43:24 epoch 1461278604

index is 4 Logging startup message to phant posting to phant GET string is GET /input/**********?private_key=**********22:43:25_Starting_vers_2_16Apr2016 Connected sending HTTP succesStarting vers_2_16Apr2016 Record Pointer=64/511 (R)eset eprom ptr (D)ump (H)Halt (G)o)? Running Starting Ethernet Attempt #1 to get epoch... accessing NTP server sending ntp packet NTP success Inet= 22:43:28 local= 22:43:28 epoch 1461278610

index is 4 Logging startup message to phant posting to phant GET string is GET /input/**********?private_key=**********data=22:43:42_Starting_vers_2_16Apr2016 Connected sending HTTP success€

… and you can tell my timezone :slight_smile:

It crashes somewhere after sending “success” from post2phant().

In that function you declare getString to hold 100 characters.

The string you print from it is:“GET /input/XXXXXXXXXXXXXXXXXXXX?private_key=XXXXXXXXXXXXXXXXXXXX&data=20:09:28_Starting_vers_2_16Apr2016”

That looks like 105 characters to me. No wonder it crashes when you try to return from the function.

(Edit: removed access keys on request of the author)

Bingo! This is very stupid of me The two program are (perhaps) logically equivalent but their behaviour diverge when subject to memory corruption and instead of asking why the behaviour diverge I should have conclude simply that they both carry the same pathology! Thank you very much John! Good lesson for me

guy_c: @AWOL I know it but, I have so many problems that I must postpone optimisation.

I don't really view it as optimisation.

Hi, some confidential stuff was left in the posted sketches: if you downloaded either kindly erase the files