CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

short and simple

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Gabriel



Joined: 03 Aug 2009
Posts: 1067
Location: Panama

View user's profile Send private message

short and simple
PostPosted: Thu May 13, 2010 5:44 am     Reply with quote

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

View user's profile Send private message

PostPosted: Thu May 13, 2010 7:22 am     Reply with quote

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

View user's profile Send private message

PostPosted: Thu May 13, 2010 8:15 am     Reply with quote

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

View user's profile Send private message

PostPosted: Thu May 13, 2010 9:08 am     Reply with quote

@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: 19504

View user's profile Send private message

PostPosted: Thu May 13, 2010 9:16 am     Reply with quote

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

View user's profile Send private message

PostPosted: Thu May 13, 2010 9:31 am     Reply with quote

Ttelmah: thats Brilliant! thank you so much... brilliant.
_________________
CCS PCM 5.078 & CCS PCH 5.093
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group