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

int main()
{
   char src[]="123456";
   strcpy(src, &src[1]);
   printf("Final copied string : %s\n", src);
}

When I use the Visual Studio 6 Compiler it gives me the expected answer "23456".

How come this program prints "23556" when compiled with gcc 4.7.2?

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
Blood-HaZaRd
  • 2,049
  • 2
  • 20
  • 43

3 Answers3

13

strcpy(src, &src[1]); is undefined behavior:

C11 §7.24.2.3 The strcpy function

The strcpy function copies the string pointed to by s2 (including the terminating null character) into the array pointed to by s1. If copying takes place between objects that overlap, the behavior is undefined.

By the way, memcpy is similar (but not memmove). See C FAQ: What's the difference between memcpy and memmove.

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
  • Hmm... seems to be similar to the `memcpy` vs `memmove` issue. – legends2k Dec 18 '14 at 12:36
  • 1
    Where do you find these information's? And how do you find them so quickly? OR have you written your own book :D? – Rizier123 Dec 18 '14 at 12:36
  • So this means that the VS compiler is srtonguer for the undefined behaviour or does the coder had a undefined reflexion to write such ligne ? – Blood-HaZaRd Dec 18 '14 at 12:39
  • 3
    @Rizier123 I have a copy of the C standard draft nearby, if that's what you are asking :). See [Where do I find the current C or C++ standard documents?](http://stackoverflow.com/q/81656/1009479). – Yu Hao Dec 18 '14 at 12:40
  • 2
    @Rizier123: On my system, it is described in the glibc manpages (`man 3 strcpy`): The strings may not overlap. – Witiko Dec 18 '14 at 12:40
  • 1
    @Blood-HaZaRd: It's undefined behaviour, the implementation can do whatever it pleases. – Witiko Dec 18 '14 at 12:41
  • Even the 1988 K&R (ANSI) C has this in the first paragraph of Appendix B3 regarding string functions. Any proper C book documents this. – JohnH Dec 18 '14 at 17:24
2

This is undefined behaviour. Use the memmove function instead. memmove is designed to allow overlapping of source and destination buffers.

memmove(src, &src[1], strlen(&src[1]) + 1) ;  // + 1 for copying the terminating zero
Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
2

From ISO/IEC 9899:TC3 (c99)

7.21.2.3 The strcpy function

Synopsis

1

#include <string.h>

char *strncpy(char * restrict s1, const char * restrict s2, size_t n);

Description

2 The strcpy function copies the string pointed to by s2 (including the terminating null character) into the array pointed to by s1. If copying takes place between objects that overlap, the behavior is undefined.

So what you are doing is simply undefined behaving ;)

You can also see the ANNEX J.2

Stating cases of undefined behavior with a note how to prevent:

The behavior is undefined in the following circumstances:

[...]

—An attempt is made to copy an object to an overlapping object by use of a library function, other than as explicitly allowed (e.g., memmove) (clause 7).

dhein
  • 6,431
  • 4
  • 42
  • 74