I'm kind of in a situation where I need to do some C-String concatenation, so I decided to take it upon my self to learn ( this is just a personal project so I have all the time in the world, really ).
So far I've come up with these two functions (explanation given below):
static inline // make inline to leave out the stack manipulation
size_t StrLength( const char* string )
{
return strnlen( string, strlen( string ) );
}
static void concatstr( char** dest, const char** src )
{
const size_t destlen = StrLength( *dest );
const size_t srclen = StrLength( *src );
const size_t total_len = destlen + srclen;
const size_t totalLenNull = total_len + 1;
char* tmp = ( char* )malloc( sizeof( char ) * totalLenNull ); //<-- Because of this...
size_t counter = 0;
for( size_t iDest = 0; iDest < destlen; ++iDest )
tmp[ counter++ ] = *dest[ iDest ];
for ( size_t iSrc = 0; iSrc < srclen; ++iSrc )
tmp[ counter++ ] = *src[ iSrc ];
*dest = ( char* ) realloc( *dest, totalLenNull );
strncpy( *dest, tmp, total_len );
free( tmp );
tmp = NULL;
}
The idea behind the inline StrLength function is that, if I understand inline correctly, it's supposed to work like a safer macro in C++, so it's less bug prone (I think). I made it inline since it's just one line of code, and a stack manipulation for that kind of a process seems a bit much. If I'm wrong, however, please correct me on this.
Now, onto the concatstr()
function:
The problem here is that as soon as I get to the second loop, the program crashes upon the 2nd iteration due to a memory read violation. Why this is happening I'm not sure.
The calling code looks as follows:
const size_t len = StrLength( msg ) + mPrefix.length() + StrLength( "\n\n" );
char* out = ( char* )malloc( sizeof( char ) * ( len + 1 ) );
out[0] = '\0';
const char* tmp_pass = mPrefix.c_str();
concatstr( &out, &tmp_pass );
concatstr( &out, &msg );
The only reason I'm putting a visual-C++ tag here is that even though I'm doing this like one would in straight c, I'm using VC++ as a compiler.
Does anyone have an idea of what the issue is here?