Thread: Text insertion not working as expected

  1. #1
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,735

    Text insertion not working as expected

    I narrowed down some string problems I was having to these 2 functions, the text at b4 to B->count is supposed to be moved across to the new end of the string but it's not doing as intended, I can see no reason for this either:
    Code:
    SHARED_EXP dint cramTextc( void *ud, STRING *B, uint b4, uint not0 )
    {
    	uint i = (b4 < B->count) ? b4 : B->count - !!(B->count);
    	dint err = growTextc( ud, B, not0 );
    	if ( err )
    	{
    		SHARED_ECHO_ERRNO( stdout, err );
    		return err;
    	}
    	if ( not0 )
    	{
    		uint c = B->count;
    		uint j = c - not0;
    		uchar *txt = B->array;
    		uchar *pos = txt + (B->Vsize * i);
    		uchar *mov = txt + (B->Vsize - j);
    		uchar *end = txt + (B->Vsize * c);
    
    		while ( mov > pos )
    			*(--end) = *(--mov);
    
    		while ( end > pos )
    			*(--end) = 032; /* Substitute character */
    	}
    	return 0;
    }
    SHARED_EXP dint	cramTextn( void *ud, STRING *B, uint b4, ptr str, uint not0 )
    {
    	uint i = (b4 < B->count) ? b4 : B->count - !!(B->count);
    	dint err = growTextc( ud, B, not0 );
    	if ( err )
    	{
    		SHARED_ECHO_ERRNO( stdout, err );
    		return err;
    	}
    	if ( not0 )
    	{
    		uint c = B->count;
    		uint j = c - not0;
    		uchar *txt = B->array;
    		uchar *pos = txt + (B->Vsize * i);
    		uchar *mov = txt + (B->Vsize - j);
    		uchar *end = txt + (B->Vsize * c);
    
    		while ( mov > pos )
    			*(--end) = *(--mov);
    		memcpy( pos, str, B->Vsize * not0 );
    	}
    	return 0;
    }
    These are core functions that other functions call for growth, insertion & overwriting (count is reset to 0 prior to the call for the last one) instead of re-inventing the wheel for every type of character (char, wchar_t, char8_t, char16_t, char32_t, TCHAR) so I need them to work, anyone have any ideas why it's not doing as intended?

  2. #2
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,735
    Welp, dunno what I did wrong but fixed it with a call to memmove instead

    Code:
    SHARED_EXP dint cramTextc( void *ud, STRING *B, uint b4, uint not0 )
    {
    	uint p = B->count;
    	uint i = (b4 < p) ? b4 : p - !!p;
    	dint err = growTextc( ud, B, not0 );
    	if ( err )
    	{
    		SHARED_ECHO_ERRNO( stdout, err );
    		return err;
    	}
    	if ( not0 )
    	{
    		uint k = i + not0;
    		uint n = p - i;
    		dint f = 0;
    		uchar *txt = B->array;
    		uchar *dst = txt + (B->Vsize * i);
    		uchar *end = txt + (B->Vsize * k);
    
    		for ( p = 0; p < sizeof(dint); ++p )
    			f = (f << CHAR_BIT) | 032;
    
    		memmove( end, dst, B->Vsize * n );
    		memset( dst, f, B->Vsize * not0 );
    	}
    	return 0;
    }
    SHARED_EXP dint	cramTextn( void *ud, STRING *B, uint b4, ptr str, uint not0 )
    {
    	uint p = B->count;
    	uint i = (b4 < p) ? b4 : p - !!p;
    	dint err = growTextc( ud, B, not0 );
    	if ( err )
    	{
    		SHARED_ECHO_ERRNO( stdout, err );
    		return err;
    	}
    	if ( not0 )
    	{
    		uint k = i + not0;
    		uint n = p - i;
    		uchar *txt = B->array;
    		uchar *dst = txt + (B->Vsize * i);
    		uchar *end = txt + (B->Vsize * k);
    
    
    		memmove( end, dst, B->Vsize * n );
    		memcpy( dst, str, B->Vsize * not0 );
    	}
    	return 0;
    }

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    > Welp, dunno what I did wrong but fixed it with a call to memmove instead
    Because memmove has guaranteed behaviour if the two areas overlap.

    memcpy does not, so can screw up on overlapped copies.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,735
    Yeah but I had performed a manual copy prior to that, if you try looking at the previous snippets then you'll find memcpy was never the problem, NEVER

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    Well I guess your luck ran out then.

    If there is any chance your memory areas overlap, then memcpy is the WRONG tool for the job, regardless of how many days/months/years you claim "it wasn't a problem before".
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,735
    Quote Originally Posted by Salem View Post
    Well I guess your luck ran out then.

    If there is any chance your memory areas overlap, then memcpy is the WRONG tool for the job, regardless of how many days/months/years you claim "it wasn't a problem before".
    As I said before, I move the target bytes out of the way 1st before copying to them, that's the whole point of the loop that was prior to the memcpy call, after that loop the bytes that would've been lost should've been where the null terminator would've been copied and onwards, in other words when inserting X in STR at point 0 STR would first become STR before the characters where  is would be overwritten with X

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-06-2015, 07:03 AM
  2. Insertion sort in ascending order isnt working
    By mizzou222 in forum C Programming
    Replies: 5
    Last Post: 06-20-2014, 10:46 AM
  3. Replies: 9
    Last Post: 04-14-2012, 01:59 PM
  4. && not working as expected
    By TonyBalony in forum C Programming
    Replies: 4
    Last Post: 12-14-2011, 12:30 PM
  5. cin.get(); doesn't seem to be working as expected
    By Diablo84 in forum C++ Programming
    Replies: 5
    Last Post: 03-30-2005, 07:00 PM

Tags for this Thread