Hey guys, I've been working on a little project of mine, and part of it requires sorting. I'm using bubble sort and before I put the algorithm in the code, the program ran fine.

void ranking(){
    int count = getcount("all");  // get total members and save value to count
    int* pArray = new int [count];
    string names[count];          // why can't I dynamically allocate memory to string arrays?

    // Get member names for point checking and sorting
    string name;
    ifstream members("members.txt");
    for(int i = 0; i < count; i++){
        members >> names[i];
    }
    members.close();

    // Get total points for each name and save to corresponding pArray element so names[i] has pArray[i] points
    for(int i = 0; i < count; i++)
        pArray[i] = totalpoints(names[i]);

    // Bubble Sort because it's simple and I don't need speed
    bool flag = true;
    for(int i = 0; (i < count) && (flag == true); i++){
        flag = false;
        for(int s = 0; s < count; s++){
            if(pArray[s] < pArray[s + 1]){
                // Sorts the names and total points keeping them in the same element number so names[i] has pArray[i] points
                int temp = 0;
                //string stemp;
                temp = pArray[s];
                pArray[s] = pArray[s + 1];
                pArray[s + 1] = temp;
                // stemp = names[s];
                // names[s] = names[s + 1];   Causes Segmentation Fualt (core dumped)
                // names[s + 1] = stemp;
                flag = true;
            }
        }
    }

    // Display for debug purposes
    for(int i = 0; i < count; i++)
            cout << pArray[i] << endl;

}

The sorting itself seems to work just fine(Except for one array element). But an error occurs and the program crashes. Here's the output. I have no idea what's wrong. Any and all help would be greatly appreciated.

3780
3680
3610
2970
2970
2400
2320
33      <-- This is the element that messes up, it should be 0
0
*** glibc detected *** /home/john/Desktop/Code::Blocks-Projects/EmpirePoints/bin/Debug/EmpirePoints: free(): invalid pointer: 0x08a03208 ***
======= Backtrace: =========
/lib/i386-linux-gnu/libc.so.6(+0x75ee2)[0xb74a9ee2]
/usr/lib/i386-linux-gnu/libstdc++.so.6(_ZdlPv+0x1f)[0xb7645adf]
/usr/lib/i386-linux-gnu/libstdc++.so.6(_ZNSs4_Rep10_M_destroyERKSaIcE+0x1b)[0xb76ad7bb]
/usr/lib/i386-linux-gnu/libstdc++.so.6(+0xb0800)[0xb76ad800]
/usr/lib/i386-linux-gnu/libstdc++.so.6(_ZNSsD1Ev+0x2e)[0xb76ad86e]
/home/john/Desktop/Code::Blocks-Projects/EmpirePoints/bin/Debug/EmpirePoints[0x8049f28]
/home/john/Desktop/Code::Blocks-Projects/EmpirePoints/bin/Debug/EmpirePoints[0x804ac03]
/lib/i386-linux-gnu/libc.so.6(__libc_start_main+0xf3)[0xb744d4d3]
/home/john/Desktop/Code::Blocks-Projects/EmpirePoints/bin/Debug/EmpirePoints[0x8048fa1]
======= Memory map: ========
08048000-0804c000 r-xp 00000000 08:01 4456478    /home/john/Desktop/Code::Blocks-Projects/EmpirePoints/bin/Debug/EmpirePoints
0804c000-0804d000 r--p 00003000 08:01 4456478    /home/john/Desktop/Code::Blocks-Projects/EmpirePoints/bin/Debug/EmpirePoints
0804d000-0804e000 rw-p 00004000 08:01 4456478    /home/john/Desktop/Code::Blocks-Projects/EmpirePoints/bin/Debug/EmpirePoints
08a01000-08a22000 rw-p 00000000 00:00 0          [heap]
b7406000-b7408000 rw-p 00000000 00:00 0 
b7408000-b7432000 r-xp 00000000 08:01 5643939    /lib/i386-linux-gnu/libm-2.15.so
b7432000-b7433000 r--p 00029000 08:01 5643939    /lib/i386-linux-gnu/libm-2.15.so
b7433000-b7434000 rw-p 0002a000 08:01 5643939    /lib/i386-linux-gnu/libm-2.15.so
b7434000-b75d7000 r-xp 00000000 08:01 5643913    /lib/i386-linux-gnu/libc-2.15.so
b75d7000-b75d8000 ---p 001a3000 08:01 5643913    /lib/i386-linux-gnu/libc-2.15.so
b75d8000-b75da000 r--p 001a3000 08:01 5643913    /lib/i386-linux-gnu/libc-2.15.so
b75da000-b75db000 rw-p 001a5000 08:01 5643913    /lib/i386-linux-gnu/libc-2.15.so
b75db000-b75df000 rw-p 00000000 00:00 0 
b75df000-b75fb000 r-xp 00000000 08:01 5636815    /lib/i386-linux-gnu/libgcc_s.so.1
b75fb000-b75fc000 r--p 0001b000 08:01 5636815    /lib/i386-linux-gnu/libgcc_s.so.1
b75fc000-b75fd000 rw-p 0001c000 08:01 5636815    /lib/i386-linux-gnu/libgcc_s.so.1
b75fd000-b76d9000 r-xp 00000000 08:01 4855068    /usr/lib/i386-linux-gnu/libstdc++.so.6.0.17
b76d9000-b76da000 ---p 000dc000 08:01 4855068    /usr/lib/i386-linux-gnu/libstdc++.so.6.0.17
b76da000-b76de000 r--p 000dc000 08:01 4855068    /usr/lib/i386-linux-gnu/libstdc++.so.6.0.17
b76de000-b76df000 rw-p 000e0000 08:01 4855068    /usr/lib/i386-linux-gnu/libstdc++.so.6.0.17
b76df000-b76e6000 rw-p 00000000 00:00 0 
b76f8000-b76fd000 rw-p 00000000 00:00 0 
b76fd000-b76fe000 r-xp 00000000 00:00 0          [vdso]
b76fe000-b771e000 r-xp 00000000 08:01 5643942    /lib/i386-linux-gnu/ld-2.15.so
b771e000-b771f000 r--p 0001f000 08:01 5643942    /lib/i386-linux-gnu/ld-2.15.so
b771f000-b7720000 rw-p 00020000 08:01 5643942    /lib/i386-linux-gnu/ld-2.15.so
bff27000-bff48000 rw-p 00000000 00:00 0          [stack]
Aborted (core dumped)

Process returned 134 (0x86)   execution time : 10.694 s
Press ENTER to continue.

Hey guys, I was looking over my code and decided to try something to fix it and it worked. I changed int* pArray = new int [count]; to int pArray[count]. Does anyone know why that would cause such a big error? It also stopped the string sorting from causing a Segmentation Fault.

Edited 3 Years Ago by Sci3nc3F1cti0n

That doesn't fix your problem it hides it. The problem is in your for loop at lines 23, 28, 29, 31 and 32 where you access pArray[s + 1] and names[s + 1]. Since the loop is written for(int s = 0; s < count; s++) in the final iteration s has the value count-1 therefore (s + 1) == count and that means you are accessing the arrays pArray and names outside there bounds which is what is causing your errors.

What you did hide the problem by moving the location in memory used to store pArray, since an out of bounds array access produces undefined behaviour just because the program started working doesn't mean you fixed the problem, just that it no longer showed up as an invalid address access.

When using <LoopCounter>+1 to access arrays you must always be careful that you adjust your loop end condition accordingly so that you do not end up with out of bound array accesses.

BTW you could make your sort more efficient, one of the features of bubble sort is that after each iteration the top (or in your case bottom) item is now correct and no longer needs checking, that means that for each interation of the outer loop of the sort the inner loop can be reduced by 1, your inner loop could be written as for(int s = 0; s < (count - (i + 1)); s++) which would improve the efficiency of the sort and fix the out of bounds array issue.

Oh wow, thank you! Haha, the bad part is that I knew it just hid it, but I just couldn't seem to find the real problem. I didn't even think about [s + 1] going out of bounds. Also, thanks for the efficiency improvement, I didn't think about that either.

Steadily working my way up to more complicated projects really gets me excited about what's to come after I have a better understanding of the language.

This question has already been answered. Start a new discussion instead.