Page 1 of 1

Fastest way to clear a block of memory

PostPosted: Jul 25, 2002 @ 9:54pm
by fzammetti
Hey guys... general question here... if I allocate a block of memory with...

unsigned short int* memBlock = (unsigned short int*)malloc(mbSize);

What would be the FASTEST way to clear said block of memory? Not deallocate it, but write all 0's to it (or some arbitrary value, but 0 is what I'm thinking).

I'm assuming that...

for (int i = 0; i < mbSize; i++) {
*(memBlock + i) = 0;
}

..would NOT be the fastest way, right?

Thanks all!

PostPosted: Jul 25, 2002 @ 10:03pm
by BurningSheep
I think memset is the fastest way to clear the memory

memset(memblock, 0, mbSize);

PostPosted: Jul 26, 2002 @ 9:03am
by refractor
If you mean you'd like the memory cleared for you when it's given to you, simply use calloc instead of malloc.

If you want to clean it yourself, then BurningSheep's suggestion is the one I'd go for.

... unless it's a severe problem and you have lots of time and patience and you want to start playing with DMA (which would actually be a slower way of clearing the memory, but could run contiguously with other processing).

Cheers,

Refractor

PostPosted: Jul 26, 2002 @ 3:32pm
by fzammetti

clearing a block of memory

PostPosted: Jul 30, 2002 @ 12:22pm
by Conan

PostPosted: Jul 30, 2002 @ 12:54pm
by refractor

PostPosted: Jul 30, 2002 @ 3:24pm
by Dan East

PostPosted: Jul 30, 2002 @ 4:37pm
by Conan

PostPosted: Aug 2, 2002 @ 4:26am
by britcode