0

I am surprised, after writing and running following C++ code below on Red Hat Linux.

#include <stdio.h>
#include <string.h>
#include <iostream>

using namespace std;

int main()
{
 char natureofGoods[17];
 char *name="sadasdasdasdas171212";

 strcpy(natureofGoods,name);

 cout<<natureofGoods<<endl;
}

I would wait here as output "sadasdasdasdas17" because natureofGoods has 17 characters size. But I took as output whole string. I mean "sadasdasdasdas171212asdadsfsf" If I run this code on Visual Studio, then my program crashes with a Debug Message as I am waiting. Why does not strcpy cut from 17. Character of name and afterwards copy into natureofGoods?

How can natureofGoods storage more caracter than its size?

Adriano Repetti
  • 65,416
  • 20
  • 137
  • 208
beterman
  • 101
  • 1
  • 10
  • 1
    Take a look at strncpy. – SBI Dec 09 '13 at 13:30
  • 1
    Is there any good reason why you are not using a `std::string`? – 111111 Dec 09 '13 at 13:32
  • 1
    @SBI - and make sure you understand it before you use it. Every use of `strncpy` that I've seen on SO has been wrong, replacing undefined behavior with data corruption. Even if you use it right, it doesn't gain much: with `strcpy` you have to check that the string you're copying is short enough to fit; with `strncpy` you have to check that the copy succeeded. – Pete Becker Dec 09 '13 at 13:32
  • I managed to miss the ++ in the tag... I should be taking a nap. – SBI Dec 09 '13 at 13:47

3 Answers3

10

strcpy attempts to copy characters from the source until it reaches a null-terminator - '\0'. You're breaking this contract because natureofGoods isn't big enough and so run into undefined behavior.

Use std::string!!!!!!!!!!

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
  • 1
    Great answer (as always from @Luchian Grigore and an axiomatic +1), but do consider the overhead of using `std::string`. – Bathsheba Dec 09 '13 at 13:34
  • sorry but if I let this Code running on Red Hat Enterprise Linux, then I recieve as Output whole source (sadasdasdasdas171212asdadsfsf). How can it happen? – beterman Dec 09 '13 at 13:36
  • 1
    @beterman undefined behavior means **anything can happen**. You shouldn't do it. – Luchian Grigore Dec 09 '13 at 13:36
  • I dont want to write a new Code. This Code belongs anyone and I am trying to unterstand, how it works – beterman Dec 09 '13 at 13:37
  • @beterman it doesn't work. It's wrong code which exhibits undefined behavior. – Luchian Grigore Dec 09 '13 at 13:38
  • @beterman : The code you have is incorrect code that has Undefined Behavior. If you need to copy into a char array, then use `strncpy(dst, src, len)`, but beware the fact that `strncpy` doesn't guarantee to put a NUL terminator on the C-style string. You have to change _something_. If you _do_ use `strncpy`, you need to be very careful to get it correct. More people get it wrong than right when they first start using it. – Joe Z Dec 09 '13 at 13:38
1

strcpy does not finish until it's ran out of data in the source string (i.e. it hits a null terminator). It's therefore possible to emit undefined behavour if your pre-allocated destination string is not large enough.

consider use strncpy instead which, in this respect, is safer.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
0

http://www.cplusplus.com/reference/cstring/strcpy/

Copies the C string pointed by source into the array pointed by destination, including the terminating null character (and stopping at that point).

To avoid overflows, the size of the array pointed by destination shall be long enough to contain the same C string as source (including the terminating null character), and should not overlap in memory with source.