Question: After using strcpy source is getting corrupted and getting correct destination. Following is my code please suggest me why my source is getting corrupted? If i keep a fixed size to second character array q[] then my source is not being changed. Why is this strange behaviour. -  

I am using MSVC 2005

```
void function(char* str1,char* str2);
void main()
{

    char p[]="Hello world";
    char q[]="";
    function(p,q);
    cout<<"after function calling..."<<endl;
    cout<<"string1:"<<"\t"<<p<<endl;
    cout<<"string2:"<<"\t"<<q<<endl;
    cin.get();
}

void function(char* str1, char* str2)
{
    strcpy(str2,str1);
}

```

OUTPUT: 

```
after function calling...
string1:        ld
string2:        Hello world

```

Thanks in advance,  

Malathi

Answer: 
`q` has only space for 1 character which is the terminating `\0`.
Please read a book about C - you need to learn something about memory management.

Most likely your memory looks like this (simplified): `Qpppppppppppp`. So when you strcpy to `q`, you will overwrite parts of `p`'s memory.

Since you are using C++: Simply use `std::string` and or `std::stringstream` instead of raw char arrays.
What everyone is saying is half correct. The code is failing because space is not reserved for the copy as others have pointed out correctly. The part that's missing is that your objects are on the stack, not the heap. Therefore it is not only likely, but inevitable that your code will get corrupted as the stack can no longer be unwound.