[Webinar] Streamline your web hosting managementRegister Today

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2520
  • Last Modified:

strcpy() when buffers overlap

I iknow it is eligal to strcpy() when the source and destination strings overlap, like

char S[10] = "ABC";

However, I need a copy of the section of the C standard that describes this.   Can someone post it along with the section number.
1 Solution
From the ANSI C Rational ()
*** Begin Quote ***
4.11.2  Copying functions
A block copy routine should be ``right'': it should work correctly even if the blocks being copied overlap.  Otherwise it is more difficult to correctly code such overlapping copy operations, and portability suffers because the optimal C-coded algorithm on one machine may be horribly slow on another.  
A block copy routine should be ``fast'': it should be implementable as a few inline instructions which take maximum advantage of any block copy provisions of the hardware.  Checking for overlapping copies produces too much code for convenient inlining in many implementations.  The programmer knows in a great many cases that the two blocks cannot possibly overlap, so the space and time overhead are for naught.  

These arguments are contradictory but each is compelling.  Therefore the Standard mandates two block copy functions: memmove is required to work correctly even if the source and destination overlap, while memcpy can presume nonoverlapping operands and be optimized accordingly.  
*** End Quote ***
strcpy() is based on the memcpy side of the house, and thus will not handle overlapping destination and source.
Sorry, forgot the link:

Section is 4.11

Please note that the C standard is not freely available on the web, but the rational document is.
Section of the 1999 C standard specifies strcpy.

The second sentence of paragraph 2, under "Description," says "If copying takes place between objects that overlap, the behavior is undefined."

The standard is ISO/IEC 9899:1999.
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

nietodAuthor Commented:
Thanks efn, that's what I anm looking for.  could you give me that whole paragraph, and/or the whole section on strcpy() if possible.  (I assume you can copy and paste it and don't have to retype it.)
dpearsonCommented:  The strcpy function


#include <string.h>
char *strcpy(char * restrict s1,
const char * restrict s2);


[#2]  The strcpy function copies the string pointed to by s2
(including the terminating null character)  into  the  array
pointed  to  by  s1.  If copying takes place between objects
that overlap, the behavior is undefined.


[#3] The strcpy function returns the value of s1.

nietodAuthor Commented:

efn, you were a bit too slow, but I've posted a question with some points for you.

Featured Post

Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now