Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

strcpy() when buffers overlap

Posted on 2003-03-28
6
Medium Priority
?
2,301 Views
Last Modified: 2010-05-18
I iknow it is eligal to strcpy() when the source and destination strings overlap, like

char S[10] = "ABC";
strcpy(S,S+1);

However, I need a copy of the section of the C standard that describes this.   Can someone post it along with the section number.
0
Comment
Question by:nietod
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 

Expert Comment

by:Veddartha
ID: 8228847
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.
0
 

Expert Comment

by:Veddartha
ID: 8228862
Sorry, forgot the link:
http://www.lysator.liu.se/c/rat/d11.html

Section is 4.11

Please note that the C standard is not freely available on the web, but the rational document is.
0
 
LVL 15

Expert Comment

by:efn
ID: 8228926
Section 7.21.2.3 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.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 22

Author Comment

by:nietod
ID: 8230089
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.)
0
 
LVL 28

Accepted Solution

by:
dpearson earned 400 total points
ID: 8231073
7.21.2.3  The strcpy function

       Synopsis

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

       Description

[#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.

Returns

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

0
 
LVL 22

Author Comment

by:nietod
ID: 8231545
thanks!

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

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
Suggested Courses

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question