473,385 Members | 1,769 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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.
Expand|Select|Wrap|Line Numbers
  1. typedef struct restaurant
  2. {
  3.     char name[STRING];
  4.     int code;
  5.     char food;
  6.     int entree;
  7.     float high_cost;
  8.     float low_cost;
  9.     float distance;
  10.     char children;
  11.     char senior;
  12.     int age;
  13.     int open1;
  14.     int close1;
  15.     int open2;
  16.     int close2;
  17.     char days_open[8];
  18.     int waitime;
  19.     float avg_cost;
  20.     char family;
  21. } REST;
  22.  
  23. /*******************************************************************************
  24. FUNCTION NAME    :    sort_by_food
  25. INPUTS        :    array of sturcts rest, int first and last
  26. RETURNS        :    none
  27. PURPOSE        :    sorts the data by the type of food 
  28. *******************************************************************************/ 
  29.  
  30. void sort_by_food( REST rest[], int first, int last )
  31. {
  32.     int middle;
  33.  
  34.     middle = ( first + last ) / 2;
  35.     if( first < last )
  36.     {
  37.         sort_by_food( rest, first, middle );
  38.         sort_by_food( rest, middle + 1, last );
  39.         merge_food( rest, first, middle, middle + 1, last );
  40.     }
  41. }
  42.  
  43. /*******************************************************************************
  44. FUNCTION NAME    :    merge_food
  45. INPUTS        :    array of sturcts rest, int first1, last1, first2, last2
  46. RETURNS        :    none
  47. PURPOSE        :    sorts the data by the type of food 
  48. *******************************************************************************/ 
  49.  
  50. void merge_food( REST rest[], int first1, int last1, int first2, int last2 )
  51. {
  52.     REST temp[SIZE];
  53.  
  54.     int i, i1, i2; // index, index 1, index 2
  55.     int num, fi;    // num, find index 
  56.  
  57.     i = 0; 
  58.     i1 = first1;
  59.     i2 = first2;
  60.     num = last1 - first1 + last2 - first2 + 2;
  61.     while( (i1 <= last1) && (i2 <= last2) )
  62.     {
  63.         if( rest[i1].food < rest[i2].food)
  64.         {
  65.             strcpy(temp[i].name, rest[i1].name);
  66.             temp[i].code = rest[i1].code;
  67.             temp[i].food = rest[i1].food;
  68.             temp[i].entree = rest[i1].entree;
  69.             temp[i].high_cost = rest[i1].high_cost;
  70.             temp[i].low_cost = rest[i1].low_cost;
  71.             temp[i].distance = rest[i1].distance;
  72.             temp[i].children = rest[i1].children;
  73.             temp[i].senior = rest[i1].senior;
  74.             temp[i].age = rest[i1].age;
  75.             temp[i].open1 = rest[i1].open1;
  76.             temp[i].close1 = rest[i1].close1;
  77.             temp[i].open2 = rest[i1].open2;
  78.             temp[i].close2 = rest[i1].close2;
  79.             strcpy(temp[i].days_open, rest[i1].days_open);
  80.             temp[i].waitime = rest[i1].waitime;
  81.             temp[i].avg_cost = rest[i1].avg_cost;
  82.             temp[i].family = rest[i1].family;
  83.             i++;
  84.             i1++;
  85.         }
  86.         else
  87.         {
  88.             strcpy(temp[i].name, rest[i2].name);
  89.             temp[i].code = rest[i2].code;
  90.             temp[i].food = rest[i2].food;
  91.             temp[i].entree = rest[i2].entree;
  92.             temp[i].high_cost = rest[i2].high_cost;
  93.             temp[i].low_cost = rest[i2].low_cost;
  94.             temp[i].distance = rest[i2].distance;
  95.             temp[i].children = rest[i2].children;
  96.             temp[i].senior = rest[i2].senior;
  97.             temp[i].age = rest[i2].age;
  98.             temp[i].open1 = rest[i2].open1;
  99.             temp[i].close1 = rest[i2].close1;
  100.             temp[i].open2 = rest[i2].open2;
  101.             temp[i].close2 = rest[i2].close2;
  102.             strcpy(temp[i].days_open, rest[i2].days_open);
  103.             temp[i].waitime = rest[i2].waitime;
  104.             temp[i].avg_cost = rest[i2].avg_cost;
  105.             temp[i].family = rest[i2].family;
  106.             i++;
  107.             i2++;
  108.         }
  109.     }
  110.  
  111.     if( i1 > last1 )
  112.     {
  113.         while( i2 <= last2 )
  114.         {
  115.             strcpy(temp[i].name, rest[i2].name);
  116.             temp[i].code = rest[i2].code;
  117.             temp[i].food = rest[i2].food;
  118.             temp[i].entree = rest[i2].entree;
  119.             temp[i].high_cost = rest[i2].high_cost;
  120.             temp[i].low_cost = rest[i2].low_cost;
  121.             temp[i].distance = rest[i2].distance;
  122.             temp[i].children = rest[i2].children;
  123.             temp[i].senior = rest[i2].senior;
  124.             temp[i].age = rest[i2].age;
  125.             temp[i].open1 = rest[i2].open1;
  126.             temp[i].close1 = rest[i2].close1;
  127.             temp[i].open2 = rest[i2].open2;
  128.             temp[i].close2 = rest[i2].close2;
  129.             strcpy(temp[i].days_open, rest[i2].days_open);
  130.             temp[i].waitime = rest[i2].waitime;
  131.             temp[i].avg_cost = rest[i2].avg_cost;
  132.             temp[i].family = rest[i2].family;
  133.             i++;
  134.             i2++;
  135.         }
  136.     }
  137.     else
  138.     {
  139.         while( i1 <= last1 )
  140.         {
  141.             strcpy(temp[i].name, rest[i1].name);
  142.             temp[i].code = rest[i1].code;
  143.             temp[i].food = rest[i1].food;
  144.             temp[i].entree = rest[i1].entree;
  145.             temp[i].high_cost = rest[i1].high_cost;
  146.             temp[i].low_cost = rest[i1].low_cost;
  147.             temp[i].distance = rest[i1].distance;
  148.             temp[i].children = rest[i1].children;
  149.             temp[i].senior = rest[i1].senior;
  150.             temp[i].age = rest[i1].age;
  151.             temp[i].open1 = rest[i1].open1;
  152.             temp[i].close1 = rest[i1].close1;
  153.             temp[i].open2 = rest[i1].open2;
  154.             temp[i].close2 = rest[i1].close2;
  155.             strcpy(temp[i].days_open, rest[i1].days_open);
  156.             temp[i].waitime = rest[i1].waitime;
  157.             temp[i].avg_cost = rest[i1].avg_cost;
  158.             temp[i].family = rest[i1].family;
  159.             i++;
  160.             i1++;
  161.         }
  162.     }
  163.  
  164.     fi = 0;
  165.     while( fi <= num - 1 )
  166.     {
  167.         strcpy(rest[first1].name, temp[fi].name);
  168.         rest[first1].code = temp[fi].code;
  169.         rest[first1].food = temp[fi].food;
  170.         rest[first1].entree = temp[fi].entree;
  171.         rest[first1].high_cost = temp[fi].high_cost;
  172.         rest[first1].low_cost = temp[fi].low_cost;
  173.         rest[first1].distance = temp[fi].distance;
  174.         rest[first1].children = temp[fi].children;
  175.         rest[first1].senior = temp[fi].senior;
  176.         rest[first1].age = temp[fi].age;
  177.         rest[first1].open1 = temp[fi].open1;
  178.         rest[first1].close1 = temp[fi].close1;
  179.         rest[first1].open2 = temp[fi].open2;
  180.         rest[first1].close2 = temp[fi].close2;
  181.         strcpy(rest[first1].days_open, temp[fi].days_open);
  182.         rest[first1].waitime = temp[fi].waitime;
  183.         rest[first1].avg_cost = temp[fi].avg_cost;
  184.         rest[first1].family = temp[fi].family;
  185.         first1++;
  186.         fi++;
  187.     }
  188. }
Mar 25 '07 #1
2 942
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....



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++;
}
}
Mar 25 '07 #2
horace1
1,510 Expert 1GB
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.

Expand|Select|Wrap|Line Numbers
  1. void sort_by_food( REST rest[], int first, int last )
  2. {
  3. int middle;
  4.  
  5. middle = ( first + last ) / 2;
  6. if( first < last )
  7. {
  8. printf("1 first %d last %d  midel %d\n",first, last, middle);
  9. sort_by_food( rest, first, middle );
  10. printf("2 first %d last %d  midel %d\n",first, last, middle);
  11. sort_by_food( rest, middle + 1, last );
  12. printf("3 first %d last %d  midel %d\n",first, last, middle);
  13. merge_food( rest, first, middle, middle + 1, last );
  14. }
  15. }
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 ?
Mar 25 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: sivignon | last post by:
Hi, I'm writing a php script which deals with 3 ORACLE databases. This script is launch by a script shell on an linux machine like this : /../php/bin/php ./MySript.php (PHP 4.3.3) My script...
3
by: diyanat | last post by:
i am writing a cgi script in C using the CGIC library, the script fails to run, i am using apache on linux error report from apache : internal server error Premature end of script headers:...
16
by: laberth | last post by:
I've got a segmentation fault on a calloc and I don'tunderstand why? Here is what I use : typedef struct noeud { int val; struct noeud *fgauche; struct noeud *fdroit; } *arbre; //for those...
3
by: Zheng Da | last post by:
Program received signal SIGSEGV, Segmentation fault. 0x40093343 in _int_malloc () from /lib/tls/libc.so.6 (gdb) bt #0 0x40093343 in _int_malloc () from /lib/tls/libc.so.6 #1 0x40094c54 in malloc...
5
by: Fra-it | last post by:
Hi everybody, I'm trying to make the following code running properly, but I can't get rid of the "SEGMENTATION FAULT" error message when executing. Reading some messages posted earlier, I...
18
by: Digital Puer | last post by:
Hi, I'm coming over from Java to C++, so please bear with me. In C++, is there a way for me to use exceptions to catch segmentation faults (e.g. when I access a location off the end of an array)?...
27
by: Paminu | last post by:
I have a wierd problem. In my main function I print "test" as the first thing. But if I run the call to node_alloc AFTER the printf call I get a segmentation fault and test is not printed! ...
7
by: pycraze | last post by:
I would like to ask a question. How do one handle the exception due to Segmentation fault due to Python ? Our bit operations and arithmetic manipulations are written in C and to some of our...
3
by: madunix | last post by:
My Server is suffering bad lag (High Utlization) I am running on that server Oracle10g with apache_1.3.35/ php-4.4.2 Web visitors retrieve data from the web by php calls through oci cobnnection...
6
by: DanielJohnson | last post by:
int main() { printf("\n Hello World"); main; return 0; } This program terminate just after one loop while the second program goes on infinitely untill segmentation fault (core dumped) on...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.