Error :- Segmentation fault(Core Dumped)

Hello everyone i have made a c++ program which is running correctly on windows on my pc but when running on unix environment it giving a “Segmentation fault” it’s completely new error to me in c++. i am unable to understand that how can i solve this. if anyone is known to this error please help me out.

the output on my pc is completely correct. the program is of huffman encoding. i have inputted a string from the user and converted it into the huffman code. Please help me out.

Thanks.

A segmentation fault ( SEGFAULT ) occurs when you are trying to access memory which you should not be trying to access ( the memory which you have not allocated ) . But, you might ask which memory are we not allowed access to ? or when are we trying to access memory which does not belong to us ?

The answer to the first is that you are not allowed to access memory which has not been allocated yet. If you try to access them, then it will lead to a SEGFAULT.

For the next part,
Well. there are many cases, but the common ones are

  • When dereferencing a NULL pointer
  • When dereferencing an uninitialized or dangling pointer
  • When you get a Buffer Overflow
  • when you get a Stack Overflow
  • When you try to change the value of a variable marked as const using pointers.
  • When you try to access free’d memory.

These are the basic and most common causes of a Segfault.

Now lets explain the dereferencing a NULL pointer
If you try something like

int *ptr = NULL;
*ptr = 4;

You are trying to change the value using *ptr, but ptr is a NULL pointer, and this might cause you a SEGFAULT

An uninitialized pointer is a pointer which you have not referenced to anything

char *ptr;
*ptr = 'a';

With this, you will be trying to give the value a to *ptr, but as ptr is not initialized, it is pointing to some memory which you do not have access to.

Now lets move on to the Dangling Pointers

int *ptr;
if ( 1 )
 {
   int a = 5;
   ptr = &a;
 }
*ptr = 6;

The variable a has a scope inside the if block. Once you go out of the if block, a is removed from memory and ptr now points to something which no longer exists. When you try to change the value like in the code, you will get a SEGFAULT.

Next is a Buffer Overflow.

char a[8] = "INCORRECT";

a can only store 8 bytes of data, but we are trying to store 10 bytes ( ‘I’ ‘N’ ‘C’ ‘O’ ‘R’ ‘R’ ‘E’ ‘C’ ‘T’ ‘\0’ ). This can also lead to SEGFAULT.

Next is Stack Overflow ( Please do not mistake StackOverflow.com for this )

The stack is the part of memory which holds the return address at function calls, arguments passed to the function, local variables for the function, etc. But there is a limit to the amount of memory available in the stack. So if you try something like

int myfunc()
  {
     return myfunc();
  }

it is an infinite recursion, and each time the function returns, memory from stack is used to store the return address of the function. Since this is infinite, the memory in stack will run out and you get a Stack Overflow.

Now, changing a constant using pointers.

char *ptr = "Yay";
*ptr = 'Oh no"        // SEGFAULT

ptr points to a const string, but you try to change it, leading to SEGFAULT.

Accessing free’d memory is just like dangling pointers. When you try to access a dereference a pointer which points to something which has already been deleted or Freed, you get a SEGFAULT.

These are the general causes of a SEGFAULT.

You can also read these links for some reference

What is segmentation fault?

Segmentation fault, Wikipedia

Segmentation Fault (Core dumped) in c++

The third link provides an example of Segmentation Fault (Core dumped) . The problem there is caused mainly due to trying to access a part of string which is out of bound.

Segmentation Fault (core dumped) C++ This is also an example.

You can find many such examples of this if you Google it.

But this is as far as I can tell you as you have not shown any code. You are most likely trying to do something like the 3rd example link, or some part of your code does not do what you think and your code ends up doing this. Well, this is all I can help you with as of now.

Hope you find this useful.

2 Likes

Thanks arun_as for explaining so well,

i have made a silly mistake in my program and i think that was the error,
the error was in the second file containing the huffman code function,

//huffman.cpp

int HuffmanCode(){

//my code here

i was not returning any value to the function back
}

But not returning the value is also the the part of SEGMENTATION FAULT?

i think it might be !! well my code run successfully! thanks for ur answer!

it would help if you post a link to your C++ code.

3 Likes

You’re welcome. And don’t worry, we all make mistakes like that.

//Any one solve my segmentation error input is - a*(b-c+d)/e.

#include
#include
using namespace std;

int precedence(char op){
if(op == ‘+’){
return 1;
} else if(op == ‘-’){
return 1;
} else if(op == ‘*’){
return 2;
} else {
return 2;
}
}
int main()
{
string s;cin>>s;
stackpostfix;
stackprefix;
stackoperators;

for (int i = 0; i < s.length(); i++) {
  char ch = s[i];
  
 
 if (ch == '+' || ch == '-' || ch == '*' || ch == '/') {
    while (operators.size() > 0 && operators.top() != '(' && 
           precedence(ch) <= precedence(operators.top()))
           {
      char op = operators.top();

      string postval2 = postfix.top();
      postfix.pop();
      string postval1 = postfix.top();
      postfix.pop();
      postfix.push(postval1 + postval2 + op);

      string preval2 = prefix.top();
      prefix.pop();
      string preval1 = prefix.top();
      prefix.pop();
      prefix.push(op + preval1 + preval2);
    }

    operators.push(ch);
  }
 if (ch == ')') {
    while (operators.size() > 0 && operators.top() != '(') {
      char op = operators.top();

     string postval2 = postfix.top();
     postfix.pop();
      string postval1 = postfix.top();
      postfix.pop();
      postfix.push(postval1 + postval2 + op);

      string preval2 = prefix.top();
      prefix.pop();
      string preval1 = prefix.top();
      prefix.pop();
      prefix.push(op + preval1 + preval2);
    }
    

  }
}

 while (operators.size() > 0) {
 char op = operators.top();

  string postval2 = postfix.top();
  postfix.pop();
  string postval1 = postfix.top();
  postfix.pop();
  postfix.push(postval1 + postval2 + op);

  string preval2 = prefix.top();
  prefix.pop();
  string preval1 = prefix.top();
  prefix.pop();
  prefix.push(op + preval1 + preval2);
}

cout<<postfix.top()<<endl;
cout<<prefix.top()<<endl;

}