memset alternatives

Hello,

I find my self needing to fill an array of 57600 byte's with a recurring sequence of 3 numbers.

so
0 ,0 ,0 , 0 ,0 ,0 <- empty array
1, 2, 3 , 1, 2, 3 <- sequence of numbers

if i use memset i can only give 1 number as parameter. and if i manually do it with a loop it takes to long(I end up looping 19200 times). so i was wondering, is there a better way to accomplish this?
How about a smear move?
1
2
3
4
5
6
7
 
  char src[3] = { 1, 2, 3 };
  char dest[57600];

  memcpy (dest, src, 3);  // Initial copy
  memcpy (&src[3], src, 57600-3); 
  memcpy (&dest[3], dest, 57600-3);


edit: corrected line 6.

A smear move is platform dependent. It relies on a hardware move instruction that moves byte by byte.
Last edited on
This is crude, but perhaps ...

1. memset the first three bytes
2. memcpy 3 bytes to next 3 bytes
3. memcpy 6 bytes to next 6 bytes
4. memcpy 12 bytes to next 12 bytes
...
N-1. memcpy X bytes to next X bytes
N. memcpy Y bytes to last Y bytes
Why is that crude, keskiverto?

It makes sense to me, reading it I saw in my head how two people would fold a bed sheet.
AbstractionAnon: um that does not work. just wondering is line 6 correct ? i want to fill dest right.
Last edited on
Line 6 was indeed incorrect. Should have been dest, not src.

Whether a smear move will work is platform dependent since it depends on the presence of an underlying hardware move instruction that moves byte by byte.

When I tried this on VS2010, it did not work. VS2010 moved bytes 0-2 to bytes 3-5, but the remaining bytes of the array were garbage.
Last edited on
Topic archived. No new replies allowed.