[SOLVED]Strange behaviour with regards to use of Char Arrays passing to function

Hello Ladies and Gentlemen,

I am trying to get my head around using char arrays. I come from a C# background and OOP. But I trying my best to understand it all.

I have a snippet of code, that I have placed in my main programs loop.

It calls a function to do a VERY rudimentary encryption:

This is how I call the function:

//test Encode 
  //====================
   char s[128];
   memset(s,'\0',sizeof(s));
   strncpy(s,"welcome to my world!",128);
   
   DEEncryptStr(s,ENCRYPT,sizeof(s)) ;
   Serial.print("Enc:");Serial.println(s);

  // egg(s);
  
  //=======================================
  //decode it
  
   DEEncryptStr(s,DECRYPT,sizeof(s)) ;
   Serial.print("Dec:");Serial.println(s);

Now, when I run it like that, (with the egg(s) function commented out) it runs just fine and produces this Serial output:

Enc:ˆíöðóý÷º«ä°²ë¦¨çâá÷ºÞÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ
Dec:welcome to my world!
Enc:ˆíöðóý÷º«ä°²ë¦¨çâá÷ºÞÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ
Dec:welcome to my world!
Enc:�èóõöøò¿®áµ·î£­âçäò¿Û

However, when I uncomment the egg function and pass the char pointer to the egg function, the decryption section of my sketch returns rubbish.

Here is my Encryption/Decryption function:

void DEEncryptStr(char* msg,byte dowhat,int lenBytes) 
{
  //Get the last 5 second gap
  int ss=(now()/5)*5;
  //turn the seconds into a char array of yyymmddhhmmss
  char sstime[13];
  sprintf(sstime, "%d", ss);
  
  
  char key[]="ABCDEFG1eewd!HIJ";    //todo:get from eeprom
  //make an improved key by adding the latest seconds
  char improvkey[sizeof(key)+20];
  strcpy(improvkey,key);
  strcat(improvkey, sstime);

  char seed='\0';
  
  if(dowhat==ENCRYPT)
  {
    //strcat(improvkey, ssr);   //add the random number to the improvkey
   
    //xor the string by the key
    for(int x=0; x<lenBytes-1; x++)
    {
      improvkey[0]=seed;
      seed=msg[x];
      for(int xx=0; xx<sizeof(improvkey)-1; xx++)
      {
        msg[x]=msg[x]^improvkey[xx];
      }
    }
    msg[lenBytes-1]='\0';
  }
  
  //decrypting section
  //-------------------
  if(dowhat==DECRYPT)
  {
    seed='\0';
    //xor the string by the key
    for(int x=0; x<lenBytes-1; x++)
    {
      improvkey[0]=seed;
      //seed=msg[x];
      for(int xx=0; xx<sizeof(improvkey)-1; xx++)
      {
        msg[x]=msg[x]^improvkey[xx];
      }
 
      //update the seed only AFTER the HRF version is avail.
      seed=msg[x];

    }
    msg[lenBytes-1]='\0';
  }
}

and here is the egg function:

void egg(char* s)
{
  char t[129];
  memset(t,'\0',sizeof(t));
  strcpy(t,s);
  t[128]='\0';
  Serial.print("Egg:");
  Serial.println(t);
  
}

So, after all of that, my question is :

What am I doing wrong when I sent the s char to the egg function that changes it ? I assume I am messing with the memory allocation?? Why does the damn thing decrypt just fine when I do nothing with the char called s, but as soon as I pass it around it doesnt decrypt???

Any help would be fabulously appreciated!

Regds

Paul

Snippets of your code don’t help much, but maybe you are running out of SRAM?

SurferTim: Snippets of your code don't help much, but maybe you are running out of SRAM?

Thanks SurferTim,

I will take a look at that!

I happen to have a sketch open with the freeRam function in it. Here is what I use. Call it just before you call the egg function.

int freeRam() {
  extern int __heap_start,*__brkval;
  int v;
  return (int)&v - (__brkval == 0 ? (int)&__heap_start : (int) __brkval);  
}

// then call it like this
Serial.print(F("SRAM free: "));
Serial.println(freeRam());

SurferTim: Snippets of your code don't help much, but maybe you are running out of SRAM?

Hi Tim,

Thanks, it's solved! indeed I was declaring the char array in the loop and obviously running out of SRAM

I now declared S as a global and it works fine!

Now...how do I mark this answer as solved???

Many thanks!

Paul

You can go back to your first post and edit it. You should be able to change the subject line to add [SOLVED].

SurferTim: I happen to have a sketch open with the freeRam function in it. Here is what I use. Call it just before you call the egg function.

int freeRam() {
  extern int __heap_start,*__brkval;
  int v;
  return (int)&v - (__brkval == 0 ? (int)&__heap_start : (int) __brkval);  
}

// then call it like this Serial.print(F("SRAM free: ")); Serial.println(freeRam());

Awesome MANY MANY MANY thanks Tim!!!!!!!!