Segmentation fault

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • codeveloper
    New Member
    • Mar 2007
    • 1

    Segmentation fault

    I have written a function where I have to use merge sort. My program is due Monday. Please, if anyone can help me out. Here is the code, plus I have declared a struct which I am using.
    Code:
    typedef struct restaurant
    {
    	char name[STRING];
    	int code;
    	char food;
    	int entree;
    	float high_cost;
    	float low_cost;
    	float distance;
    	char children;
    	char senior;
    	int age;
    	int open1;
    	int close1;
    	int open2;
    	int close2;
    	char days_open[8];
    	int waitime;
    	float avg_cost;
    	char family;
    } REST;
    
    /*******************************************************************************
    FUNCTION NAME	:	sort_by_food
    INPUTS		:	array of sturcts rest, int first and last
    RETURNS		:	none
    PURPOSE		:	sorts the data by the type of food 
    *******************************************************************************/ 
    
    void sort_by_food( REST rest[], int first, int last )
    {
    	int middle;
    
    	middle = ( first + last ) / 2;
    	if( first < last )
    	{
    		sort_by_food( rest, first, middle );
    		sort_by_food( rest, middle + 1, last );
    		merge_food( rest, first, middle, middle + 1, last );
    	}
    }
    
    /*******************************************************************************
    FUNCTION NAME	:	merge_food
    INPUTS		:	array of sturcts rest, int first1, last1, first2, last2
    RETURNS		:	none
    PURPOSE		:	sorts the data by the type of food 
    *******************************************************************************/ 
    
    void merge_food( REST rest[], int first1, int last1, int first2, int last2 )
    {
    	REST temp[SIZE];
    
    	int i, i1, i2; // index, index 1, index 2
    	int num, fi;	// num, find index 
    
    	i = 0; 
    	i1 = first1;
    	i2 = first2;
    	num = last1 - first1 + last2 - first2 + 2;
    	while( (i1 <= last1) && (i2 <= last2) )
    	{
    		if( rest[i1].food < rest[i2].food)
    		{
    			strcpy(temp[i].name, rest[i1].name);
    			temp[i].code = rest[i1].code;
    			temp[i].food = rest[i1].food;
    			temp[i].entree = rest[i1].entree;
    			temp[i].high_cost = rest[i1].high_cost;
    			temp[i].low_cost = rest[i1].low_cost;
    			temp[i].distance = rest[i1].distance;
    			temp[i].children = rest[i1].children;
    			temp[i].senior = rest[i1].senior;
    			temp[i].age = rest[i1].age;
    			temp[i].open1 = rest[i1].open1;
    			temp[i].close1 = rest[i1].close1;
    			temp[i].open2 = rest[i1].open2;
    			temp[i].close2 = rest[i1].close2;
    			strcpy(temp[i].days_open, rest[i1].days_open);
    			temp[i].waitime = rest[i1].waitime;
    			temp[i].avg_cost = rest[i1].avg_cost;
    			temp[i].family = rest[i1].family;
    			i++;
    			i1++;
    		}
    		else
    		{
    			strcpy(temp[i].name, rest[i2].name);
    			temp[i].code = rest[i2].code;
    			temp[i].food = rest[i2].food;
    			temp[i].entree = rest[i2].entree;
    			temp[i].high_cost = rest[i2].high_cost;
    			temp[i].low_cost = rest[i2].low_cost;
    			temp[i].distance = rest[i2].distance;
    			temp[i].children = rest[i2].children;
    			temp[i].senior = rest[i2].senior;
    			temp[i].age = rest[i2].age;
    			temp[i].open1 = rest[i2].open1;
    			temp[i].close1 = rest[i2].close1;
    			temp[i].open2 = rest[i2].open2;
    			temp[i].close2 = rest[i2].close2;
    			strcpy(temp[i].days_open, rest[i2].days_open);
    			temp[i].waitime = rest[i2].waitime;
    			temp[i].avg_cost = rest[i2].avg_cost;
    			temp[i].family = rest[i2].family;
    			i++;
    			i2++;
    		}
    	}
    
    	if( i1 > last1 )
    	{
    		while( i2 <= last2 )
    		{
    			strcpy(temp[i].name, rest[i2].name);
    			temp[i].code = rest[i2].code;
    			temp[i].food = rest[i2].food;
    			temp[i].entree = rest[i2].entree;
    			temp[i].high_cost = rest[i2].high_cost;
    			temp[i].low_cost = rest[i2].low_cost;
    			temp[i].distance = rest[i2].distance;
    			temp[i].children = rest[i2].children;
    			temp[i].senior = rest[i2].senior;
    			temp[i].age = rest[i2].age;
    			temp[i].open1 = rest[i2].open1;
    			temp[i].close1 = rest[i2].close1;
    			temp[i].open2 = rest[i2].open2;
    			temp[i].close2 = rest[i2].close2;
    			strcpy(temp[i].days_open, rest[i2].days_open);
    			temp[i].waitime = rest[i2].waitime;
    			temp[i].avg_cost = rest[i2].avg_cost;
    			temp[i].family = rest[i2].family;
    			i++;
    			i2++;
    		}
    	}
    	else
    	{
    		while( i1 <= last1 )
    		{
    			strcpy(temp[i].name, rest[i1].name);
    			temp[i].code = rest[i1].code;
    			temp[i].food = rest[i1].food;
    			temp[i].entree = rest[i1].entree;
    			temp[i].high_cost = rest[i1].high_cost;
    			temp[i].low_cost = rest[i1].low_cost;
    			temp[i].distance = rest[i1].distance;
    			temp[i].children = rest[i1].children;
    			temp[i].senior = rest[i1].senior;
    			temp[i].age = rest[i1].age;
    			temp[i].open1 = rest[i1].open1;
    			temp[i].close1 = rest[i1].close1;
    			temp[i].open2 = rest[i1].open2;
    			temp[i].close2 = rest[i1].close2;
    			strcpy(temp[i].days_open, rest[i1].days_open);
    			temp[i].waitime = rest[i1].waitime;
    			temp[i].avg_cost = rest[i1].avg_cost;
    			temp[i].family = rest[i1].family;
    			i++;
    			i1++;
    		}
    	}
    
    	fi = 0;
    	while( fi <= num - 1 )
    	{
    		strcpy(rest[first1].name, temp[fi].name);
    		rest[first1].code = temp[fi].code;
    		rest[first1].food = temp[fi].food;
    		rest[first1].entree = temp[fi].entree;
    		rest[first1].high_cost = temp[fi].high_cost;
    		rest[first1].low_cost = temp[fi].low_cost;
    		rest[first1].distance = temp[fi].distance;
    		rest[first1].children = temp[fi].children;
    		rest[first1].senior = temp[fi].senior;
    		rest[first1].age = temp[fi].age;
    		rest[first1].open1 = temp[fi].open1;
    		rest[first1].close1 = temp[fi].close1;
    		rest[first1].open2 = temp[fi].open2;
    		rest[first1].close2 = temp[fi].close2;
    		strcpy(rest[first1].days_open, temp[fi].days_open);
    		rest[first1].waitime = temp[fi].waitime;
    		rest[first1].avg_cost = temp[fi].avg_cost;
    		rest[first1].family = temp[fi].family;
    		first1++;
    		fi++;
    	}
    }
    Last edited by horace1; Mar 25 '07, 07:55 AM. Reason: added code tags
  • langesh
    New Member
    • Mar 2007
    • 1

    #2
    the problem for Segmentation fault is u r accessing some memory but memory is not allocated.

    see where u r access with out allocating memory.

    or use GDB to find where its giving Segmentation fault....



    Originally posted by codeveloper
    I have written a function where I have to use merge sort.
    My program is due Monday. Please, if anyone can help me out. Here is the code, plus I have declared a struct which I am using.

    typedef struct restaurant
    {
    char name[STRING];
    int code;
    char food;
    int entree;
    float high_cost;
    float low_cost;
    float distance;
    char children;
    char senior;
    int age;
    int open1;
    int close1;
    int open2;
    int close2;
    char days_open[8];
    int waitime;
    float avg_cost;
    char family;
    } REST;

    /*************** *************** *************** *************** *************** ****
    FUNCTION NAME : sort_by_food
    INPUTS : array of sturcts rest, int first and last
    RETURNS : none
    PURPOSE : sorts the data by the type of food
    *************** *************** *************** *************** *************** ****/

    void sort_by_food( REST rest[], int first, int last )
    {
    int middle;

    middle = ( first + last ) / 2;
    if( first < last )
    {
    sort_by_food( rest, first, middle );
    sort_by_food( rest, middle + 1, last );
    merge_food( rest, first, middle, middle + 1, last );
    }
    }

    /*************** *************** *************** *************** *************** ****
    FUNCTION NAME : merge_food
    INPUTS : array of sturcts rest, int first1, last1, first2, last2
    RETURNS : none
    PURPOSE : sorts the data by the type of food
    *************** *************** *************** *************** *************** ****/

    void merge_food( REST rest[], int first1, int last1, int first2, int last2 )
    {
    REST temp[SIZE];

    int i, i1, i2; // index, index 1, index 2
    int num, fi; // num, find index

    i = 0;
    i1 = first1;
    i2 = first2;
    num = last1 - first1 + last2 - first2 + 2;
    while( (i1 <= last1) && (i2 <= last2) )
    {
    if( rest[i1].food < rest[i2].food)
    {
    strcpy(temp[i].name, rest[i1].name);
    temp[i].code = rest[i1].code;
    temp[i].food = rest[i1].food;
    temp[i].entree = rest[i1].entree;
    temp[i].high_cost = rest[i1].high_cost;
    temp[i].low_cost = rest[i1].low_cost;
    temp[i].distance = rest[i1].distance;
    temp[i].children = rest[i1].children;
    temp[i].senior = rest[i1].senior;
    temp[i].age = rest[i1].age;
    temp[i].open1 = rest[i1].open1;
    temp[i].close1 = rest[i1].close1;
    temp[i].open2 = rest[i1].open2;
    temp[i].close2 = rest[i1].close2;
    strcpy(temp[i].days_open, rest[i1].days_open);
    temp[i].waitime = rest[i1].waitime;
    temp[i].avg_cost = rest[i1].avg_cost;
    temp[i].family = rest[i1].family;
    i++;
    i1++;
    }
    else
    {
    strcpy(temp[i].name, rest[i2].name);
    temp[i].code = rest[i2].code;
    temp[i].food = rest[i2].food;
    temp[i].entree = rest[i2].entree;
    temp[i].high_cost = rest[i2].high_cost;
    temp[i].low_cost = rest[i2].low_cost;
    temp[i].distance = rest[i2].distance;
    temp[i].children = rest[i2].children;
    temp[i].senior = rest[i2].senior;
    temp[i].age = rest[i2].age;
    temp[i].open1 = rest[i2].open1;
    temp[i].close1 = rest[i2].close1;
    temp[i].open2 = rest[i2].open2;
    temp[i].close2 = rest[i2].close2;
    strcpy(temp[i].days_open, rest[i2].days_open);
    temp[i].waitime = rest[i2].waitime;
    temp[i].avg_cost = rest[i2].avg_cost;
    temp[i].family = rest[i2].family;
    i++;
    i2++;
    }
    }

    if( i1 > last1 )
    {
    while( i2 <= last2 )
    {
    strcpy(temp[i].name, rest[i2].name);
    temp[i].code = rest[i2].code;
    temp[i].food = rest[i2].food;
    temp[i].entree = rest[i2].entree;
    temp[i].high_cost = rest[i2].high_cost;
    temp[i].low_cost = rest[i2].low_cost;
    temp[i].distance = rest[i2].distance;
    temp[i].children = rest[i2].children;
    temp[i].senior = rest[i2].senior;
    temp[i].age = rest[i2].age;
    temp[i].open1 = rest[i2].open1;
    temp[i].close1 = rest[i2].close1;
    temp[i].open2 = rest[i2].open2;
    temp[i].close2 = rest[i2].close2;
    strcpy(temp[i].days_open, rest[i2].days_open);
    temp[i].waitime = rest[i2].waitime;
    temp[i].avg_cost = rest[i2].avg_cost;
    temp[i].family = rest[i2].family;
    i++;
    i2++;
    }
    }
    else
    {
    while( i1 <= last1 )
    {
    strcpy(temp[i].name, rest[i1].name);
    temp[i].code = rest[i1].code;
    temp[i].food = rest[i1].food;
    temp[i].entree = rest[i1].entree;
    temp[i].high_cost = rest[i1].high_cost;
    temp[i].low_cost = rest[i1].low_cost;
    temp[i].distance = rest[i1].distance;
    temp[i].children = rest[i1].children;
    temp[i].senior = rest[i1].senior;
    temp[i].age = rest[i1].age;
    temp[i].open1 = rest[i1].open1;
    temp[i].close1 = rest[i1].close1;
    temp[i].open2 = rest[i1].open2;
    temp[i].close2 = rest[i1].close2;
    strcpy(temp[i].days_open, rest[i1].days_open);
    temp[i].waitime = rest[i1].waitime;
    temp[i].avg_cost = rest[i1].avg_cost;
    temp[i].family = rest[i1].family;
    i++;
    i1++;
    }
    }

    fi = 0;
    while( fi <= num - 1 )
    {
    strcpy(rest[first1].name, temp[fi].name);
    rest[first1].code = temp[fi].code;
    rest[first1].food = temp[fi].food;
    rest[first1].entree = temp[fi].entree;
    rest[first1].high_cost = temp[fi].high_cost;
    rest[first1].low_cost = temp[fi].low_cost;
    rest[first1].distance = temp[fi].distance;
    rest[first1].children = temp[fi].children;
    rest[first1].senior = temp[fi].senior;
    rest[first1].age = temp[fi].age;
    rest[first1].open1 = temp[fi].open1;
    rest[first1].close1 = temp[fi].close1;
    rest[first1].open2 = temp[fi].open2;
    rest[first1].close2 = temp[fi].close2;
    strcpy(rest[first1].days_open, temp[fi].days_open);
    rest[first1].waitime = temp[fi].waitime;
    rest[first1].avg_cost = temp[fi].avg_cost;
    rest[first1].family = temp[fi].family;
    first1++;
    fi++;
    }
    }

    Comment

    • horace1
      Recognized Expert Top Contributor
      • Nov 2006
      • 1510

      #3
      you are probably exceeding the bounds of an array - either use a debugger or put print statements in to try to localise the problem, e.g.

      Code:
      void sort_by_food( REST rest[], int first, int last )
      {
      int middle;
      
      middle = ( first + last ) / 2;
      if( first < last )
      {
      printf("1 first %d last %d  midel %d\n",first, last, middle);
      sort_by_food( rest, first, middle );
      printf("2 first %d last %d  midel %d\n",first, last, middle);
      sort_by_food( rest, middle + 1, last );
      printf("3 first %d last %d  midel %d\n",first, last, middle);
      merge_food( rest, first, middle, middle + 1, last );
      }
      }
      when you know which function the error is on put print statements in that etc

      recursive functions such as sort_by_food() can also run out of stack space - how big is SIZE ?

      Comment

      Working...