[solved] Memset seemingly not working

Using virtual wire, I am sending and receiving data between two arduinos. I clear the array in which I store the values using memset after processing the message. After running for a bit, memset appears to not work anymore. When I print the contents of the array after the memset function has been called, it is not filled with 0’s like I am setting. Any insight into this? I know it is on the receiver end and the transmitter is sending the correct data. I am transmitting data on a frequency that is used for various other things in my environment so it seems to sometimes “receive” a message I never sent. It is after this that memset quits working. I am unable to change the frequency I’m working at, but my code should be able to handle an incorrect message.

#include <VirtualWire.h>
uint8_t BufSize = 6;

void setup()
{ 
  vw_set_ptt_pin(8);
  vw_set_rx_pin(12);
  vw_setup(4000);  // Bits per sec
  
  vw_rx_start();       // Start the receiver PLL running
    
  Serial.begin(9600);
  Serial.println("Ready");
}

void loop()
{
  byte B1Buf[BufSize];
  
  Serial.println("Listening");
  while(!vw_have_message())
  {
    //Serial.println("Waiting");
  }
  vw_get_message(B1Buf, &BufSize);
  for (int i = 0; i < 6; i++)
    {
      int x = B1Buf[i];
      Serial.print(x);
    }
    Serial.println();

/*/////////////////////////////////////////
 Call function to process message received
////////////////////////////////////////////
*/
    
    memset(B1Buf, 0, sizeof(B1Buf));
  
    for (int i = 0; i < 6; i++)
    {
      int x = B1Buf[i];
      Serial.print(x);
    }

/* Initially this will print that all contents have successfully been set to 0. However, after running for
a bit (time varies), this no longer works and the the array still contains the received values.
*/
    
    Serial.println();
  
}

Why are you concerned about clearing a local variable after you've used it? The variable then immediately goes out of scope.

Proof, of course, is missing from your post. As is, clearly, a lot of code.

vvote:

vw_get_message(B1Buf, &BufSize);

In the second parameter provided by '&BufSize' the function will return the number of bytes actually received.

So if your programm will accidentally receive less than 6 Bytes, then the "BufSize" variable will contain different value from then on and not '6' anymore.

So what?

If your program needs constants to be constant all the time, you'd better declare them 'const':

const uint8_t BufSize = 6;

And then ("a miracle, oh miracle!") the variable 'BufSize' will not even be accepted as a parameter by the vw_get_message function, which would show you, that there is a logical problem with your code.

The memset function will (of course) work all the time.
But in your programming logic 'BufSize' is not always 6.

PaulS:
Why are you concerned about clearing a local variable after you've used it? The variable then immediately goes out of scope.

What? And leave behind a "messy" stack frame? What would your mother say? :slight_smile:

Regards,
Ray L.

I’m not sure I understand the root cause of why the phantom message corrupts memset, but you can initialize the rx buffer when declared in the loop, and you won’t need to use memset. Try the explicit initialization, and see if you can still see the uncleared data.

include <VirtualWire.h>
uint8_t BufSize = 6;

void setup()
{ 
  vw_set_ptt_pin(8);
  vw_set_rx_pin(12);
  vw_setup(4000);  // Bits per sec
  
  vw_rx_start();       // Start the receiver PLL running
    
  Serial.begin(9600);
  Serial.println("Ready");
}

void loop()
{
  byte B1Buf[BufSize]= {};//explicitly initialize to 0

//move your test print here
 for (int i = 0; i < 6; i++)
    {
      int x = B1Buf[i];
      Serial.print(x);
    }

  
  Serial.println("Listening");
  while(!vw_have_message())
  {
    //Serial.println("Waiting");
  }
  vw_get_message(B1Buf, &BufSize);
  for (int i = 0; i < 6; i++)
    {
      int x = B1Buf[i];
      Serial.print(x);
    }
    Serial.println();

/*/////////////////////////////////////////
 Call function to process message received
////////////////////////////////////////////
*/
    
 //don't  use memset
/*
   memset(B1Buf, 0, sizeof(B1Buf));
  
    for (int i = 0; i < 6; i++)
    {
      int x = B1Buf[i];
      Serial.print(x);
    }
*/

/* Initially this will print that all contents have successfully been set to 0. However, after running for
a bit (time varies), this no longer works and the the array still contains the received values.
*/
    
    Serial.println();
  
}

Thank you, jurs, for your help. I figured it wasn't actually an issue with memset, which is why I said it "seemingly" wouldn't work. I was just struggling to find the logic error. Sometimes a fresh set of eyes is the best help.

cattledog, jurs pointed out the root cause. To get around this logic error, I was able to migrate to RadioHead instead of VirtualWire...something I should've done a long time ago but was reluctant since I had already started there.

Thanks!

What do you do with a buffer full of zeros?