confusion with strcpy

I have been playing with strcpy http://www.cplusplus.com/reference/cstring/strcpy/ and well, when I do an operation with a source and a destination if that destination have more than the amount of character the source has it works well...but if the destination has the same amount of "space" than the source...once the operation is done the source is deleted, it is like a move more than a copy...It must have a relation with the null character I guess but i can't see why the source is deleted...here I have done an example where you can check that out..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
  
int main(){
char str1 [] = "0123456789";
	std::cout<<"Lenght str1"<<std::endl;
	std::cout<<strlen(str1)<<std::endl;
	std::cout<<"str1 text"<<std::endl;
	std::cout<<str1<<std::endl;
	char str2 [10];
	strcpy(str2,str1);
	std::cout<<"Lenght str2"<<std::endl;
	std::cout<<strlen(str2)<<std::endl;
	std::cout<<"str2 text"<<std::endl;
	std::cout<<str2<<std::endl;
	
	std::cout<<"Lenght str1"<<std::endl;
	std::cout<<strlen(str1)<<std::endl;
	std::cout<<"str1 text"<<std::endl;
	std::cout<<str1<<std::endl;
	
	
	char str3 [] = "0123456789";
	std::cout<<"Lenght str3"<<std::endl;
	std::cout<<strlen(str3)<<std::endl;
	std::cout<<"str3 text"<<std::endl;
	std::cout<<str3<<std::endl;
	char str4 [11];
	strcpy(str4,str3);
	std::cout<<"Lenght str4"<<std::endl;
	std::cout<<strlen(str4)<<std::endl;
	std::cout<<"str4 text"<<std::endl;
	std::cout<<str4<<std::endl;
	
	std::cout<<"Lenght str3"<<std::endl;
	std::cout<<strlen(str3)<<std::endl;
	std::cout<<"str3 text"<<std::endl;
	std::cout<<str3<<std::endl;
	
	
	char str5 [] = "0123456789";
	std::cout<<"Lenght str5"<<std::endl;
	std::cout<<strlen(str5)<<std::endl;
	std::cout<<"str5 text"<<std::endl;
	std::cout<<str5<<std::endl;
	char str6 [4];
	strcpy(str6,str5);
	std::cout<<"Lenght str6"<<std::endl;
	std::cout<<strlen(str6)<<std::endl;
	std::cout<<"str6 text"<<std::endl;
	std::cout<<str6<<std::endl;
	
	std::cout<<"Lenght str5"<<std::endl;
	std::cout<<strlen(str5)<<std::endl;
	std::cout<<"str5 text"<<std::endl;
	std::cout<<str5<<std::endl;

}


It seem s like if the detination is smaller than the source It get as many characters as it can, but it move them , it doesn't copy them and I cant see why....does anyone know why??thanks
From https://msdn.microsoft.com/en-us/library/kk6xf663.aspx

Because strcpy does not check for sufficient space in strDestination before it copies strSource, it is a potential cause of buffer overruns. Therefore, we recommend that you use strcpy_s instead.

And

The behavior of strcpy is undefined if the source and destination strings overlap.

It sounds like you are ending up with the destination string starting location being set to just before the source string location in memory - then the copy operation ends up overwriting the beginning of the source string. If by chance the null character gets copied to the location of the first character of the source, then you'd see nothing when you subsequently try to output the source string.

But the occurence and symptoms of this effect would be seemingly random (for example, running this code in the cpp.sh I didn't get any issue at all).
Last edited on
I recommand that you use string class methods(c++) define in <string> instead of <string.h> unless you are familiar with pointers .
Topic archived. No new replies allowed.