when i am adding while statment
while(temp->next!=NULL && temp->next->data < curr->data)
and
while(temp->next->data < curr->data && temp->next!=NULL)
the second one is giving segmentation fault whereas first one code is running fine why is that
when i am adding while statment
while(temp->next!=NULL && temp->next->data < curr->data)
and
while(temp->next->data < curr->data && temp->next!=NULL)
the second one is giving segmentation fault whereas first one code is running fine why is that
In the first case you don’t get segmentation fault because you never check for an invalid memory location , where as in the second case you do make a check and thus get a SIGSEGV.
A similar code to this is as follows
#include <bits/stdc++.h>
using namespace std;
int main() {
int i = -1;
vector<int>v;
while(v[i]<5&&i!=-1) cout<<"A";
}
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.