Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Clear an ARRAY in one shot. Is it possible ? Plz teah me how.

Status
Not open for further replies.
Hi,

This clearing array thing is really bugging me for few days now. In my sign board project I am using a 64 byte array. So after all the elements have been processed. I want to clear the array.

So normally what I am doing is start a loop.

What I want to know, Is there a one shot solution for that. Or what the other perfect methods ?

Thanks
 
Either a loop, or 64 consecutive writes - it depends if speed or code size is important?.

However, either method is extremely fast - certainly compared with the slow speeds of the real world.
 
Hi,

This clearing array thing is really bugging me for few days now. In my sign board project I am using a 64 byte array. So after all the elements have been processed. I want to clear the array.

So normally what I am doing is start a loop.

What I want to know, Is there a one shot solution for that. Or what the other perfect methods ?

Thanks

hi,
When you have cleared the array, do you write new data to the array.?
 
I'm curious too. Why do you need to clear this array? Is this the display buffer?
 
Last edited:
Usually you would do this with buffer pointers or a circular buffer. Instead of deleting anything you would just set the end of buffer pointer back how many character you want to delete.
 
what compiler ? in C18 you can simply use "memset"
**broken link removed**
im sure memset is in other compilers... .check the manuals...

In your case:
Code:
memset(MyArray,0x00,64);

Not sure if its better but it will surely make your code look better with 1 line of code and not 3 for a loop
 

Attachments

  • memset..jpg
    memset..jpg
    122.5 KB · Views: 164
Last edited:
i found that about 2-3 months ago and ive been messing with C18 for a year or 2 heh. Imagine me... i need to actually read up on these things too :D

I actually need some regular electronics catchup... i went straight to micros never learned analog circuits or digital ones without a micro heh...
 
i found that about 2-3 months ago and ive been messing with C18 for a year or 2 heh. Imagine me... i need to actually read up on these things too :D

I actually need some regular electronics catchup... i went straight to micros never learned analog circuits or digital ones without a micro heh...

At the moment Im also improving my analog electronic side. :)
 
I buy at least 1 time a week from them.. Great prices and shipping and perfect support... I recommend it 100%...

A course here? where ? on the forum or from where your located?
 
if you are using a 16 or 32 bit processor, you should align your array to such a boundary and set the array using the natural word (2 or 4 bytes) rather than byte. For example, if you are running a 32 bit processor this:

// get start of array
unsigned int *currentPos = (unsigned int *)myClearedArray;

// get end of array
unsigned int *endPos = (unsigned int *)&myClearedArray[sizeof(myClearedArray) - sizeof(unsigned int)];

// set the array to some 32 bit value, 4 bytes at a time
while (currentPos++ != endPos) *currentPos = 0x00000000;

would set the array 4x faster than doing the same byte by byte. More advanced memset implementations strive to do the same with the added cost of checking the block for alignment. Set byte by byte until natural alignment is reached, then set word by word, then finish off byte by byte if applicable.
 
of course the loop is faster...anytime you call a subroutine from a function library, you push and pull to and from the stack, as well as jump to and from the sub. Also, it probably pushes and pulls between EACH memory location, instead of looping as pretty as you can. One fun exercise, is to compile with a listing so you can see what code the functions are running. One project I worked on in teh 90s used a motorola QUICC (mc68360)... we were running at 25MHz and was sending and receiving 10Mbit ethernet better than a 500MHz pentium running windows... because we were using assembly and didn't have to do DOS calls... among others...
 
You could just do a for loop,
Code:
    for(i=0;i<64;i++)
        Message[i]=0;
or, use a pointer to make it faster,
Code:
ram char *pointer;
    pointer=&Message[0];
    for(i=0;i<64;i++)
        *pointer++=0;

Mike.
 
Status
Not open for further replies.

Latest threads

Back
Top