home bbs files messages ]

Forums before death by AOL, social media and spammers... "We can't have nice things"

   comp.lang.c      Meh, in C you gotta define EVERYTHING      243,242 messages   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]

   Message 242,807 of 243,242   
   highcrew to All   
   UB-free slice   
   05 Jan 26 22:36:28   
   
   From: high.crew3868@fastmail.com   
      
   Hello,   
      
   Let `struct Buffer` be defined as:   
      
      struct Buffer { unsigned char *bytes; size_t length; };   
      
   Then let's define the following function:   
      
      int buffer_append(struct Buffer *dst, const struct Buffer *src)   
      {   
        // checks on size etc etc, return non-zero on failure   
      
        memcpy(dst->bytes, src->bytes, length);   
        dst->bytes += length;   
        dst->length -= length;   
        return 0;   
      }   
      
   In my understanding, if we call buffer_append(&x, &y) for x.bytes and   
   y.bytes pointing to overlapping areas of the same array, we get UB by   
   the first two parameters of memcpy being restrict-qualified pointers.   
      
   I suppose the correct thing to do would be to replace memcpy with   
   memmove, allegedly taking some performance penalty.   
      
   Assuming that such penalty matters on the scale of things, what is   
   the correct/recommended way of handing this situation?   
   Would it make sense to define a `struct RBuffer` as the following for   
   the purpose?   
      
      struct RBuffer { unsigned char * restrict bytes; size_t length };   
      
   This is maybe OK with respect to UB, but IMO it would result in a rather   
   ugly API.  We don't always *need* that bytes pointer to be restricted.   
      
   Alternative ideas? I can think of the following, but I don't like   
   it either...   
      
      int buffer_append(struct Buffer *dst, const struct Buffer *src,   
                        bool overlapping)   
      {   
         // if overlapping then memmove else memcpy   
      }   
      
      
   --   
   High Crew   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]


(c) 1994,  bbs@darkrealms.ca