473,394 Members | 1,671 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Dynamic array generation fails with over 100 elements

So I'm making a heapsort program... all my sort code seems to work just fine. Only problem is, the part where I create the array to test it bombs out whenever the number of elements is over 100. The code is like this...

Expand|Select|Wrap|Line Numbers
  1.   short max;
  2.   cout << "\nHeapsort Program\n\n";
  3.   cout << "Enter # of elements to generate: ";
  4.   cin >> max;
  5.   short* unsortedList = new short[max];
  6.  
  7.   cout << "Generating list of integers...";
  8.   for (short x=0; x<max; x++)
  9.   {
  10.     unsortedList[x] = (rand()%100)+1;
  11.   }
  12.   cout << " done.\n\nInitializing sort...";
  13.   //actual sort code follows, of course
...and when I enter a number over 100 for the number of elements, I get this error...

terminate called after throwing an instance of 'std::exception'
what(): St9exception
Initializing sort...Aborted

Bear in mind that printing "Initializing sort..." is actually part of my code. So it throws an exception buts somehow executes the next line anyway. Also, when the number is really high (500,000 for example), it generates this error instead:

terminate called after throwing an instance of 'std::bad_alloc'
what(): St9bad_alloc
Aborted

Note the lack of "Initializing sort..." in this case, meaning it terminated before the next line this time. At first I thought it was just running out of memory, so I changed the ints to shorts (as seen in the code), but it didn't change anything.

Oh, and as a technical point, I'm using Linux GPP, and the use of a pointer is necessary to how the heap works (it's essentially a priority queue).
Oct 2 '07 #1
3 1915
weaknessforcats
9,208 Expert Mod 8TB
I am not getting an error for the number of elements greater than 100. Of couse, the short can only hold a value of 64K which is why the 500000 doesn't work.

I used Visual Studio.NET 2005.
Oct 2 '07 #2
arnaudk
424 256MB
I compiled and ran
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  3. int main(int argc, char *argv[]) {
  4.     short maX;
  5.     cout << "Enter # of elements to generate: ";
  6.     cin >> maX;
  7.     short* unsortedList = new short[maX];
  8.     cout << "Generating list of integers...";
  9.     for (short x=0; x<maX; x++) unsortedList[x] = (rand()%100)+1;
  10.     cout << " done.\n\nInitializing sort...\n";
  11.     unsigned long int n(0);
  12.     cin.clear();
  13.     for (short x=0; x<maX; x++) {
  14.             n++;
  15.             cout << unsortedList[x] << "\t";
  16.         }
  17.     cout << "\nn = " << n << ". Press enter to continue.\n";
  18.     cin.ignore(100, '\n');
  19.     cin.get();
  20.     return 0;
  21. }
  22.  
without any errors so the errors you apparently have to do with the rest of your program. It therefore makes sense that you see the message "Initializing sort...". Output (ignore line numbers):
Expand|Select|Wrap|Line Numbers
  1. Enter # of elements to generate: 20
  2. Generating list of integers... done.
  3. Initializing sort...
  4. 42      68      35      1       70      25      79      59      63      65
  5. 6       46      82      28      62      92      96      43      28      37
  6. n = 20. Press enter to continue.
  7.  
- The maximum value for a short int is 32767. When you specify a number greater than this, max is implicitly cast into a short int which may in effect be negative. You could then get a memory allocation error which is a fatal error and the program terminates immediately. I suggest you declare maX to be an unsigned long int. On my compiler, I don't get an error but I get an unpredictable number of random integers with the above script (when I entered 500000 I got n = 828 not 500000).
- max() is a method that is part of STL algorithms, perhaps you could use a different variable name.
Oct 2 '07 #3
Well, I tried that code in place of mine in the whole program, and then the error occurred when I started the sort instead of before. I don't know why that is, but it got me to checking my Enqueue function. It was then that I realized the problem: I had copied most of the code over from my first heap program, and still had my maxItems=100 line! So I changed maxItems to a parameter on the constructor and it works fine. As usual the problem was something completely unrelated to what I thought it was. Go fig.

It still gives me a "bad_alloc" error when I try to make the array too long (a billion elements bombed out, for example) but I'm pretty sure that's an out-of-memory error. This machine only has, I think, a gig of RAM, and a billion shorts is two gigs right there.

Problem solved. Thanks!
Oct 4 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Peter B. Steiger | last post by:
The latest project in my ongoing quest to evolve my brain from Pascal to C is a simple word game that involves stringing together random lists of words. In the Pascal version the whole array was...
11
by: Marco Loskamp | last post by:
Dear list, I'm trying to dynamically generate functions; it seems that what I really want is beyond C itself, but I'd like to be confirmed here. In the minimal example below, I'd like to...
4
by: Venus | last post by:
Hello, Thanks for your reply. I understand that a control can be created dynamically in several ways: 1) using StringBuilder 2) using Controls.Add 3) using ASP PlaceHolder But this is just...
0
by: Venus | last post by:
Hello, After trying some ways to do it I wanted to use something like the code below but for some reason is not working (I have to generate the entire form dynamically (not only the controls)):...
2
by: Ghada Al-Mashaqbeh via DotNetMonster.com | last post by:
Hi all, I am facing a problem in dynamic code generation at run time, the problem occurs when the dynmaic code use global data exist within the original application. Lets say that my...
1
by: Peterwkc | last post by:
Hello all expert, i have two program which make me desperate bu after i have noticed the forum, my future is become brightness back. By the way, my problem is like this i the first program was...
1
by: Gurur | last post by:
Hi all, I have a doubt. If I have 2 structures and one is parent of other , ie the child structure is present in the parent one . And if the child structure is declared as dynamic array in the...
7
by: dino d. | last post by:
Hi- I want to create a dynamic image with areas so that when the user clicks different areas, the user jumps to those pages. The problem is, I can't seem to figure out how to do this efficiently....
11
by: C C++ C++ | last post by:
Hi all, got this interview question please respond. How can you quickly find the number of elements stored in a a) static array b) dynamic array ? Rgrds MA
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.