I'm trying to implement Radix sort(http://en.wikipedia.org/wiki/Radix_sort). I'm getting segmentation fault at line 74 in the code although i'm not trying to dereference any null pointer.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166`` ``````#include #include #include using namespace std; int *p; //pointer to array of elements int n; //length of array int cnt=1;//to track the maximum length of entered numbers class node { public: int data; node *link; }*a[10]; //array of pointers to node void initialize() //initializes each node to NULL { for(int i=0;i<10;i++) { a[i]=new node; a[i]=NULL; } } void calculate(int i) //calculates the maximum length among the entered numbers { int cnt2=1; i=i/10; while(i!=0) { i=i/10; cnt2++; if(cntdata=n; if(st==NULL) { temp->link=NULL; st=temp; } else { while(st->link!=NULL) { st=st->link; } st->link=temp; temp->link=NULL; } } void array_insert() //relocates the array with the new numbers according to //the elements in the linked list { int i=0,j=0; beg: node *temp=new node; temp=a[i]; while(jlink==NULL) { p[j]=temp->data; j++; i++; goto beg; } else { p[j]=temp->data; j++; temp=temp->link; } } } void radix_sort() //sort the array according to radix sort method { initialize(); int i=0,div,mod; for(i=0;i>n; p=new int[n]; cout<<"Enter the elements"<>p[i]; calculate(p[i]); } radix_sort(); for(i=0;i

 ``12`` `````` a[i]=new node; a[i]=NULL;``````

¿what do you think you are doing?

 ``12345678`` ``````void insert(int n,node *st){ //... if(st==NULL) { temp->link=NULL; st=temp; } //... ``````
`st' is an input parameter. Its modification would not be seeing outside the function.

Get rid of the globals and the goto.
thank you ne555 the node *st was causing the problem as it's modification was invisible outside function.I modified it as follow
 ``12345678910111213141516171819202122`` ``````void insert(int n,int i) { node *temp=new node; temp->data=n; if(a[i]==NULL) { temp->link=NULL; a[i]=temp; } else { node *temp2=new node; temp2=a[i]; while(temp2->link!=NULL) { temp2=temp2->link; } temp2->link=temp; temp->link=NULL; } }``````
Topic archived. No new replies allowed.