Ms01
Ms01

Reputation: 4712

Why does this pointer/integer comparison generate a segmentation fault?

I am working on a program which generates a segmentation fault and I cannot understand why. If I remove the pointer declaration from "lowest" and "largest" variables and use them as pointerless integers the program works fine.

However, as soon as I try to use pointers, problems arise and I receive a segmentation fault. I realize that this is probably a very easy and well addressed issue, but I've tried to understand the code by looking on other similar problems. I haven't found a solution for my problem yet. Nor do I understand what's going wrong.

This is the code generating the problem (link to full source is below):

     cout << "This is the array containing the random numbers:\n";
 for(int *i=numbers; i != numbers + arrLength; i++) {
     if((*i % 200) == 0 && *i > 200) {
         cin.get();
         cout << endl;
     }
     else
         cout << *i << ' ';

     // Get statistics
     // In the continuation of getting, lowest, largest then adding to sum.
     // THIS PART IS MAKING SEGMENTATION FAULT.
     if(*i < *lowest)
         lowest =  i;
     if(*i > *largest)
         largest = i;
     sum += *i;
 }

The i variable points to an old reference which is declared after user input:

cout << "You entered: " << arrLength << "\n\n";

 int *numbers = new int[arrLength];

 // Fill the array with random numbers
 srand(time(NULL));
 int x;
 int range = 5001;
 for(int index=0; index<arrLength; index++){
     *(numbers + index) = rand() % range;
     x = rand() % 2;
     if(x > 0) {
         *(numbers + index) = *(numbers + index) * -1;
     }
 }

Please explain to be why my program isn't working and what I am doing wrong. As I've said earlier everything works except the:

if(*i < *lowest)
         lowest =  i;
     if(*i > *largest)
         largest = i;

Full source: http://pastie.org/2105963

Thanks in advance on this matter!

Upvotes: 3

Views: 2470

Answers (6)

VGE
VGE

Reputation: 4191

Your programming is using invalid memory (lowest and largest pointer). But !

Pointers are not necessary in this code

In fact the program has an unrequired complexity. Why using pointer, when you can use indices ?

You will problably found your allocation/initialisation error.

 for(int index=0; index<arrLength; index++){
     numbers[index] = rand() % range;
     x = rand() % 2;
     if(x > 0) {
         numbers[index] = numbers[index] * -1;
     }
 }

You need only a pointer to create the array.

int *numbers = new int[arrayLength];

And finally your program will be simpler :

int lowest =numbers[0], largest = lowest;
 for(int i=0; i < arrLength; i++) {
     int ii = numbers[i ];                      )
     if((ii % 200) == 0 && ii > 200) {
         cin.get();
         cout << endl;
     }
     else
         cout << ii << ' ';

     if(ii < lowest)
         lowest =  ii;
     if(ii > largest)
         largest = ii;
     sum += ii;
 }

Upvotes: 1

Skizz
Skizz

Reputation: 71080

You are initialising lowest and largest to 0 and the dereference is accessing memory via a null pointer and so it falls over. Try this instead:

if (!lowest || *i < *lowest)
    lowest =  i;
if (!largest || *i > *largest)
    largest = i;

or:

int *numbers = new int[arrLength];
lowest = largers = numbers; // initialise to something non-null

Upvotes: 0

grayDad
grayDad

Reputation: 324

You are initializing the lowest & largest value pointers to 0 then dereferencing them later without pointing them to a valid value.

int *largest = 0, *lowest = 0, sum = 0;

Upvotes: 1

karadoc
karadoc

Reputation: 2721

are lowest and largest initialized before you dereference them? Using *lowest will cause a seg fault if lowest doesn't point to a valid address.

you can try setting lowest = largest = numbers; somewhere at the start. Maybe that will help you.

Upvotes: 1

Armen Tsirunyan
Armen Tsirunyan

Reputation: 133054

int *largest = 0, *lowest = 0, sum = 0;

This is your problem. You never allocate memory or assign a valid address to these. And when you dereference a NULL pointer, like here

if(*i < *lowest) //lowest is NULL

you get Undefined Behavior, which includes segmentation fault, nasal demons, and anything else

Upvotes: 3

Šimon T&#243;th
Šimon T&#243;th

Reputation: 36443

It doesn't work, because it doesn't compare pointers. You are comparing the values stored at some memory addresses, which most likely don't belong to your program, therefore you are shot down by a SIGSEGV.

You can't access memory that doesn't belong to your program.

Upvotes: 2

Related Questions