Clear array

Hello,

I want to clear an byte array

byte BytesIn[32];

In processing this is done in the following way:

byte BytesIn = null;

but this does not work...

It there a way to clear an array or to delete and recreate an array with the same name?

Thanks in advance,

/me

It depends how the array is to be used.
If it’s a string, then “BytesIn [0] = ‘\0’;” may suffice.
Otherwise, look at “memset”.

I am sending wireless data packets between arduino’s using my nfr24L01+. The amount of bytes I send varies between 1 and 32.

void TransitSerialDataGamePad() {

  //Serial read variables
  byte BytesIn[32];
  int i = 0;

  while(Serial.available() > 0){
    if (i < 33)
    {
      BytesIn[i++] = Serial.read();
      
      Serial.println(BytesIn[i]);
    }
    else
    {
      break;
    } 

  }

  if (i > 0)
  {
    SendWirelessData(BytesIn);
  }
}

A little example:

Packet 1:

byte BytesIn[32];

The array is filled with the following data:

1,2,3,4,5,6,7,8,9,10, ect…

This array is sent to the other arduino wireless.

Packet 2:

byte BytesIn[32];

The array is filled with the following data:

T,E,S,T

But the array really is:

T,E,S,T,5,6,7,8,9,10, ect… This because of the “old” data that’s still in the array.

I also want to be able to determine the size of the new array every cycle!!

memset let you set a value but I want to “destroy” the excising array and create a new one with the same name. Or just clear it.

memset just clears an array. Or you could just reset the index.

You might want to look into “dynamic array handling” - note that this won’t be easy, but here are a couple of resources:

http://forums.adafruit.com/viewtopic.php?f=8&t=9392
http://www.arduino.cc/playground/Code/DynamicArrayHelper

The two main functions used are malloc() and free(), which are used to assign and release memory structures for arrays, among other things. You will be treading into difficult waters…

Good luck.

:slight_smile:

memset(BytesIn, 0, (sizeof(BytesIn)/sizeof(BytesIn[0]));

or since 'BytesIn' is an array of single bytes:

memset(BytesIn, 0, sizeof(BytesIn));

@ Groove

memset just clears an array. Or you could just reset the index.

Memset lets you replace characters inside an array (as far as I know), and does not clear it (correct me if I am wrong):

http://www.cplusplus.com/reference/clibrary/cstring/memset/

Resetting the Index can create the following problem:

Packet 1:

byte BytesIn[32];

The array is filled with the following data:

1,2,3,4,5,6,7,8,9,10, ect..

This array is sent to the other arduino wireless.

Packet 2:

byte BytesIn[32];

The array is filled with the following data:

T,E,S,T

But the array really is:

T,E,S,T,5,6,7,8,9,10, ect... This because of the "old" data that's still in the array.

The problem is not all 33 locations of the array are filled every cycle.

@ cr0sh

You might want to look into "dynamic array handling"

That too difficult for me, but thanks :)

/me

Memset lets you replace characters inside an array (as far as I know), and does not clear it

Memset lets you replace [u]all[/u] characters inside an array with a single value. I suggest you use zero.

How else do you define "clear"?

Or, of course, there's "memclr". ;)

The array is filled with the following data:

T,E,S,T

Or just keep a count of how much you put in....

How else do you define "clear"?

I don't exactly know the definition of an empty/cleared array but:

How does the array looks like when it is defined, and no data is in the array? I assume nothing, so no "0".

This is what I call "Clear" :)

What do you call clear ?

Cheers.

/me

memset(BytesIn, 0, sizeof(BytesIn));

Is still valid. Given the address of the buffer 'BytesIn' store a 0 at that address to the address of its last entry.

Zero was chosen as it marks the end of a 'C' string.

How does the array looks like when it is defined, and no data is in the array? I assume nothing, so no "0".

If the array is an automatic, when it is declared it has whatever junk was in the stack frame that it occupies. If you "malloc"ed the array, it contains whatever junk was on the heap. If you "calloc"ed it, it was initialised to zeroes. If it was "static" or had global scope, then it was initialised to zero before "main" ran.

At no point was it ever "empty" - memory was allocated, and that memory contained something, even if the something was the value zero..

How you manage what defines "empty" is down to you - as has been pointed out, if you're managing a C string, then a simple "myBuffer[0] = '\0';" will suffice. It may be that setting the index ([u]your[/u] index) into the buffer that you increment when you put in a new character to zero will suffice, since you know when [u]you[/u] have filled the buffer (whatever that means to [u]you[/u]) as far as you want, and want to move on to processing that buffer.

I can't decide if you're trying to make some philosophical point about the relative merits of buffer management in different languages and architectures, or you simply don't understand the nature of the problem you think you face.

What wrong with a simple loop filling each byte with whatever value you want ? e.g. nulls.

I can’t decide if you’re trying to make some philosophical point about the relative merits of buffer management in different languages and architectures, or you simply don’t understand the nature of the problem you think you face.

I just want to know how array’s work and what’s considered as an “Empty” array. Your explanation makes sense and explains everything to me, so thanks a lot.

To filling the array with “0” makes a lot of sens now to me!! And that’s what I’ll do.

Thanks!!

/me