-2

Why does this program give segmentation fault

int main()
{
    char *ptr;
    ptr = (char *)malloc(15*sizeof(char));
    ptr = "string";
    strcpy(ptr,"NewString");
}

while this does not

int main()
{
    char *ptr;
    ptr = (char *)malloc(15*sizeof(char));
    strcpy(ptr,"String");
    ptr = "Newstring";
}

OR the similar program when one literal in the string is to be modified

int main()
{
    char *ptr;
    ptr = "string";
    ptr[1] = 's';
}

when this does not

int main()
{
    char *ptr;
    ptr = (char *)malloc(15*sizeof(char));
    strcpy(ptr,"String");
    ptr[1] = 's';
}
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
Kranthi
  • 1
  • 1
  • In the 2nd example, the optimizer is free to eliminate the strcpy, since the next line replaces the value. – stark Apr 05 '17 at 13:54
  • [Please see this discussion on why not to cast the return value of `malloc()` and family in `C`.](http://stackoverflow.com/q/605845/2173917). – Sourav Ghosh Apr 05 '17 at 13:54
  • @stark : the optimizer is free to eliminate the strcpy, can you please provide any link for further study and understanding – Kranthi Apr 05 '17 at 14:03

1 Answers1

1

All of above scenarios cause undefined behavior.

For ease of explanation, let's call the four snippets as (I), (II), (III) and (IV), in order of appearance.

  • So, in (I), strcpy(ptr,"String"); is an attempt to write to non-writable memory (attempt to modification of string literal, put another way).

  • In (II), strcpy(ptr,"String"); causes memory overrun as the destination has lesser space than the source.

  • In (III), ptr[1] = 's'; is an attempt to modify (part of) string literal.

  • In (IV), same as (II).

You cannot reasonify the outcome of a program causing UB. Segmentation fault is one of the many possible side effects of UB, not the only one.

That said, please see this discussion on why not to cast the return value of malloc() and family in C..

Community
  • 1
  • 1
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • @ThingyWotsit maybe someone lost their key, or ..... – Sourav Ghosh Apr 05 '17 at 13:49
  • 2
    No, because the original version (containing only the first and last sentence) - while correct - really wasn't useful in any way. Now some content was added, but I ask myself, where the reason is to just spit out a sensess answer as fast as possible and then refining it step by step. – Ctx Apr 05 '17 at 13:51
  • 1
    It looks like a homework dump anyway:( – ThingyWotsit Apr 05 '17 at 13:52
  • @Ctx Well, it was an answer, useful or not, up to you. The outcome of UB is explained in the link...so it was self sufficient. A bit of polish never hurts. :) – Sourav Ghosh Apr 05 '17 at 13:53
  • 1
    @SouravGhosh He asked, _why_ the program gives a segmentation fault. The response "it's UB" just explains, why this behaviour is standard compliant, but it is not the reason for the segv itself. You now start to provide a real answer, so I'll retract the DV. But I personally do not like useless, early answers, just to be the first to go... – Ctx Apr 05 '17 at 13:58
  • @Sourav attempt to write to non-writable memory, can you please explain it more. Where is this 'ptr' stored in memory ? Data section or Code section. – Kranthi Apr 05 '17 at 14:00
  • @Kranthi depends on the compiler, should be in read-only memory, for most of them. C only mandates that attempt to alter a string literal will cause UB. – Sourav Ghosh Apr 05 '17 at 14:01
  • Also what is the difference between assigning string literal to ptr and strcpy. What exactly happens in the compiler – Kranthi Apr 05 '17 at 14:02