1. No Profile Picture
Junior Member
Devshed Newbie (0 - 499 posts)

Join Date
Sep 2003
Location
Boston
Posts
7
Rep Power
0

#### Segmentation Fault

If someone could help figure out why this code gives me a "segementation fault" message everytime I try to run it, that would be greatly appreciated.
Code:
```/* File: Prog-2-10.c
* -----------------
* This program takes in values from the user and when the sentinel value is
* entered, a function will find and return the maximum and minimum values of the
* values entered.
*/

#include <stdio.h>
#include "genlib.h"
#include "simpio.h"

/* Constant Assignments */
#define SENTINEL -1
#define HUGE 99999999

/* Function Prototypes */

void maxMin(double arr[], int numberInArr, double *pmin, double *pmax);

/* Main Function */

int
main()
{
int numberInArray;
double numberEntered, max, min;
double array[HUGE];

printf("Enter the elements of the array, one per line.\n");
printf("Use -1 to signal the end of the list.\n.");

numberInArray = 0;

do
{
numberEntered = GetReal();
numberInArray++;
}
while (numberEntered != SENTINEL);

maxMin(array, numberInArray, &min, &max);

printf("The range of values is %d-%d ", min, max);

return(0);
}

void maxMin(double arr[], int numberInArr, double *pmin, double *pmax)
{
int i;
double maximum, minimum;
double *a;

a = malloc(numberInArr * sizeof(double));

maximum = arr[0];
minimum = arr[0];

for(i=0; i < numberInArr; i++)
{
a[i]=arr[i];

if(a[i]> maximum)
maximum = a[i];

if (a[i]<minimum)
minimum = a[i];
}

pmin = &minimum;
pmax = &maximum;

}```
*edit :: placed code tags around program*
Last edited by Onslaught; September 19th, 2003 at 08:50 AM.
2. try stepping the program through a debugger to see where you are getting the segmentation fault thrown from.
3. I think I found the problem. In one place at least; since it's in your technique you may have used the same technique in more than one place.

You wrote:
Code:
```void maxMin(double arr[], int numberInArr, double *pmin, double *pmax)
{
int i;
double maximum, minimum;
double *a;

a = malloc(numberInArr * sizeof(double));

maximum = arr[0];
minimum = arr[0];

for(i=0; i < numberInArr; i++)
{
a[i]=arr[i];

if(a[i]> maximum)
maximum = a[i];

if (a[i]<minimum)
minimum = a[i];
}

pmin = &minimum;
pmax = &maximum;

}```
Bottom line: you set the pointer arguments to the addresses of two local variables, but when you exit the function then those variable go away, they no longer exist.

You got the idea right about needing to pass pointers to change the arguments passed to a function, but you misapplied the technique. You need to assign the values to the location that the pointer is pointing to, not assign new locations.

BTW, it wouldn't even do that , because the changes to pmin and pmax remain local to the function and do nothing to affect the actual variables passed. You need to dereference the pointers (ie, *pmin) and assign the value of minimum and maximum to them. BTW, the function call itself looks correct.

OK, as you were. What I found was a definite error that needs to be corrected, but now I cannot see how it would cause a segmentation fault.

One other thing:
Remember that every variable that is declared locally within a function is placed on the stack. If you are on a Wintel platform, that stack cannot be any larger than 64KB. So declaring a large data structure, such as a huge array, locally is generally a very bad idea that can cause things to blow up (software-wise; first time I tried it I blew the stack all the way to the next county, figuratively speaking). Come to think of it, this might be what's causing the segmentation error.

You should move that huge array out of the main function and make it global. Or else keep a pointer to it as a local variable but malloc the space for it out in the heap. Whatever it takes to keep the actual array off the stack.