Can a code go into infinite loop within void setup()

Hi,

I have been trying to implement a piece of code but something strange is happening. Arduino is not resetting. Even though I don’t have any code within the void loop() part the code is going into infinite loop. Even after the IDE shows done compiling, the L and TX LED keeps on blinking.

Can anybody tell me the reason behind this strange behavior.

I had a problem with this one time when I was messing around with the built in watchdog function. Can you post your code?

Please post your code...

while ( !( posted code)); // wait, possibly forever

# include <stdio.h>

unsigned char A[21] = {0X36,0X3E,0X34,0XE8,0X37,0X46,0X99,0XD4,0X68,0X11,0X99,0XA0,0X7E,0XD5,0XA2,0X86,0X62,0XA1,0XEB,0XF0,0X03};
unsigned char B[21] = {0XF1,0X24,0X73,0X79,0X0C,0X5C,0X1C,0XB1,0X45,0XD5,0XCD,0XA2,0X4F,0X09,0XA0,0X71,0X6C,0XBC,0X1F,0XD5,0X00};
unsigned char C[21] = {0X01,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00};
unsigned char T1[21];
unsigned char T2[21];
unsigned char D[21];
unsigned char E[21];
unsigned char F[21];

void add (unsigned char* number1, unsigned char* number2)
{
  int i;
  for (i=0; i<21; i++)
  {
    number2[i] = number1[i] ^ number2[i];
  }
}

void sqr(unsigned char* number, unsigned char* output)
{
  int i,m,k,t1,t2;
  unsigned char pr[41];
        for (i=0; i<41; i++)
	{
                k=4;
		for (m=0; m<8; m+=2)
		{	
                        if ((i%2) != 0)
                        {
                        t2 = bitRead(number[(i-1)/2],k);
                        pr[i] = bitWrite(pr[i],m,t2);
                        k++;
                        }
                        else
                        {
                         t1 = bitRead(number[i/2],(m/2));
                         pr[i] = bitWrite(pr[i],m,t1);
                        }
			
		}
	}
        

 for(i=0; i<21; i++)
        {
          output[i] = pr[i];
        }
	
}


void mul(unsigned char *a, unsigned char *b, unsigned char *output)
{
        int i,j,m,t,bi;
	int x=0, y=0, z = 0;
        unsigned char pr[41];

// first nested loop generating the first 4 columns

	for (i=0; i<21; i++)
	{
		for (j=0; j<=i; j++)
		{
			for (m=0; m<8; m++)
			{
                                bi = bitRead(b[i-j],m);
                                if (bi == 0)
                                {
                                  t = 0;
                                }
                                else 
                                  t = a[j];
				z = z ^ (t<<m);
			}
                        x = x ^ z;
			z = 0;
		}
		y = x>>8; 
		pr[i] = x&0XFF; 
		x = y; 
	}

// second nested loop generating the last 3 columns
	for (i=21; i<41; i++)
	{
		for (j=i-20; j<21; j++)
		{
			for (m=0; m<8; m++)
			{
                                bi = bitRead(b[i-j],m);
                                if (bi == 0)
                                {
                                  t = 0;
                                }
                                else 
                                  t = a[j];
				z = z ^ (t<<m);
			}
                        x = x ^ z;
			z = 0;
		}
		y = x>>8; 
		pr[i] = x&0XFF; 
		x = y; 
	}
	pr[41] = y;

        for(i=0; i<21; i++)
        {
          output[i] = pr[i];
        }
        
	
}

void dble(unsigned char* number1, unsigned char* number2, unsigned char* number3,unsigned char* number4, unsigned char* number5, unsigned char* number6) 
{  
  int i,j;
  int test = 0;
  unsigned char dummy1[21] = {0X01,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00};
  
/*for (j=0; j<21; j++)
 {
   if (number1[j] != dummy1[j])
   {
     test = 1;
     break;
   }
   if (number2[j] != 0X00)
   {
     test = 1;
     break;
   }
 }
 Serial.println(test);
 
 if (test == 0)
 {
  for (i = 0; i<21; i++)
 {
  number4[i] = dummy1[i];
  number5[i] = 0X00;
  number6[i] = 0X00;
 }
return;
 }
  */
  sqr(number3, T1); 
  sqr(number1, T2);
  mul(T1, T2, number6);
  sqr(T2, number4);
  sqr(T1, T1);
  add(T2,number4); 
  sqr(number2, T1);
  add(number6,T1); 
  add(T2,T1); 
  add(T1, number5);
  
}

void setup()
{
  Serial.begin(115200);
  int i;
  dble(A, B, C, D, E, F);
  
  for (i=20; i>=0; i--)
  {
   Serial.print(D[i], HEX);
   Serial.print(" ");
  }
  Serial.print("\n");
  for (i=20; i>=0; i--)
  {
   Serial.print(E[i], HEX);
   Serial.print(" ");
  }
  Serial.print("\n");
  for (i=20; i>=0; i--)
  {
   Serial.print(F[i], HEX);
   Serial.print(" ");
  }
}
void loop() {
  
}

In the void dble() function if I remove the comments in the commented out parts, the code is going into infinite loop and Arduino is not resetting. The TX led keeps on blinking.

If I keep the comments I am getting output. But If I close the serial monitor and open it again the output changes

I quick skim through doesn't show any obvious causes, but the symptoms suggest you're running out of memory or corrupting the memory. It may be that the corruption is occurring in a bit of code unrelated to the part you are commenting out.

Sounds like a cheap answer, but granted all those loops, I wouldn't be surprised if you are running out of memory.

What memory are you talking about? Is it the Flash memory or the RAM.

The shown binary sketch size is about 1%

RAM, you only have 2k to play with on a '328P.

I am working on Mega 2560. It has 8K RAM i guess. But anyways how can I minimise the memory size?