[ERROR] Pointer Errors

I'm creating a C++ program with g++ and QT Creator. Here's my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#include <iostream>
#include <time.h>
#include <stdlib.h>
#include "ArrayList.h"
#include "ListExceptions.h"

using namespace std;

static const int X[] = {10000, 15000, 20000, 25000, 30000, 35000, 40000, 45000, 50000};
static const char chars[] = "1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM";

ArrayList<char> getRand(const char c[], int l)
{
    ArrayList<char> trn = ArrayList<char>();

    for(int i = 0; i < l; i++)
    {
        trn += c[rand()%62];
    }

    return trn;
}

char getMin(ArrayList<ArrayList<char> > c, int j, int l)
{
    char min = c[0][0];

    for(int i = 0; i < l; i++)
    {
        if(min > c[i][j])
            min = c[i][j];
    }
    return min;
}

/* Merge-Sort Functions */

ArrayList<char>* merge(ArrayList<char>* f, ArrayList<char>* l, int s)
{
    ArrayList<char>* n = new ArrayList<char>[s]();

    for(int i = 0; i < s; i += 2)
    {
        if(f[i] > l[i])
        {
            n[i] = l[i];
            n[i+1] = f[i];
        }
        else
        {
            n[i] = f[i];
            n[i+1] = l[i];
        }
    }

    return n;
}
ArrayList<char>* mgsort(ArrayList<char>* c, int f, int l)
{
    int s = l - f;
    int half = s/2;
    int ind = 0;

    ArrayList<char>* fst = new ArrayList<char>[half]();
    ArrayList<char>* lst = new ArrayList<char>[s-half]();

    while(ind < half)
    {
        fst[ind] = c[ind];
    }
    while(ind < s)
    {
        lst[ind] = c[ind];
    }

    delete[] c;
    return merge(mgsort(fst, 0, half), mgsort(lst, half+1, s), s);
}
void msort(ArrayList<char>* c, int l)
{
    int half = l/2;
    int ind = 0;

    ArrayList<char>* fst = new ArrayList<char>[half]();
    ArrayList<char>* lst = new ArrayList<char>[l-half]();

    while(ind < half)
    {
        fst[ind] = c[ind];
        ++ind;
    }
    while(ind < l)
    {
        lst[ind] = c[ind];
        ++ind;
    }

    c = merge(mgsort(fst, 0, half), mgsort(lst, half+1, l), l);
}

/* Radix-Sort Functions */

void rsort(ArrayList<ArrayList<char> > c, int p, int l)
{
    char m;
    ArrayList<ArrayList<ArrayList<char> > > t = ArrayList<ArrayList<ArrayList<char> > >();
    int d = 0;

    while(!c.isEmpty())
    {
        m = getMin(c, p, l);

        for(int i = 1; i < l; i++)
        {
            if(c[i][p] == m)
            {
                t[d].append(c[i]);
                c.remove(c[i]);
            }
        }
        ++d;
    }

    for(int i = 0; i < t.getSize(); i++)
    {
        rsort(t[i], p+1, t[i].getSize());
    }
}

/* Main */

int main()
{
    ArrayList<char>* mstrings[9];
    clock_t mtimers[9];
    clock_t rtimers[9];

    cout << "Data structures established." << endl;

    for(int i = 0; i < 9; i++)
    {
        for(int j = 0; j < X[i]; j++)
            mstrings[i][j] = getRand(chars, rand()%10); //Error Fixed: Now initialized.
    }

    cout << "Strings for merge-sorting established." << endl;

    ArrayList<ArrayList<char> > rstrings[9];

    for(int i = 0; i < 9; i++)
        rstrings[i] = ArrayList<ArrayList<char> >(mstrings[i]);

    cout << "Data from mstrings copied to strings for radix sorting." << endl;

    for(int i = 0; i < 9; i++)
    {
        mtimers[i] = clock();
        msort(mstrings[i], X[i]);
        mtimers[i] = clock() - mtimers[i];
    }

    cout << "Merge-sort complete." << endl;

    for(int i = 0; i < 9; i++)
    {
        rtimers[i] = clock();
        rsort(rstrings[i], 0, X[i]);
        rtimers[i] = clock() - rtimers[i];
    }

    cout << "Radix-sort complete." << endl;

    double mtimersF = (double)mtimers[0];
    double rtimersF = (double)rtimers[0];

    for(int i = 1; i < 9; i++)
    {
        mtimersF += (double)mtimers[i];
        rtimersF += (double)rtimers[i];
    }

    cout << "Sorting times established." << endl;

    cout << "Merge Sort: " << (mtimersF * 1000.0) / (9.0 * CLOCKS_PER_SEC) << endl;
    cout << "Radix Sort: " << (rtimersF * 1000.0) / (9.0 * CLOCKS_PER_SEC) << endl;

    return 0;
}



*** Error in `./P5-Linux/P5': double free or corruption (out): 0x00007f11bdc4c650 ***
======= Backtrace: =========
/lib64/libc.so.6(+0x77d9e)[0x7f11bd0f3d9e]
/lib64/libc.so.6(cfree+0x5b5)[0x7f11bd0ff9f5]
./P5-Linux/P5[0x4021b9]
./P5-Linux/P5[0x4022fc]
./P5-Linux/P5[0x400d2a]
./P5-Linux/P5[0x401aa3]
/lib64/libc.so.6(__libc_start_main+0xf0)[0x7f11bd09bfe0]
./P5-Linux/P5[0x400bc9]
======= Memory map: ========
00400000-00405000 r-xp 00000000 fd:03 18093757                           /home/gcarlton/Qt/P5-Linux/P5
00604000-00605000 r--p 00004000 fd:03 18093757                           /home/gcarlton/Qt/P5-Linux/P5
00605000-00606000 rw-p 00005000 fd:03 18093757                           /home/gcarlton/Qt/P5-Linux/P5
00d43000-00d64000 rw-p 00000000 00:00 0                                  [heap]
7f11bd07c000-7f11bd22f000 r-xp 00000000 fd:02 3153277                    /usr/lib64/libc-2.20.so
7f11bd22f000-7f11bd42f000 ---p 001b3000 fd:02 3153277                    /usr/lib64/libc-2.20.so
7f11bd42f000-7f11bd433000 r--p 001b3000 fd:02 3153277                    /usr/lib64/libc-2.20.so
7f11bd433000-7f11bd435000 rw-p 001b7000 fd:02 3153277                    /usr/lib64/libc-2.20.so
7f11bd435000-7f11bd439000 rw-p 00000000 00:00 0 
7f11bd439000-7f11bd44f000 r-xp 00000000 fd:02 3147282                    /usr/lib64/libgcc_s-4.9.2-20150212.so.1
7f11bd44f000-7f11bd64e000 ---p 00016000 fd:02 3147282                    /usr/lib64/libgcc_s-4.9.2-20150212.so.1
7f11bd64e000-7f11bd64f000 r--p 00015000 fd:02 3147282                    /usr/lib64/libgcc_s-4.9.2-20150212.so.1
7f11bd64f000-7f11bd650000 rw-p 00016000 fd:02 3147282                    /usr/lib64/libgcc_s-4.9.2-20150212.so.1
7f11bd650000-7f11bd757000 r-xp 00000000 fd:02 3153305                    /usr/lib64/libm-2.20.so
7f11bd757000-7f11bd956000 ---p 00107000 fd:02 3153305                    /usr/lib64/libm-2.20.so
7f11bd956000-7f11bd957000 r--p 00106000 fd:02 3153305                    /usr/lib64/libm-2.20.so
7f11bd957000-7f11bd958000 rw-p 00107000 fd:02 3153305                    /usr/lib64/libm-2.20.so
7f11bd958000-7f11bda48000 r-xp 00000000 fd:02 3153421                    /usr/lib64/libstdc++.so.6.0.20
7f11bda48000-7f11bdc48000 ---p 000f0000 fd:02 3153421                    /usr/lib64/libstdc++.so.6.0.20
7f11bdc48000-7f11bdc50000 r--p 000f0000 fd:02 3153421                    /usr/lib64/libstdc++.so.6.0.20
7f11bdc50000-7f11bdc52000 rw-p 000f8000 fd:02 3153421                    /usr/lib64/libstdc++.so.6.0.20
7f11bdc52000-7f11bdc67000 rw-p 00000000 00:00 0 
7f11bdc67000-7f11bdc88000 r-xp 00000000 fd:02 3153196                    /usr/lib64/ld-2.20.so
7f11bde66000-7f11bde6b000 rw-p 00000000 00:00 0 
7f11bde85000-7f11bde88000 rw-p 00000000 00:00 0 
7f11bde88000-7f11bde89000 r--p 00021000 fd:02 3153196                    /usr/lib64/ld-2.20.so
7f11bde89000-7f11bde8a000 rw-p 00022000 fd:02 3153196                    /usr/lib64/ld-2.20.so
7f11bde8a000-7f11bde8b000 rw-p 00000000 00:00 0 
7ffc979de000-7ffc979ff000 rw-p 00000000 00:00 0                          [stack]
7ffc97b19000-7ffc97b1b000 r--p 00000000 00:00 0                          [vvar]
7ffc97b1b000-7ffc97b1d000 r-xp 00000000 00:00 0                          [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0                  [vsyscall]
Aborted (core dumped)


As far as I can tell, it got as far as mstrings[i][j] = getRand(chars, rand()%10); before aborting. What can you tell me?
Last edited on
> As far as I can tell, it got as far as mstrings[i][j] = getRand(chars, rand()%10); before aborting
¿how did you figure that out?


> ArrayList<char>* mstrings[9];
¿what the hell is that? ¿you couldn't decide on a container?


1
2
3
4
5
    for(int i = 0; i < 9; i++)
    {
        for(int j = 0; j < X[i]; j++) //X = {10000, 15000, 20000, 25000, 30000, 35000, 40000, 45000, 50000};
            mstrings[i][j] = getRand(chars, rand()%10);
    }
mstrings[i] is a pointer that you never initialised, then you try to dereference that invalid pointer and 50e3 more positions next to it.
¿what the hell is that? ¿you couldn't decide on a container?

It's a very odd 3-dimentional array. A static array of dynamic arrays of home-made vectors.

Also, now I'm past that point in my code. Now I'm at a segfault -_-. From what my debugger is telling me, an ArrayList in mstrings is trying to delete data it can't access.
Last edited on
It's a very odd 3-dimentional array. A static array of dynamic arrays of home-made vectors.

Where are the "dynamic arrays" that your pointers point to?
Topic archived. No new replies allowed.