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.