Click here to Skip to main content
15,887,812 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
QuestionProblems handling DBT_DEVICEQUERYREMOVE [modified] Pin
64BitWho10-Oct-07 12:23
64BitWho10-Oct-07 12:23 
QuestionVRP using genetic algorithms coded in c++ Pin
mihira18410-Oct-07 11:35
mihira18410-Oct-07 11:35 
AnswerRe: VRP using genetic algorithms coded in c++ Pin
Mark Salsbery10-Oct-07 12:24
Mark Salsbery10-Oct-07 12:24 
Questionfilling memory with a pattern Pin
DQNOK10-Oct-07 11:00
professionalDQNOK10-Oct-07 11:00 
QuestionRe: filling memory with a pattern Pin
David Crow11-Oct-07 3:28
David Crow11-Oct-07 3:28 
AnswerRe: filling memory with a pattern Pin
DQNOK11-Oct-07 3:44
professionalDQNOK11-Oct-07 3:44 
GeneralRe: filling memory with a pattern Pin
David Crow11-Oct-07 3:52
David Crow11-Oct-07 3:52 
GeneralRe: filling memory with a pattern Pin
DQNOK11-Oct-07 7:06
professionalDQNOK11-Oct-07 7:06 
OK. Thanks for your thoughts and replies.

My algorithm is not application specific (that I can see). I'll go ahead and post it here in case anyone else can benefit from it. It looks long, but it's mostly comments. The test case at the end works fine for me when I compile and run it with Digital Mars C++.

//author: David Qualls
//date:   Sept 2007
#include <string.h>

void memoryfill( void* target, char* pattern, size_t ptrnlen, size_t numRepeats )
{  
   char* trgt = (char*)target;  //for ease in pointer manipulation.
   if( 0 == numRepeats ) 
   {
      ; // do nothing!
   }
   else if( 1 == ptrnlen ) 
   {
      memset(target, *pattern, numRepeats);   
   }
   else    
   {
   // The naive approach here would be:
   // while( numRepeats--  > 0 )
   // {  memcpy(target, pattern, ptrnlen);
   //    (char*)target += ptrnlen;
   // }
   // But this is O(numRepeats).  Very ineffecient for large numRepeats.
   // Use a binary algorithm instead.  This one uses between log(numRepeats)
   // and 2*log(numRepeats) calls to memcpy(); a maximum of 64 calls to 
   // memcpy()for 32-bit size_t's : versus up to 4-billion calls to memcpy().

   // First, fill target up to the highest "bit" of repeatCount. log(n) loops.
      memcpy(trgt, pattern, ptrnlen); //begin by putting one copy into target.
      int snglbit=1;                  //number of copies in target. Always a power of 2.
      int thisCnt = numRepeats >> 1;  //one bit of repeatCount has already been taken care of.
      while( thisCnt > 0 )            //until all bits have been shifted off.
      {
         memcpy(trgt+ptrnlen*snglbit, trgt, ptrnlen*snglbit); //now double the amount in target.
         snglbit <<= 1;  //doubles the value of snglbit.
         thisCnt >>= 1;  //halves the value of thisCnt.
      }
   // snglbit now matches the most significant bit of repeatCount.
      char* endptr = trgt + ptrnlen*snglbit;   //where the next memcpy target is.
      while( (snglbit >>= 1) > 0 )             //for each lower bit within numRepeats,
      {
         if( snglbit & numRepeats )            //if this bit is ON within numRepeats,
         {
            memcpy(endptr, trgt, ptrnlen*snglbit);
            endptr += ptrnlen*snglbit;
         }
      }
   }
   return;
}
<br>
#include <stdio.h>
<br>
int main()
{
   char buf[1024];
   memset(buf, 0, 1024);  //insure null terminator(s)
   memoryfill( buf, "Hello World_", 12, 19 );
   printf("\n%s", buf);
}

GeneralRe: filling memory with a pattern Pin
David Crow11-Oct-07 7:21
David Crow11-Oct-07 7:21 
GeneralRe: filling memory with a pattern Pin
DQNOK11-Oct-07 7:25
professionalDQNOK11-Oct-07 7:25 
AnswerRe: filling memory with a pattern Pin
Scott Dorman11-Oct-07 8:10
professionalScott Dorman11-Oct-07 8:10 
GeneralRe: filling memory with a pattern Pin
DQNOK11-Oct-07 8:30
professionalDQNOK11-Oct-07 8:30 
GeneralRe: filling memory with a pattern Pin
Scott Dorman11-Oct-07 8:41
professionalScott Dorman11-Oct-07 8:41 
GeneralRe: filling memory with a pattern Pin
DQNOK11-Oct-07 8:51
professionalDQNOK11-Oct-07 8:51 
GeneralRe: filling memory with a pattern Pin
Scott Dorman11-Oct-07 9:15
professionalScott Dorman11-Oct-07 9:15 
GeneralRe: filling memory with a pattern Pin
DQNOK11-Oct-07 9:43
professionalDQNOK11-Oct-07 9:43 
GeneralRe: filling memory with a pattern Pin
Scott Dorman11-Oct-07 9:45
professionalScott Dorman11-Oct-07 9:45 
QuestionChange the size of the scrollbar, getsystemmetrics [modified] Pin
tortexy10-Oct-07 7:36
tortexy10-Oct-07 7:36 
AnswerRe: Change the size of the scrollbar, getsystemmetrics Pin
Mark Salsbery10-Oct-07 7:48
Mark Salsbery10-Oct-07 7:48 
GeneralRe: Change the size of the scrollbar, getsystemmetrics Pin
tortexy10-Oct-07 7:53
tortexy10-Oct-07 7:53 
GeneralRe: Change the size of the scrollbar, getsystemmetrics Pin
Mark Salsbery10-Oct-07 10:15
Mark Salsbery10-Oct-07 10:15 
GeneralRe: Change the size of the scrollbar, getsystemmetrics Pin
tortexy11-Oct-07 2:33
tortexy11-Oct-07 2:33 
QuestionMIDI...DIRECT MUSIC...VISUAL C++...WINDOWS VISTA Pin
Rance_Wilson10-Oct-07 5:41
Rance_Wilson10-Oct-07 5:41 
Questionvc 2005 CDatabase compile error Pin
ns10-Oct-07 5:21
ns10-Oct-07 5:21 
AnswerRe: vc 2005 CDatabase compile error Pin
Mark Salsbery10-Oct-07 5:35
Mark Salsbery10-Oct-07 5:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.