Working Program Error

Hello,
First thread here, but I have a simple program that does the following.
Takes user input and enters it to an array. (of ints)

Then sorts them in order using different sorting algorithms.

The program works fine, but when you input large numbers or a long list the program crashes. Why is this?

There are three files, header for sorting the cpp and the main cpp.

They can be looked at and downloaded here:
http://cl.ly/0X231N0Q1I0o2S0l1q0j

Thank You.
Is the array large enough to hold all the values in the long list?
1
2
3
4
5
6
7
8
9
10
11
12
    int x = 0;
    int Myarray[x];
    int copied[x];
    char holder[128] = {0};
    while(derp.isnumber(holder) == true)
    {
        cin.getline(holder,256);
        if(derp.isnumber(holder))
        {
            int c = atoi(holder);
            Myarray[x] = c;
            x = x + 1;
Arrays have fixed size. They don't expand when you access an out of bound element.
How do you mean?
When I run the program I get to just before merge sort and this happens:
http://cl.ly/2L2T1Z2c3c3u3O1j2X1R
I didn't bother downloading your zip file. If you can't reduce it to some compileable version that can be posted here, you're not going to get get many people looking at it.

But, just looking at the snippet ne555 posted, you have code that is not legal C++ and 3 potential accesses to memory that you don't own (of which at least 2 are realized.)

There are no variable length arrays in C++ so lines 2 and 3 are illegal. (You're probably taking advantage of a compiler extension to enable them.)

If they were legal, Myarray and copied would never be capable of holding any elements, since you conveniently told the compiler they have none. Thus accessing elements of those arrays means you're accessing memory that you can't legally access.

holder is capable of storing 128 chars. You told cin.getline() it's capable of holding 256 chars, which also means you're telling cin it's okay to trample on memory you don't own and it certainly is not.
Topic archived. No new replies allowed.