c++ - would you propose this method for copying strings? -


to achieve higher performance, propose using method below when copying strings specially when there lot of characters in string, lot more 12?

 unsigned char one[12]={1,2,3,4,5,6,7,8,9,10,11,12};  unsigned char two[12];  unsigned int (& three)[3]=reinterpret_cast<unsigned int (&)[3]>(one);  unsigned int (& four)[3]=reinterpret_cast<unsigned int (&)[3]>(two);  (unsigned int i=0;i<3;i++)   four[i]=three[i]; 

no, (almost) never. use std::strcpy (although not in case, since “strings” aren’t 0 terminated), or std::copy, or std::string copy constructor.

these methods optimized job you. if code (or similar) happens faster naive character character copying, rest assured strcpy use underneath. in fact, is happens (depending on architecture).

don’t try outsmart modern compilers , frameworks, unless you’re domain expert (and not then).


Comments

Popular posts from this blog

android - Spacing between the stars of a rating bar? -

aspxgridview - Devexpress grid - header filter does not work if column is initially hidden -

c# - How to execute a particular part of code asynchronously in a class -