Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6061011
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T08:53:25+00:00 2026-05-23T08:53:25+00:00

I am working on a program which generates a segmentation fault and I cannot

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T08:53:25+00:00Added an answer on May 23, 2026 at 8:53 am

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on a program which manipulates images of different sizes. Many of
I am working on a simple CAD program which uses OpenGL to handle on-screen
I am working on a Windows utility program which communicates with some custom hardware
Background I am working on a phonetic converter program which converts english text into
I'm working on a Java 2D rendering program (running on 1.6.0_11), which uses external
I'm working on a project which generates a very large number of sequential text
I have a working program in C++ that generates data for a Mandelbrot Set.
I have an otherwise working iPhone program. A recent change means that it generates
I've got a program which is working and uses Core Data for storage. However,
What profilers have you used when working with .net programs, and which would you

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.