error message

Posted on 2003-02-27
Medium Priority
Last Modified: 2010-04-01
I'm testing a sort algorithm using an array, but as I increase the array size and run the program I get an error message.

This is the program:

#include <ctime>
#include <iostream>
#include <stdlib.h>
#include <limits.h>
using namespace std;
typedef unsigned long Item;

//Shellsort function
void shellsort(Item a[], int l, int r)
     int h;
     for(h=1; h <= (r-l)/9; h = 3*h+1);
     for(; h>0; h/= 3)
          for(int i= l+h; i<=r; i++)
               int j = i; Item v = a[i];
               while (j >= l+h && v < a[j-h])
                    a[j] = a[j-h]; j -= h;
               a[j] = v;

int main()
        //For this line I get 2147483647
        cout<<"Max. long = "<<LONG_MAX<<endl;
        //for this line I get -2147483647
        cout<<"Min. long = "<<LONG_MIN<<endl;
     //Clock initialization
     clock_t start, finish;
     start = clock();
     finish = clock();
     double delay = (double)(finish - start);
     double elapsed = (double)(finish - start);
     elapsed -= delay;
     //Array initialization
     const unsigned long MAX = 2000000;
     unsigned long DataArray[MAX];
     //Input data
     long a, b, temp;
     a = 0;
     b = MAX - 1;
     temp = b;
     cout<<"Loading array ..."<<endl;
     for(int i=0; i < MAX; i++)
          temp = temp - 1;
          DataArray[i] = temp;
     //Start time
     start = clock();
     //Sort data
     shellsort(DataArray, a, b);
     //Finish time
     finish = clock();
     //calculate time
     double clock_ticks = finish - start - delay;
     double sortTime = clock_ticks / CLOCKS_PER_SEC;
     //output data
     cout<<"It took "<<sortTime<<" seconds to sort the array."<<endl;
     return 0;
I would really appreciate if someone could tell me why this is happening if I add another zero to MAX.
Question by:aparicion25
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Expert Comment

ID: 8038548
What does the error message say?

Accepted Solution

fourgee earned 200 total points
ID: 8038570

The problem is that you've exceeded the memory that's been allocated at compile time when MAX = 2000000. To fix this problem you should dynamically allocate the memory for DataArray instead i.e.

unsigned long *DataArray = new unsigned long[2000000];

and then deallocate the memory at the end of your function with

delete [] DataArray;

Author Comment

ID: 8038662
Thanks, it worked!

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question