calling setup() again and again

Hi everyone,
I am new to arduino software. I am having an Atmega328P board.
I am trying to execute a SHA-3 candidate “Cubehash”.
When i simulate a short message, evrything works fine.
But when i simulate a long message, execution fails at memset() and calls setup again.
So the output for the following code is

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <avr/pgmspace.h>
#include "cubehash.h"




void setup(){
    Serial.begin(9600);
 
    int i,ret_val;
 //   Serial.flush();
    Serial.println("start");
    ret_val=genShortMsg(224);
  
}





void loop() 
{
 
}

STATUS_CODES
genShortMsg(int hashbitlen)
{

      int                  msglen, msgbytelen;
      BitSequence      Msg[4228], MD[64];

            msglen=2111;      
            msgbytelen = (msglen+7)/8;
      


      int            ch, started;
      BitSequence      ich;
      int i;
      if ( msgbytelen == 0 ) {
            Msg[0] = 0x00;
            
      }
        Serial.println(msgbytelen);
        Serial.println("reached 1");
      //memset(Msg, 0x00, msgbytelen);
        for(i=0;i<msgbytelen;++i){
            Serial.println(i);
            Msg[i]=0x00;
        }
        
        Serial.println("reached 2");
      started = 0;
      //static char str[] PROGMEM= "14B06DD54EB364";
      static char str[] PROGMEM="919FE5E7F35F64A7487649E564771DBBF10AE204ECC2181312D1A79FB579297C94F0DB9EAAE9E009A4F02057AF2C973C5DAFA7B60154371A5D2C8E992FB6429176F8424B1A866BC1D1BED00438E97FAB42040DCACDEF7CA9FC2033059B8898BB40CCFB2634B051797BDF3B915C503EC81839AD01E0F4F2F871EFF2008D40011730BE7A47888E7955A806876BE120CB0F3A139A3620154ECC6482A70F5629F6A9D3341BE6FBBF48E5AA0C53589A04F057DD44268AFFCABF75ADFC549F73F454264D46A98CCA80E3000C7446853DD5B430C9344E87E3230555B09FB3E7E64B5AD3989293AC0FEEC0E75F909696F028A5525D26DDEA5D2B2C813FB3613DFF38CE23209285CC77C60860";
      int j;

      for(j=0;j<strlen(str);j++){
            ch=pgm_read_byte(&(str[j]));
               // Serial.print(ch,BYTE);
                  if ( (ch >= '0') && (ch <= '9') )
                        ich = ch - '0';
                  else if ( (ch >= 'A') && (ch <= 'F') )
                        ich = ch - 'A' + 10;
                  
                  else if ( (ch >= 'a') && (ch <= 'f') )
                        ich = ch - 'a' + 10;
                  
                  for ( i=0; i<msgbytelen-1; i++ )Msg[i] = (Msg[i] << 4) | (Msg[i+1] >> 4);
                  Msg[msgbytelen-1] = (Msg[msgbytelen-1] << 4) | ich;
            
      }
  Serial.println("");
      
      Hash(hashbitlen, Msg, msglen, MD);

/*      
            
                for(i=0;i<hashbitlen/8;++i){
                  if(MD[i] < 16){
                       Serial.print(0,HEX);
                      Serial.print(MD[i],HEX);
                  }
                  else{
                     Serial.print(MD[i],HEX);
                  }
                }
                Serial.println("");
                
    //    Serial.flush();*/
      return KAT_SUCCESS;
}

–Arpan

Behaviour like that sounds like overrunning RAM. A 328P has only 2K of RAM, some of which is required for the stack.

Your declaration BitSequence Msg[4228] looks like it may cause "issues".

@Steve S
Thankyou very much Steve for having a look at my problem.

The problem is not with
BitSequence Msg[4228];
Because if i use the long msg(commented one) and keep msglen to 55, it works fine.
The problem is at increasing “msglen”.
How do i know that?
Well, see its not printing “reached 2”, so i replaced
memset()
with a loop
for(i=0;i<msgbytelen;++i){
Serial.println(i);
Msg*=0x00;*
}
then in the output i see “i” going upto 191 and then calling setup again.
So the problem is its not setting Msg to “0” upto required length (in this case it is (2111+7)/8 = 264) and fails at setting Msg[191]=0 and somehow resets.
–Arpan

Can you post the code you're actually using, please, the one earlier has an empty "loop".

Please use the "#" button when posting code.

@Groove Hi, I have kept the loop empty in orignal code also. Also, there is the code part for Hash(...) appended, which i have not shown

Well I think since the Msg fails at some "i" and it resets the code , may be it is trying to access the restricted area. But i don't understand how can be it restricted, when the code passes the declaration "Bitsequence Msg[4228]" smoothly.

Am I missing something here?

void setup(){
   Serial.begin(9600);

   int i,ret_val;
   Serial.flush();
   Serial.println("start");

}





void loop() 
{

}

As has already been pointed out, this:

     BitSequence      Msg[4228], MD[64];

Can never work on a 168 or 328.

@Groove ohh i really sorry! There is a call to genShortMsg() in the setup()

so the code goes like this:

void setup(){
    Serial.begin(9600);

    int i,ret_val;
 //   Serial.flush();
    Serial.println("start");
    ret_val=genShortMsg(224);
  
}





void loop() 
{

}

But i don't understand how can be it restricted, when the code passes the declaration "Bitsequence Msg[4228]" smoothly

All that does is tells the compiler that a stack frame with sufficient space for 4228 "Bitsequence"s has to be reserved. The compiler doesn't know how much memory isn't available. Neither, as it happens, does the processor, when the code is executed.

But i don’t understand how can be it restricted, when the code passes the declaration
“Bitsequence Msg[4228]” smoothly

All that does is tells the compiler that a stack frame with sufficient space for 4228 "Bitsequence"s has to be reserved. The compiler doesn’t know how much memory isn’t available.
Neither, as it happens, does the processor, when the code is executed.

ok! this thing i got it!
so the problem is there is not enough memory available?
so whats the way to execute longer messages on the board.

Thanks a lot Groove!

Problem Solved :) :) Actually i changed the declaration from

BitSequence Msg[4228]

to

BitSequence Msg["desired range"]

where desired range is only "msgbytelen"

and

PROBLEM SOLVED

--Thanks Groove and Steve Regards Arpan