How to rectify the segmentation fault in LINUX system in C language?

OK, first off; you have posted in the incorrect section of the site. This question belongs in the C section, NOT the web development section.... But I'm sure one of the mods will rectify that shortly!

Secondly, can you post more details on the program that is experiencing the segfault? For starters, is this a program you have written yourself? Or a program you have installed from your chosen distros repositories? Or something you have downloaded from elsewhere? Which Linux distro are you running? Do you have the source code for the program?

With the very vague information you have given, the only advice I can give you is equally vague:
1. Get the source code for the program if you do not already have it.
2. Build a debug version of the program by including the -g flag (you might need to alter some makefiles to do this).
3. Run the program through gdb (GNU debugger) until it crashes.
4. Examine the code around the point of the crash and try to determine what is causing the crash and why.
5. Alter the source code to fix the problem.
6. Rebuild the program - again generating debugging symbols with -g.
7. Run the program to see if the fix worked. If the program still crashes, keep running through steps 4-7 until the problem is fixed.
8. Once the problem is fixed, build a release version of the program (remove the -g flag) and re-install the program (if required)

What JasonHippy said. To elaborate:

Segfaults are caused by accessing invalid memory. The most common causes are:

  1. Trying to read/write to a null pointer.
  2. Over-writing the memory allocated to a variable.
  3. Trying to access the member of an array beyond the end of the array, or (less common) before the beginning of the array.

Items #2 and #3 are somewhat related. Example:

for (size_t i = 0, j = size-of-array; j <= j; i++)
{
    array[i] = some-value;
}
/* Oops - just crashed! */

Why did we crash? Because we wrote to the array one element past the end of the array, where i == j. The proper way would be:

for (size_t i = 0, j = size-of-array; j < j; i++)
{
    array[i] = some-value;
}
/* Oops - just crashed! */

What a difference a singe character in your source code makes!

He meant i < j in the above post

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.