[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

error message

Posted on 2003-02-27
3
Medium Priority
?
187 Views
Last Modified: 2010-04-01
Hi:
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.
Thanks.
0
Comment
Question by:aparicion25
3 Comments
 

Expert Comment

by:dude1337
ID: 8038548
What does the error message say?
0
 

Accepted Solution

by:
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;
0
 

Author Comment

by:aparicion25
ID: 8038662
Thanks, it worked!
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

612 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