Segmentation fault in strcpy

Posted by Alien01 on Stack Overflow See other posts from Stack Overflow or by Alien01
Published on 2009-04-05T07:02:16Z Indexed on 2010/05/06 2:58 UTC
Read the original article Hit count: 496

Filed under:
|
|

consider the program below

    char str[5];
    strcpy(str,"Hello12345678");
    printf("%s",str);

When run this program gives segmentation fault.

But when strcpy is replaced with following, program runs fine.

strcpy(str,"Hello1234567");

So question is it should crash when trying to copy to str any other string of more than 5 chars length.

So why it is not crashing for "Hello1234567" and only crashing for "Hello12345678" ie of string with length 13 or more than 13.

This program was run on 32 bit machine .

© Stack Overflow or respective owner

Related posts about c++

Related posts about c