1

Suppose I do like this to copy the string.

char str[] = "";
char *str2 = "abc";
strcpy(str, str2);
printf("%s", str);  // "abc"
printf("%d", strlen(str));  // 3

Then, why it doesn't give me undefined behaviour or causing the program to fail. What are the disadvantages of doing like that ?

Ashish Rawat
  • 3,363
  • 5
  • 29
  • 35
  • 6
    How do you know it's not giving you undefined behavior? – Luchian Grigore Jul 11 '13 at 17:05
  • because it's giving me correct length of the string. – Ashish Rawat Jul 11 '13 at 17:07
  • 1
    [Undefined behavior means *anything* can happen](http://blogs.msdn.com/b/oldnewthing/archive/2011/09/29/10217910.aspx) – phihag Jul 11 '13 at 17:07
  • `What are the disadvantages of doing like that ?` There is one: it doesn't work. – milleniumbug Jul 11 '13 at 17:07
  • If in a function, this error may go after function returns(stack released?)? Or char array constants always in heap? – huseyin tugrul buyukisik Jul 11 '13 at 17:08
  • 2
    This is definitely munging the stack, but apparently not to a point where it is [blatantly] breaking things. Try a larger string and see what it does; I would wager that you start to see strange things occurring. – Will Jul 11 '13 at 17:12
  • @Will: Yes, you're right, the program crash after I tried longer string. (68 char length) – Ashish Rawat Jul 11 '13 at 17:14
  • @ashish2expert: Exactly ... with such a small string, you are overrunning the stack by a couple of characters (the size of a 16-bit integer). The stack may be able to recover from this, depending on the calling convention being used, but a larger overrun like your 68-character string, or the one in my response below, reveals the bug. What really bothers me is that [at least] gcc does not flag this, even with the `-pedantic` flag used at compile time. – Will Jul 11 '13 at 17:21
  • 1
    @ashish2expert Read: [`strcat()` implementation works but causes a core dump at the end](http://stackoverflow.com/questions/16750998/strcat-implementation-works-but-causes-a-core-dump-at-the-end/16751011#16751011) – Grijesh Chauhan Jul 11 '13 at 17:39
  • This is one of the limitations of C, compared to a high level language. No one knows that you are doing something bad resulting in undefined behavior, except maybe the programmer. The compiler doesn't know, and the operating system doesn't know. I suppose it is job security for C programmers, because these bugs happen *all the time* even 40 years after C was invented and refined. – Mark Lakata Jul 11 '13 at 18:04

3 Answers3

4

You are writing past the memory space allocated to str on the stack. You need to make sure you have the correct amount of space for str. In the example you mentioned, you need space for a, b, and c plus a null character to end the string, so this code should work:

char str[4];
char *str2 = "abc";
strcpy(str, str2);
printf("%s", str);  // "abc"
printf("%d", strlen(str));  // 3
zztops
  • 694
  • 1
  • 5
  • 11
3

This code is definitely causing a stack problem, though with such a small string, you are not seeing the issue. Take, for example, the following:

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

int main()
{
        char str[] = "";
        char *str2 = "A really, really, really, really, really, really loooooooooooooooonnnnnnnnnnnnnnnnng string.";
        strcpy(str, str2);
        printf("%s\n", str);
        printf("%d\n", strlen(str));
        return 0;
}

A contrived example, yes, but the result of running this is:

A really, really, really, really, really, really loooooooooooooooonnnnnnnnnnnnnnnnng string.
92
Segmentation fault

This is one of the reasons why the strcpy function is discouraged, and usage of copy and concatenate functions that require specifying the sizes of the strings involved are recommended.

Will
  • 3,500
  • 4
  • 30
  • 38
2

It actually gives you undefined behavior, but your program doesn't have to fail because of that. That's how undefined behavior works.

typ1232
  • 5,535
  • 6
  • 35
  • 51