String pointers

Pages: 12
Why can't I delete them with
 
delete [] s; break;


when allocated with:
 
s = new string [maximumsizeofarrays];
That is fine. There must be something else.
Nope, I can delete s; but not delete [] s;.
Zhuge wrote:
That is fine. There must be something else.
What does the c++ standard say about rain? It's been raining a lot here for the past two weeks. Maybe that has something to do with it, ya think? Maybe if I wait until the monsoon season is over, I'll hereby be complying with the standard and my code will compile.
Last edited on
Yes, undefined behavior can cause rain to affect your program adversely.

Maybe if I wait until the monsoon season is over, I'll hereby be complying with the standard and my code will compile.

Are you saying it does not compile right now?
And fixing the error(s) is a far better idea than waiting for it to stop raining.

Last edited on
wtf wrote:
Nope, I can delete s; but not delete [] s;
¿what do you mean? crash/don't compile
No, what I meant to say was maybe then i wouldn't get stuck in the rut of 'undefined behavior'.
My program does compile, it just crashes when I try to free memory allocated for a string pointer.
Here is my code: (Just a diversion for me until it stops raining)
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
#ifndef TYPE_H
#define TYPE_H

using namespace std;

#define alphabetate( name ) # name

enum KINDS { INT = 1, BOOL, FLOAT, DOUBLE, CHAR, STRING, CHARPOINTER };
const char * kindnames[] = {"", alphabetate(INT), alphabetate(BOOL), alphabetate(FLOAT), alphabetate(DOUBLE), alphabetate(CHAR),
    alphabetate(STRING), alphabetate(CHARPOINTER)};

#include <ostream>
#include <iostream>

/*ostream ostreamify_kinds_wrapper(KINDS kin)

{
    ostream oz(cout);
    oz << kindnames[kin];
    oz << flush;
    //oz << kin << endl;
    //return oz;
}

ostream ostreamify_kinds(int kin)
{
    ostreamify_kinds_wrapper(oz, (KINDS)kin);
    //return oz;
}
*/


string printify_kinds_wrapper(KINDS KIN)
{
    return kindnames[KIN];
}


string printify_kinds(int kin)
{
    return printify_kinds_wrapper((KINDS) kin);
}

template <class it> int get_type(const it & other) {  return -1;}
int get_type(const string & s) { return STRING;}
int get_type(const char & c) { return CHAR; }
int get_type(const double & d) {return DOUBLE; }
int get_type(const float & f) { return FLOAT; }
int get_type(const bool & b) { return BOOL; }
int get_type(const int & i) { return INT; }
int get_type(const char * & cp) { return CHARPOINTER;}
int get_type(char * cp) { return CHARPOINTER; }


#endif
Here is compound.h

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
#ifndef COMPOUND_H
#define COMPOUND_H
#include <string>
#include <sstream>
#include "type.h"
using namespace std;
int cot(int a, int b);
int num_of_digits(const int & num);
const int maximumsizeofarrays = 10000;
class compound
{
    private:
    bool what[8]; //0 is for other //i should take out charpointer from enum I think.  //maybe not now since i've added char ** cp;
    //char *
    public://july 12
    char * c; int * i; float * f; double * d; bool * b; string * s; char * * cp;
    string data;
    public:int size;
    private:int * index; //for example [0] = FLOAT; [1] = CHAR; [2] = INT; [3] = FLOAT ETC;

    public:

    compound()
    {
        i = new int [maximumsizeofarrays]; c = new char [maximumsizeofarrays]; b = new bool [maximumsizeofarrays];
        d = new double[maximumsizeofarrays]; f = new float[maximumsizeofarrays]; s = new string [maximumsizeofarrays];
        cp = new char * [maximumsizeofarrays]; index = new int [maximumsizeofarrays];
        for (int j = 0; j < maximumsizeofarrays; ++j)
            {
                i[j] = 0;
                c[j] = 0;
                b[j] = 0;
                d[j] = 0;
                f[j] = 0;
                s[j] = "";
                cp[j] = 0;
                index[j] = 0;
            }
    }
    compound(string & str)
    {
        i = new int [maximumsizeofarrays]; c = new char [maximumsizeofarrays]; b = new bool [maximumsizeofarrays];
        d = new double[maximumsizeofarrays]; f = new float[maximumsizeofarrays];
        s = new string [maximumsizeofarrays]; //changed from new string
        cp = new char * [maximumsizeofarrays]; index = new int [maximumsizeofarrays];

        for (int j = 0; j < maximumsizeofarrays; ++j)
            {
                i[j] = 0;
                c[j] = 0;
                b[j] = 0;
                d[j] = 0;
                f[j] = 0;
                s[j] = "";
                cp[j] = 0;
                index[j] = 0;
            }
        data = str; convert();
    }

    ~compound()
    {
        int kize = kinds();
        if (!size)
            return;
        for (int j = 1; j < 8; ++j)
            {
                if (what[j])
                    {
                        what[j] = 0;
                        cout << "here j = " << j << endl;
                        switch(index[j-1])
                            {
                                case INT: delete [] i; break;
                                case FLOAT: delete [] f; break;
                                case DOUBLE: delete [] d; break;
                                case BOOL: delete [] b; break;
                                case STRING: delete [] s; break;

                                /*case STRING: //break; //july 13
                                    for (int j = 0; j < maximumsizeofarrays; ++j)
                                        {
                                            //if (s[j] != "")
                                                {
                                                    //delete [] *s[j][0];
                                                    //delete [] s;
                                                    //delete s[j];
                                                    //delete s[];
                                                    if (*s != "")
                                                        exit(12345);

                                                }
                                        }
                                    break;*/

                                case CHAR: delete [] c; break;
                                case CHARPOINTER: delete [] cp; break;
                                default:
                                break;
                            }
                    }
            }
    }

    int kinds(const bool doanyway = 0)
    {
        int ic = 0, bc = 0, fc = 0, dc = 0, cc = 0, cpc = 0, sc = 0;
        for (int j = 0; j < size; ++j)
            {
                switch (index[j]) //;
                    {
                        case INT:
                            if (!ic || doanyway)
                                ++ic;
                            break;
                        case BOOL:
                            if (!bc || doanyway)
                                ++bc;
                            break;
                        case CHAR:
                            if (!cc || doanyway)
                                ++cc;
                            break;
                        case DOUBLE:
                            if (!dc || doanyway)
                                ++dc;
                            break;
                        case FLOAT:
                            if (!fc || doanyway)
                                ++fc;
                            break;
                        case STRING:
                            if (!sc || doanyway)
                                ++sc;
                            break;
                        case CHARPOINTER:
                            if (!cpc || doanyway)
                                ++cpc;
                            break;
                        default: break;
                    }
            }
        return ic + bc + fc + dc + cc + cpc + sc;
    }

    int getsize()
    { int rv = kinds(1); return rv; }

    void clearall(){}
compound.h continued:
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
bool convert() //string 2 compound.
    {
        int testn; int chunks; bool posneg = 0;  //probably don't need these.
        string ts = data; stringstream river(ts);
        clearall(); string temps = ""; char character; int strsize = data.size();

        for (int j = 0; j < strsize; ++j)
            {
                int tincrement = 0;
                if (isalpha(ts[j]))
                    {
                        //we have a string of letters
                        //string ts = ts[j];
                        //temps += ts[j];
                        temps = ts[j];
                        int k = j + 1;
                        while(isalpha(ts[k]))
                            {
                                temps += ts[k];
                                ++k;
                            }
                        s[firstemptys()] = temps;
                        ++size;
                        index[firstemptyindex()] = STRING;
                        j = k - 1;
                        continue;
                        //j = k;

                        /*
                        //cout << "ts[j] = " << ts[j] << endl;
                        c[firstemptyc()] = ts[j];
                        ++size;
                        index[firstemptyindex()] = CHAR;
                        continue;
                        */
                    }
                if (!isalnum(ts[j]))
                    {
                        //cout << "ts[j] = " << ts[j] << endl;

                        if (ts[j] == '-')
                            {
                                if (isdigit(ts[j+1]))
                                    {
                                        //we have either an int, double, or float
                                        int tn; int k = j + 2; tn = ts[j+1] - 48;
                                        //while(isdigit(ts[k] +48))
                                        while(isdigit(ts[k]))
                                            {
                                                int tn2 = ts[k] - 48;
                                                tn = cot(tn, tn2);
                                                ++k;
                                            }
                                        //if (ts[k+1] == '.' && isdigit(ts[k+2]) )
                                        if (ts[k] == '.' && isdigit(ts[k+1]))
                                            {
                                                //we have a negative float or a double
                                                int l = k+1;
                                                int divisor = 10;
                                                //double d = ts[l] - 48;
                                                int d1 = ts[l] - 48;
                                                double rv = 0;
                                                while(isdigit(ts[l+1]))
                                                    {
                                                        //double d2 = ts[l+1] - 48;
                                                        int d2 = ts[l+1] - 48;
                                                        //d2 /= divisor;
                                                        ++l; divisor *= 10;
                                                        //double tcv = cot(d, d2);
                                                        rv = cot(d1, d2);
                                                        d1 = cot(d1, d2);
                                                        cout << "rv = " << rv << endl;

                                                    }
                                                cout << "rv = " << rv << ".\n" << flush;
                                                //exit(45);
                                                rv /= divisor;
                                                cout << "now rv = " << rv << endl;
                                                //exit(46);


                                                double tndouble = tn + rv;
                                                tndouble *= -1;
                                                cout << "tndouble = " << tndouble << endl;
                                                //exit(47);
                                                d[firstemptyd()] = tndouble;
                                                ++size;
                                                index[firstemptyindex()] = DOUBLE;
                                                j = l - 1;
                                                continue;


                                            }
                                        else
                                            {
                                                //we have an int
                                                tn *= -1;
                                                i[firstemptyi()] = tn;
                                                ++size;
                                                index[firstemptyindex()] = INT;
                                                j = k - 1;
                                                continue;
                                            }
                                    }
                            }
                        {//we have a char.
                            c[firstemptyc()] = ts[j];
                            ++size;
                            index[firstemptyindex()] = CHAR;
                            continue;
                        }
                    }





                else if (isdigit(ts[j]))
                    {
                        //we have a positive number
                        //repeat like above.
                        int tn; int k = j + 1; tn = ts[j] - 48;
                        cout << "int tn = " << tn << ".\n" << flush << endl;
                        while(isdigit(ts[k]))
                            {
                                int tn2 = ts[k] - 48;
                                tn = cot(tn, tn2);
                                ++k;
                            }
                        if (ts[k] == '.' && isdigit(ts[k+1]))
                            {//we have a positive float or double
                            }
                        else
                            {
                                i[firstemptyi()] = tn;
                                ++size;
                                index[firstemptyindex()] = INT;
                                //j = k + 1;
                                j = k - 1;
                                continue;
                            }
                    }
                else
                    {

                        //its a char
                        //c[firstemptychp()] = ts[j];
                        //why not working
                        //c[0] = ts[j];


                        //c = ts[j];

                        c[firstemptyc()] = ts[j];

                        ++size; index[firstemptyindex()] = INT;
                        continue;
                    }

            }
    }
compound.h part 3:
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
   void print()
    {
        int ci = 0, ii = 0, fi = 0, di = 0, bi = 0, si = 0, cpi = 0;
        for (int j = 0; j < size; ++j)
            {
                switch(index[j])
                    {
                        case INT: printf("%d", i[ii++]);
                        break;

                        case FLOAT: printf("%f", f[fi++]);
                        break;

                        case CHAR: printf("%c", c[ci++]);
                        break;

                        case STRING: //exit(144);
                        //cout << "s[si].c_str() = " << s[si].c_str() << flush << endl << endl << flush << endl << flush;
                        printf("%s", s[si++].c_str());
                        break;

                        case CHARPOINTER: printf("%c", cp[cpi++]);
                        break;

                        case BOOL: printf("%d", b[bi++]);
                        break;

                        case DOUBLE: printf("f", d[di++]);
                        break;
                    }
            }
        return;
    }

    template<class it>
    void print_(it item)
    {
    }

    int firstemptyi()
    {
        int counter = 0;
        while(!i[counter] == 0)
            counter++;
        return counter;
    }

    int firstemptyindex()
    {
        int counter = 0;
        while(!index[counter] == 0)
            {
                //counter += counter;
                counter++;
            }
        return counter;
    }

    int firstemptychp()
    {
        int counter = 0;
        while(!cp[counter] == 0)
            ++counter;
        return counter;
    }

    int firstemptyc()
    {
        int counter = 0;
        while(!c[counter] == 0)
            ++counter;
        //cout << "counter = " << counter << endl;
        return counter;
    }

    int firstemptys()
        {
            int counter = 0;
            while(s[counter] != "")
                ++counter;
            return counter;
        }

    int firstemptyd()
        {
            int counter = 0;
            while(!d[counter] == 0)
                ++counter;
            return counter;
        }
};

#endif

int cot(int a, int b)
{
    int newnum = 0;
    int ap = num_of_digits(a), bp = num_of_digits(b);
    int j = 1;
    newnum = b; int fp = a; int multiplier = 1;
    for (int i = 0; i < bp; ++i)
        {
            multiplier = 10 * multiplier;
        }
    newnum = (multiplier * a) + b;
    return newnum;
}

int num_of_digits(const int & num)
{
    int n = (unsigned int) num; //or ABS
    int t = 10;
    int rv = 1;
    if (n < 10 && n >= 0)
        return 1;
    if (n < 0)
        n *= -1;
    while(t < n)
        if ( (n / t) > 0)
            {
                ++rv;
                t *= 10;
            }
    if (n % 10 == 0)
        ++rv;
    return rv;
}
my main program:
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
#include <iomanip>

#include "type.h"
//#define stringify( name ) # name

#include <iostream>

#include <sstream>

#include "compound.h"

using namespace std;

int main()
{
    cout.precision(10);
    cout << "This is a tester. \n" << flush << endl << flush;
    string hi = "He3llo W-245.1987orld!\n";
    compound interest(hi);
    //compound interest;
    //interest.i[0] = 4;
    cout << "interest.firstemptyi() = " << interest.firstemptyi() << ".\n" << flush;
    cout << "interest.c = " << interest.c << endl;
    cout << "interest.i[1] = " << interest.i[1] << "." << endl;
    cout << "interest.data = " << interest.data << ". \n" << flush << endl;
    cout << "interest.s[0] = " << interest.s[0] << ".\n" << flush << endl;
    cout << "interest.d[0] = " << interest.d[0] << ".\n" << flush << endl;

    cout << "\n\n" << flush;
    cout << "Printing... " << endl << endl << endl;
    interest.print();



    cout << "\nLooping. \n"<< flush << endl;
    //why o why can't I delete a string pointer in ~compound?
    begin:
    //goto begin;
    interest.~compound();
    return 0;
}
http://paste.pocoo.org/ would have been nice in the future. (Bookmark it! It's better than pastebin!)
Could you at least tell us which function(s) wee need to be looking at for starters?
Sorry I can't bookmark, I'm on a shared computer.

case STRING: delete [] s; break;
in the compound destructor.
This is definitely an error: interest.~compound();

That aside, I'm not exactly sure what the compound class is for, but I highly doubt its usefulness - you can probably provide a functional equivalent with just a few lines of code. And the convert function is probably made obsolete by lexical_cast.
Well it compiles, and it was until i found the error causing me to crash, crashing without the call to the destructor, even when commenting out the delete [] s; line.

What i'm doing is just as an excercise really, maybe something usefull will come out of it, maybe not.

And tell me more about lexical cast?

And btw, I think i found the error, it was in bool what[8] not being initialized to false; ....

edit:
Besides I've always read and i strongly concur that its important to be able to, or at least know how to do many of the things that can be done with libraries. Plus I believe what I'm doing is called parsing, is it not? and that is supposedly one of the harder things to do, so I think that it makes for a pretty good excercize, even if I can't do anything with it when I'm done.
Last edited on
lexical_cast is part of boost and allows you to convert different types, e.g.:
lexical_cast<string>(3.14) and lexical_cast<double>("3.14")
It's far more convenient than using a stringstream (and much faster too).

Plus I believe what I'm doing is called parsing, is it not? and that is supposedly one of the harder things to do, so I think that it makes for a pretty good excercize

Yeah, I suppose it is.
Can I use lexical_cast<int, bool, float, double, char, string, char *>("He3llo w-245.1987orld!")
to cast that string into an int pointer = 3, a double pointer = -245.1987 a string * = "Hello" another string * = "world" and a char * for the space and the !?

No, you can only lexical_cast a type into one other type. If you want to do that (IMO strange) separation stuff you can separate it out then use lexical_cast.
Pages: 12