2

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

crazyscot
  • 11,819
  • 2
  • 39
  • 40
malathi
  • 21
  • 1
  • 2

7 Answers7

8

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.

Benoit Thiery
  • 6,325
  • 4
  • 22
  • 28
6

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.

ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
4

In your code, q, is an one-element array (basing on the length of "", which is equal to one due to the null terminator), so it cannot contain the whole string. Hence you can't do a strcpy because it writes over invalid memory location (tries to write too much data to an array).

Declare q to be big enough to contain your string. Also, you can use strncpy to be on the safe side.

Kos
  • 70,399
  • 25
  • 169
  • 233
  • Sadly `strncpy` isn't a "safe" `strcpy`, instead it's intended for fixed-size strings, so if the string is long as the buffer it doesn't NUL-terminate it. The nonstandard safe alternative is `strlcpy`. – Matteo Italia Mar 17 '11 at 14:09
3

char q[] = ""; creates a character array with exactly 1 element - copying more data into it won't reserve more memory for it.

So, what happens is that when you write past the space reserved for q, you start overwriting what's in p - the two variables are next to each other in memory.

Erik
  • 88,732
  • 13
  • 198
  • 189
3

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.

Tavison
  • 1,523
  • 1
  • 11
  • 19
2

The array "q" is just one byte long; it definitely doesn't have room for the string "Hello, World"! When you try to copy "Hello, World" to q, you end up exceeding the bounds of q and overwriting p, which is adjacent to it on the stack. I imagine drawing a diagram of how these things are laid out on the stack, you could determine exactly why the garbage that ends up in p is just "ld".

Ernest Friedman-Hill
  • 80,601
  • 10
  • 150
  • 186
2

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.

flurry
  • 21
  • 2