Pages: [1]   Go Down
Author Topic: Clear array  (Read 8723 times)
0 Members and 1 Guest are viewing this topic.
The Netherlands (Rotterdam)
Offline Offline
Full Member
***
Karma: 1
Posts: 169
Arduino rocks!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello,

I want to clear an byte array
Code:
byte BytesIn[32];

In processing this is done in the following way:

Code:
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

« Last Edit: June 20, 2010, 03:19:40 pm by OpenSource » Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 239
Posts: 24373
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It depends how the array is to be used.
If it's a string, then "BytesIn
  • = '\0';" may suffice.
    Otherwise, look at "memset".
« Last Edit: June 20, 2010, 03:22:34 pm by AWOL » Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

The Netherlands (Rotterdam)
Offline Offline
Full Member
***
Karma: 1
Posts: 169
Arduino rocks!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
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.
« Last Edit: June 20, 2010, 03:43:24 pm by OpenSource » Logged

UK
Offline Offline
Faraday Member
**
Karma: 16
Posts: 2883
Gorm deficient
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

memset just clears an array.
Or you could just reset the index.
« Last Edit: June 20, 2010, 03:49:48 pm by GrooveFlotilla » Logged

Per Arduino ad Astra

Phoenix, Arizona USA
Offline Offline
Faraday Member
**
Karma: 36
Posts: 5519
Where's the beer?
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

 smiley
Logged

I will not respond to Arduino help PM's from random forum users; if you have such a question, start a new topic thread.

Des Moines, WA - USA
Offline Offline
God Member
*****
Karma: 25
Posts: 779
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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


memset(BytesIn, 0, sizeof(BytesIn));
« Last Edit: June 20, 2010, 07:01:43 pm by lloyddean » Logged

The Netherlands (Rotterdam)
Offline Offline
Full Member
***
Karma: 1
Posts: 169
Arduino rocks!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

@ Groove

Quote
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

Quote
You might want to look into "dynamic array handling"

That too difficult for me, but thanks smiley

/me
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 239
Posts: 24373
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Memset lets you replace all 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".   smiley-wink

Quote
The array is filled with the following data:

T,E,S,T
Or just keep a count of how much you put in....
« Last Edit: June 21, 2010, 02:43:42 pm by AWOL » Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

The Netherlands (Rotterdam)
Offline Offline
Full Member
***
Karma: 1
Posts: 169
Arduino rocks!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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" smiley

What do you call clear ?

Cheers.

/me
Logged

Des Moines, WA - USA
Offline Offline
God Member
*****
Karma: 25
Posts: 779
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
« Last Edit: June 21, 2010, 03:21:25 pm by lloyddean » Logged

UK
Offline Offline
Faraday Member
**
Karma: 16
Posts: 2883
Gorm deficient
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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 (your index) into the buffer that you increment when you put in a new character to zero will suffice, since you know when you have filled the buffer (whatever that means to you) 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.
« Last Edit: June 22, 2010, 01:32:59 am by GrooveFlotilla » Logged

Per Arduino ad Astra

Australia
Offline Offline
Sr. Member
****
Karma: 10
Posts: 397
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

The Netherlands (Rotterdam)
Offline Offline
Full Member
***
Karma: 1
Posts: 169
Arduino rocks!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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

Logged

Pages: [1]   Go Up
Jump to: