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: 
`strcpy` does not allocate memory required to store the string.
You must allocate enough memory in `str2` before you do the `strcpy`.
Otherwise, you get undefined behaviour as you are overwriting some non-allocated memory.
strcpy expects you to provide an allocated storage buffer, not just a char\* pointer. If you change `char q[]="";` to `char q[50];` it will work. Since you're only giving strcpy a pointer to a zero length string it doesn't have enough space to store the copied string and overwrites aka corrupts the memory.