Yahoo Groups archive

AVR-Chat

Index last updated: 2026-04-28 22:41 UTC

Thread

Clearing Arrays In C

Clearing Arrays In C

2007-04-17 by kernels_nz

Hi guys,

I was writing some software for a scrolling message board when I came
across the problem of clearing arrays during run-time in C.

Usually I do something like:

unsigned char array1[100];
unsigned char count;

for (count = 0; count < 100; count++){
  
  array1[count] = 0;
}

Has anybody ever come across a faster or more interesting way of
accomplishing this ?

Cheers
Hein B
Auckland, New Zealand

Re: [AVR-Chat] Clearing Arrays In C

2007-04-17 by Mike Harrison

On Tue, 17 Apr 2007 09:58:09 -0000, you wrote:

>Hi guys,
>
>I was writing some software for a scrolling message board when I came
>across the problem of clearing arrays during run-time in C.
>
>Usually I do something like:
>
>unsigned char array1[100];
>unsigned char count;
>
>for (count = 0; count < 100; count++){
>  
>  array1[count] = 0;
>}
>
>Has anybody ever come across a faster or more interesting way of
>accomplishing this ?

Faster would generally depend on how clever the compiler is, but giving it a helping hand with
something like

for (count = 0; count < 100; array1[count++]=0);

may help as it might encourage the compiler to use an auto-incrementing addressing mode. Maybe. 
Partially unrolling the loop is also likely to help significantly, at the cost of code space.

Re: [AVR-Chat] Clearing Arrays In C

2007-04-17 by John Clymer

If you want to clear the whole thing, use memset:

memset(array1, 0, sizeof(array1));

For the AVR, this shouldn't save appreciable time.  For larger 
architectures, this can result in much faster execution, as this can be 
extended out to 16, 32 and 64 bit fills rather than 8 bit fills.  This 
becomes more important if you are looking at making the code 
semi-portable (yes, it is embedded, so portability is somewhat of a myth.)

John C

kernels_nz wrote:
Show quoted textHide quoted text
>
> Hi guys,
>
> I was writing some software for a scrolling message board when I came
> across the problem of clearing arrays during run-time in C.
>
> Usually I do something like:
>
> unsigned char array1[100];
> unsigned char count;
>
> for (count = 0; count < 100; count++){
>
> array1[count] = 0;
> }
>
> Has anybody ever come across a faster or more interesting way of
> accomplishing this ?
>
> Cheers
> Hein B
> Auckland, New Zealand
>
>

Re: [AVR-Chat] Clearing Arrays In C

2007-04-17 by David Kelly

On Tue, Apr 17, 2007 at 09:58:09AM -0000, kernels_nz wrote:
> Hi guys,
> 
> I was writing some software for a scrolling message board when I came
> across the problem of clearing arrays during run-time in C.

Why bother clearing the array? Why not set count = 0 and simply
overwrite the old values. Possibly set array1[0] = 0; if you are using
it as a C string.

-- 
David Kelly N4HHE, dkelly@HiWAAY.net
========================================================================
Whom computers would destroy, they must first drive mad.

Re: [AVR-Chat] Clearing Arrays In C

2007-04-17 by dlc@frii.com

How about memset?

void * memset ( void * ptr, int value, size_t num );

usage:

#include <stdio.h>
#include <string.h>

int main ()
{
  memset (str,'-',6);
}

DLC
Show quoted textHide quoted text
> Hi guys,
>
> I was writing some software for a scrolling message board when I came
> across the problem of clearing arrays during run-time in C.
>
> Usually I do something like:
>
> unsigned char array1[100];
> unsigned char count;
>
> for (count = 0; count < 100; count++){
>
>   array1[count] = 0;
> }
>
> Has anybody ever come across a faster or more interesting way of
> accomplishing this ?
>
> Cheers
> Hein B
> Auckland, New Zealand
>
>
>
>
> Yahoo! Groups Links
>
>
>
>

Move to quarantaine

This moves the raw source file on disk only. The archive index is not changed automatically, so you still need to run a manual refresh afterward.