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 ?
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 ?
Hi, no its not the display buffer. I have a ps2 key board interfaced. The array is for store the data entered. So if I hit a key like "DEL" I want to clear the array.
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.
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...