What is Segmentation fault in C? How to Solve it?

This is the most common error for beginners. I would like to give an example to demonstrate about segmentation fault.

To define a string you can use:

char *str = "Constant String";

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <conio.h>
  4. int main()
  5. {
  6. char *str = "Constant String"; //segmentation fault
  7. //char str[] = "Constant String";
  8. *(str+0) = 'K';
  9. printf("%s",str);
  10. getch();
  11. return 0;
  12. }

So now the constant string is assigned to char pointer str.  This is certainly valid.  But if you try to modify the value of a constant string using:


*(str+0) = 'K';

then it will result in a segmentation fault.

As constant string "Constant String" is stored in code segment & user can not modify the value of code segment inside the code. Which is not valid or legal. The Certain compiler will give the error as "Segmentation fault". The Certain compiler will not give you the error but result in a runtime failure.


One more example of segmentation fault is:


Assume that you can function pointer with (*pf)(); Ideally you need to assign it as an address of a function. But now instead of assigning function address if you assign it to a value 10. Then it will also result in a segmentation fault.


Ultimately, if you are trying to access to the region to which you don't have access can result in Segmentation fault.
  1. #include <iostream>
  2. #include<string.h>
  3.  
  4. int main()
  5. {
  6.     char *p;
  7.     strcpy(p, "How r u");
  8. }

Common Reason to Get Segmentation fault:


  • Working with or Trying to access the variable which is out of scope or died.
  • Trying to access the array of bound Index.
  • Writing to an area for which memory is not allocated.
  • Freeing memory more than one time. 
  • Running out of memory in case of recursion.
  • Accessing the return address of the local variable.

Simple example Code with Solution:

Not Working Code:

  1. #include <stdio.h>
  2. #include <conio.h>
  3. #include <string.h>
  4. #include <stdlib.h>

  5. int main()
  6. {
  7. char testString[] = "Kapil Thakar";
  8.         char *outString = NULL; //Segmentation Fault
  9. int i = 0;
  10. for(i=0 ; i<strlen(testString); i++)
  11. {
  12. if ( *(testString + i) != ' ')
  13. {
  14. printf("%c" , *(outString + i) = *(testString + i));
  15. }
  16. }

  17. getch();
  18. return 0;
  19. }



Working Code:

  1. #include <stdio.h>
  2. #include <conio.h>
  3. #include <string.h>
  4. #include <stdlib.h>

  5. int main()
  6. {
  7. char testString[] = "Kapil Thakar";
  8. char *outString = (char *) malloc (sizeof(testString));
  9. int i = 0;
  10. for(i=0 ; i<strlen(testString); i++)
  11. {
  12. if ( *(testString + i) != ' ')
  13. {
  14. printf("%c" , *(outString + i) = *(testString + i));
  15. }
  16. }

  17. getch();
  18. return 0;
  19. }
Share on Google Plus

About Kapil Thakar

"I am Kapil Thakar, an Embedded Engineer cum Blogger wants to learn new things. I love to share my knowledge solutions to the problems. Interested in Blogging, Creative-Writing, SEO, Website Creation, Video Making, Editing, Affiliation Programs, Online Making Money."
    Blogger Comment
    Facebook Comment

0 comments :

Post a Comment

Related Posts Plugin for WordPress, Blogger...