View previous topic :: View next topic |
Author |
Message |
Gabriel
Joined: 03 Aug 2009 Posts: 1067 Location: Panama
|
short and simple |
Posted: Thu May 13, 2010 5:44 am |
|
|
hello all,
i have an char array [70] which i use in multiple functions.
i would like for each function to start with a clean array....
and by clean i mean all elements = 0 or ''
is there somthing like "array[70] ={'0'}"
where all elements reset to cero?
( i know -> array[70] ={'0'} is unreal, its just an example)
im trying to avoid loops... imagine clrf for a whole array.
gabriel _________________ CCS PCM 5.078 & CCS PCH 5.093 |
|
|
Wayne_
Joined: 10 Oct 2007 Posts: 681
|
|
Posted: Thu May 13, 2010 7:22 am |
|
|
I would first ask WHY you need an array which is initialised to zero ?
There is no way to ensure that all elements are zero without looping through and clearing each element.
calloc is used to clear a block of memory using assembler but this will still loop through all memory locations to do it.
I have found a lot of code that clears the array before use is not required.
An option would be to use EEprom and perform an erase on that before use but generally EEprom and flash erase to 0xFF and even then using flash or EEprom is a lot slower than ram. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19496
|
|
Posted: Thu May 13, 2010 8:15 am |
|
|
memset
Code: |
memset(&array,0,sizeof(array));
|
It is more effiecient than a simple loop, since it uses the table write operation to loop through the size requested. However a loop and write, is 'at heart' the only way to do it.
Best Wishes |
|
|
Gabriel
Joined: 03 Aug 2009 Posts: 1067 Location: Panama
|
|
Posted: Thu May 13, 2010 9:08 am |
|
|
@wayne_:
its a circular buffer i use in multiple functions... i search for things in it and so on, but the "problem" is that each function that uses it, uses a diferent number of char in the array and the strings they buffer in that array all have common items...so in theory one function could return it found what it was looking for, but in reality it was something placed there by a diferent function.... since its a shared buffer.... i can share it because only one function uses the buffer at the time... so no multiple writes....or counter mixups....
my code works as is.... just trying to elimininate possible logic errors that might arise in the future...
im cleaning up my sms routines etc....having them start with a clean buffer sounded like a good idea....
Ttelmah... thanks anyways....
i guess loop is the solution...
if you guys are still reading this....
is this even possible?:
while(( i==0) || (delay_ms(1000)))
as in "do this while i remains 0 or until the delay has been reached"? . . .
thoughts?
gabriel _________________ CCS PCM 5.078 & CCS PCH 5.093 |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19496
|
|
Posted: Thu May 13, 2010 9:16 am |
|
|
The way to code this type of thing, is to 'segment' the delay.
So something like:
Code: |
ctr=0;
while (i==0 && ctr<100) {
delay_ms(10);
ctr++;
}
|
As a general comment on your other question, why do the buffer contents matter?. If as you say, this is used for various 'circular' buffers, all that needs to 'zero' when you enter the different routines, are the buffer pointers. The contents don't matter at all...
Best Wishes |
|
|
Gabriel
Joined: 03 Aug 2009 Posts: 1067 Location: Panama
|
|
Posted: Thu May 13, 2010 9:31 am |
|
|
Ttelmah: thats Brilliant! thank you so much... brilliant. _________________ CCS PCM 5.078 & CCS PCH 5.093 |
|
|
|