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 3441052
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T08:30:19+00:00 2026-05-18T08:30:19+00:00

Ok, so I have a simple c++ program that’s supposed to run a couple

  • 0

Ok, so I have a simple c++ program that’s supposed to run a couple sorting algorithms on an array composed of ints and track the time each one takes.. pretty basic, however I’ve run into a problem.

When the program first starts, it asks how many items you would like in the array. My assignment involves setting the array at specific lengths from 100 items all the way to 750000. It’ll handle many values, including up to around 600000. When I try 750000 however it immediately segfaults. A couple couts here and there led me to discover that the error happens when the fourth array (all of the same length) is initialized. The weird thing is that it only happens on my OS; at my school it works no problem. (i’m on the latest ubuntu while my school uses redhat. not sure if that’s useful)

I’ll include the complete code just for reference but the segfault occurs at line 27:

int array1[num], array2[num], array3[num], array4[num]; // initialize arrays 

I know this because I initialized each array on separate lines and put couts in between. array1, 2, and 3 were initialized, then it segfaults. Again this ONLY happens when the arrays are longer than about 600000 or so. Anything less works fine.

Full code:

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

void insertionSort(int array[], int size);
void bubbleSort(int array[], int size);
void mergeSort(int array[], int first, int last, int size);
void quickSort(int array[], int size);

int main()
{

    cout << endl << endl << "\t\t**** Extra Credit Assignment- Sorting ****" << endl << endl << endl;
    cout << "Enter the number of items to sort: ";
    int num; 
    cin >> num;
    while(cin.fail()) // while cin does not recieve an integer 
    {
        cin.clear();
        cin.ignore(1000, '\n');
        cout << "Invalid entry. Try again: "; // try again
        cin >> num;
    }

    int array1[num], array2[num], array3[num], array4[num]; // initialize arrays 

    int randNum, sizeOfArray = sizeof(array1)/sizeof(array1[0]); // random number, size of the arrays 
    srand(time(NULL)); // random seed (used with rand()) 

    for(int i = 0; i < sizeOfArray; i++) // traverse through the array
    {
        randNum = rand() % 2147483647+1; // establish random number (from 1 to 2147483647)
        array1[i] = array2[i] = array3[i] = array3[i] = randNum; // set randNum to all arrays at current index
    }

    time_t beginTime, endTime; 
    double elapsedTime;

    cout << endl << "Elapsed time:" << endl << "\tInsertion Sort-\t";

    time(&beginTime);
    insertionSort(array1, sizeOfArray);
    time(&endTime);

    elapsedTime = difftime(endTime, beginTime);
    cout << elapsedTime << " seconds" << endl << "\tBubble Sort-\t";

    time(&beginTime);
    bubbleSort(array2, sizeOfArray);
    time(&endTime);

    elapsedTime = difftime(endTime, beginTime);
    cout << elapsedTime << " seconds" << endl << "\tMerge Sort-\t";

    time(&beginTime);
    mergeSort(array3, 0, sizeOfArray-1, sizeOfArray);
    time(&endTime);

    elapsedTime = difftime(endTime, beginTime);
    cout << elapsedTime << " seconds"<< endl; 





/* ********************* TESTING *************************
// *******************************************************

    cout << "Insertion->Unsorted:\t";
    for(int i = 0; i < sizeOfArray; i++)
    {
        cout <<  array1[i] << " ";
    }
    cout << endl;

    insertionSort(array1, sizeOfArray);

    cout << "Insertion->Sorted:\t";
    for(int i = 0; i < sizeOfArray; i++)
    {
        cout <<  array1[i] << " ";
    }
    cout << endl;




    cout << "Bubble->Unsorted:\t";
    for(int i = 0; i < sizeOfArray; i++)
    {
        cout <<  array2[i] << " ";
    }
    cout << endl;

    bubbleSort(array2, sizeOfArray);

    cout << "Bubble->Sorted:\t\t";
    for(int i = 0; i < sizeOfArray; i++)
    {
        cout <<  array2[i] << " ";
    }
    cout << endl;




    cout << "Merge->Unsorted:\t";
    for(int i = 0; i < sizeOfArray; i++)
    {
        cout <<  array3[i] << " ";
    }
    cout << endl;

    mergeSort(array3, 0, sizeOfArray-1, sizeOfArray);

    cout << "Merge->Sorted:\t\t";
    for(int i = 0; i < sizeOfArray; i++)
    {
        cout <<  array3[i] << " ";
    }
    cout << endl; */

    return 0;
}

void insertionSort(int array[], int size)
{
    for(int i = 1; i < size; i++)
    {
        int item = array[i], index = i;
        while(index > 0 && array[index-1] > item)
        {
            array[index] = array[index-1];
            index--;
        }
        array[index] = item;
    }
}

void bubbleSort(int array[], int size)
{
    bool sorted = false;
    for(int i = 1; i < size && !sorted; i++)
    {
        sorted = true;
        for(int i2 = 0; i2 < size-i; i2++)
        {
            int nextI = i2+1;
            if(array[i2] > array[nextI])
            {
                swap(array[i2], array[nextI]);
                sorted = false;
            }
        }
    }
}

void merge(int array[], int first, int mid, int last, int size)
{
    int tempArray[size];
    int first1 = first, first2 = mid+1;
    int last1 = mid, last2 = last;
    int index = first1;

    while(first1 <= last1 && first2 <= last2)
    {
        if(array[first1] < array[first2])
        {
            tempArray[index] = array[first1];
            first1++;
        }
        else
        {
            tempArray[index] = array[first2];
            first2++;
        }
        index++;
    }
    while(first1 <= last1)
    {
        tempArray[index] = array[first1];
        first1++;
        index++;
    }
    while(first2 <= last2)
    {
        tempArray[index] = array[first2];
        first2++;
        index++;
    }

    for(index = first; index <= last; index++)
    {
        array[index] = tempArray[index];
    }
}

void mergeSort(int array[], int first, int last, int size)
{
    if(first < last)
    {
        int mid = (first+last)/2;
        mergeSort(array, first, mid, size);
        mergeSort(array, mid+1, last, size);
        merge(array, first, mid, last, size);
    }
}

Any help is greatly appreciated. It might be a memory limitation on my system? I really don’t know lol just a thought.

  • 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-18T08:30:19+00:00Added an answer on May 18, 2026 at 8:30 am

    As others have noted, the SEGV you’re getting is due to overflowing the stack. The reason it happens on your ubuntu machine and not your school’s redhat machine is likely due to differences in the default stack size.

    You may be able to change your default stack size with ulimit -s which, with no additional arguments, will print your current stack size in kilobytes. For example, on my machine, that prints 8192 or 8 megabytes. I can raise it to 16MB with ulimit -s 16384

    An array of 750000 ints will require about 3MB of stack space (4 bytes per int), so 4 such arrays (like you have) will require 12MB…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple Tray icon program that opens a site using System.Diagnostics.Process.Start(URL) And
I have two simple while loops in my program that I feel ought to
I have a relatively simple select statement in a VB6 program that I have
I have a simple script which is used to start another program. This other
Have a look at this very simple example WPF program: <Window x:Class=WpfApplication1.Window1 xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml
I have the need to express simple conditionals as program input. The input must
Does anyone have sample code to copy open (in-use and locked by another program)
I have simple regex \.*\ for me its says select everything between and ,
Ok, i have simple scenario: have two pages: login and welcome pages. im using
In general, is it a best practice to have simple POJO Java classes implement

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.