开发者

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

开发者 https://www.devze.com 2023-02-22 23:51 出处:网络
I wrote some C program like below. #include <stdio.h> #include <string.h> main() { char *s1 = \"hello world!\" ;

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, 开发者_如何学Go 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.


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 ) ;
}


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.


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.


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


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


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.


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


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..

0

精彩评论

暂无评论...
验证码 换一张
取 消