webnoon
webnoon

Reputation: 1015

i can't understand why 'segmentation fault error occurred in c

I wrote some C program like below.

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

 main() {
   char *s1 = "hello world!" ;
   char *sto = "it's original string" ;
   //copy
   strcpy( sto, s1 ) ;  

   printf( "%s", sto ) ;

 }

Yes, there are so many articles handling this subject. i read every article. so i found out that no initialized variable cause the error.

but, I thought this code has no errors because sto variable was already initialized as the value of "it's~~bla bla".

i'm a newby about c, please be kind to me. thank you.

Upvotes: 2

Views: 266

Answers (8)

pratik
pratik

Reputation: 119

As mentioned by everyone else you are trying to make changes to constant strings which are not editable... You can however make s1 and sto to point to some other address....

To make your code working do some changes..

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

main() {
    char *s1 = strdup("hello world!");
    char *sto = strdup("it's original string") ;
    //copy
    strcpy( sto, s1 ) ;  
    printf( "%s", sto ) ;

}

The function strdup is a combination of ( malloc + strcpy ).. what it does is it takes the input string as parameter.. allocates enough size for the string in heap memory, copies the strings contents to the memory and returns the address of memory..

So now you have two dynamically allocated strings which you are free to play with.. :) :)

Hope it helped..

Upvotes: 0

JeremyP
JeremyP

Reputation: 86691

Actually, this problem has nothing to do with C per se. You are attempting to overwrite a string literal but the C99 standard says the result of doing this is "undefined" (6.4.5 point 6). This means the C implementation is free to choose to do what it likes. Most implementations just attempt to do the write anyway and a combination of other factors causes the attempt to fail.

The reason your program seg faults is because your compiler has chosen to put the string literal in the executable's text segment i.e. the part of the executable file where all the code is. When the operating system loaded the program into memory, it marked the text segment as read only. Then when strcpy() attempted to write to the string, the operating system/processor's memory protection caused the seg fault.

You can alter this behaviour in some compilers. For instance, gcc has the switch -fwritable-strings which will make the string literals load into the writable data segment. I don't recommend it.

Upvotes: 3

aivikiss
aivikiss

Reputation: 101

The "This is a string" is a string content which stored in read-only memory. So you can not overwrite it any more!

Upvotes: 0

check123
check123

Reputation: 2009

It happens because memory allocated to sto is larger than that allocated to s1. You will have to declare a new char pointer to create a copy of sto.

Upvotes: 0

Lindydancer
Lindydancer

Reputation: 26164

In C, you have to manage the memory are where strings are stored.

A string literal, such as "This is a string" is stored in read-only memory.

You can't change the content of it.

However, you can write something like:

main()
{
  char *s1 = "hello world!" ;

  // This will allocate 100 bytes on the stack. You can use it up until the function returns.
  char sto[100] = "it's original string" ;
  //copy
  strcpy( sto, s1 ) ;  

  printf( "%s", sto ) ;
}

Upvotes: 5

flolo
flolo

Reputation: 15526

The pointers point to init-strings. But those lie in the read-only data segment, so they are not writeable.

Upvotes: 0

pmg
pmg

Reputation: 108986

sto points to a string literal. String literals are non-modifiable char arrays. strcpy tries to modify the non-modifiable elements: BANG!

Upvotes: 0

Nathan Fellman
Nathan Fellman

Reputation: 127638

both s1 and sto are pointers to constant strings.

You're trying to overwrite the string pointed to by sto with a different string, but this is a constant, so you get a segfault for trying to write a readonly area.

Upvotes: 5

Related Questions