Strdup: Difference between revisions

From Valve Developer Community
Jump to navigation Jump to search
m (strdup)
mNo edit summary
Line 1: Line 1:
Defined as a static <code>CloneString</code> in <code>src\vgui2\controls\BitmapImagePanel.cpp</code> and as a static <code>CopyString</code> in <code>src\dlls\AI_ResponseSystem.cpp</code>, this function simply returns a pointer to a clone of the string parameter.  This function handles the repetitive code that is seen when copying a string and makes it an inline function.  If copied to <code>src\game_shared\util_shared.h</code>, this function could come in handy at times. It is also valid to use <code>strdup</code> to achieve the same results.
<code>'''strdup'''</code> is useful for '''dup'''licating '''str'''ings to store them off to pointers. Remember to delete the duplicated string once you're done with it.
inline char *CloneString( const char *str )
{
char *cloneStr = new char [ strlen(str)+1 ];
strcpy( cloneStr, str );
return cloneStr;
}
[[Category:Helpers]]
[[Category:Helpers]]

Revision as of 19:09, 17 June 2006

strdup is useful for duplicating strings to store them off to pointers. Remember to delete the duplicated string once you're done with it.