Memory leak problem

I made own string and I have memory leak with adding operator...
I know why but I don't know how to solve this without non temporary classes

This adding like strings's adding so it make a temporary string but in mine if I make temporary that goes delete and no results

NOTE: I only use C++ 98

(Please if you can't answer don't take "blah blah" comment)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//main
#include<cstdio>

#include<ala_text.h>



int main(){
    while(true){
        tale str="hue ";
        tale stn="stn ";
            str+=stn+"hue";
    };

    /*
    puts(stn.gets());
    puts(str.gets());

    gets((new char[1]));
    */

    return 0;
};


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
//header
#ifndef ALA_TEXT_H
#define ALA_TEXT_H

#include<cstddef>
#include<cstring>
#include<cstdlib>
#include<sstream>

typedef char                s_char;
typedef const char          cs_char;
typedef const size_t        size_tc;



template<typename unkwn>
cs_char*_to_string(unkwn item){
    std::stringstream ss;
        ss<<std::fixed<<item;
    std::string str=ss.str();
    return str.c_str();
};



class tale{
    private:
        size_t           _length;
        s_char*          _buffer;

    public:
        //method
        bool empty(){
            return _length==0;
        };

        size_tc length(){
            return _length;
        };

        cs_char*gets(){
            return(cs_char*)_buffer;
        };

        void clear(){
            if(_length<1)return;
            _length=0;
            delete[]_buffer;
        };

        //operator
        template<typename unkwn>
        tale&operator+(unkwn item){
            tale*res=new tale[1];
                *res+=*this;
                *res+=_to_string(item);
            return*res;
        };

        tale&operator+(tale&item){
            tale*res=new tale[1];
                *res+=*this;
                *res+=item;
            return*res;
        };

        tale&operator+(cs_char*item){
            tale*res=new tale[1];
                *res+=*this;
                *res+=item;
            return*res;
        };

        tale&operator+(cs_char&item){
            tale*res=new tale[1];
                *res+=*this;
                *res+=item;
            return*res;
        };

        tale&operator+=(tale&text){
            size_tc len=text.length();

            _buffer=(s_char*)realloc(_buffer,_length+len+1);
                memcpy(&_buffer[_length],text.gets(),len);
                _buffer[_length+=len]=0;

            return*this;
        };

        tale&operator+=(cs_char*text){
            size_tc len=strlen(text);

            _buffer=(s_char*)realloc(_buffer,_length+len+1);
                memcpy(&_buffer[_length],text,len);
                _buffer[_length+=len]=0;

            return*this;
        };

        tale&operator+=(cs_char&chr){
            _buffer=(s_char*)realloc(_buffer,_length+2);
                _buffer[_length]=chr;
                _buffer[_length+=1]=0;
            return*this;
        };

        tale&operator=(tale&text){
            delete[]_buffer;
                _length=text.length();
                _buffer=new s_char[_length];
                memcpy(_buffer,text.gets(),_length);
                _buffer[_length]=0;

            return*this;
        };

        tale&operator=(cs_char*text){
            delete[]_buffer;
                _length=strlen(text);
                _buffer=new s_char[_length];
                memcpy(_buffer,text,_length);
                _buffer[_length]=0;
            return*this;
        };

        tale&operator=(cs_char&chr){
            delete[]_buffer;
                _length=1;
                _buffer=new s_char[_length];
                _buffer[0]=chr;
                _buffer[_length]=0;
            return*this;
        };

        //constructor
        tale(cs_char*text){
            _length=strlen(text);
            _buffer=new s_char[_length];
            memcpy(_buffer,text,_length);
            _buffer[_length]=0;
        };

        tale(cs_char&chr){
            _length=1;
            _buffer=new s_char[_length];
            _buffer[0]=chr;
            _buffer[_length]=0;
        };

        tale(){
            _length=0;
            _buffer=new s_char[_length];
            _buffer[0]=0;
        };

        //destructor
        ~tale(){
            delete[]_buffer;
        };
};

#endif
Last edited on
Why are you allocating the tale objects using new? Just declare a local object and return a copy.
1
2
3
4
5
6
7
template<typename unkwn>
tale operator+(unkwn item){
	tale res;
	res += *this;
	res += _to_string(item);
	return res;
};


Wouldn't that work?

Make sure to implement copy constructor and copy assignment operator to make copying tale objects work correctly. See the rule of three. http://en.wikipedia.org/wiki/Rule_of_three_%28C%2B%2B_programming%29

I do because if I do this without clones

1
2
3
4
5
6
7
8
9

tale a="tale1 ";
tale b="tale2 ";

a=b+a;

puts(a.gets());// tale1 take2 take2 tale1
puts(b.gets());// tale2 tale1


or something like this

It's not good if temporary clones stay until the original not gets remove

I think I need to make a system for this with a default value what get check and if that true that is a temporary then that gets remove
weird doesn't work

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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#ifndef ALA_TEXT_H
#define ALA_TEXT_H

#include<cstddef>
#include<cstring>
#include<cstdlib>
#include<sstream>
#include<algorithm>

typedef char                s_char;
typedef const char          cs_char;
typedef const size_t        size_tc;



template<typename unkwn>
cs_char*_to_string(unkwn item){
    std::stringstream ss;
        ss<<std::fixed<<item;
    std::string str=ss.str();
    return str.c_str();
};



template<class unkwn,unkwn def>
struct def_value{
    unkwn value;

    def_value():value(def){};
    def_value(unkwn v):value(v){};
    operator def_value()const{return value;};
};

class tale{
    private:
        size_t           _length;
        s_char*          _buffer;

    public:
        //properties
        def_value<bool,false>_temporary;

        //method
        bool empty(){
            return _length==0;
        };

        size_tc length(){
            return _length;
        };

        cs_char*gets(){
            return(cs_char*)_buffer;
        };

        void clear(){
            if(_length<1)return;
            _length=0;
            delete[]_buffer;
        };

        //operator
        template<typename unkwn>
        tale&operator+(unkwn item){
            if(_temporary.value){
                *this+=_to_string(item);
                return*this;
            };

            tale*res=new tale[1];
                *res+=*this;
                *res+=_to_string(item);
                (*res)._temporary=true;
            return*res;
        };

        tale&operator+(tale&item){
            if(_temporary.value){
                *this+=item;

                if(item._temporary.value){
                    delete&item;
                };

                return*this;

            };

            tale*res=new tale[1];
                *res+=*this;
                *res+=item.gets();
                (*res)._temporary=true;

            if(item._temporary.value){
                delete&item;
            };

            return*res;
        };

        tale&operator+(cs_char*item){
            if(_temporary.value){
                *this+=_to_string(item);
                return*this;
            };

            tale*res=new tale[1];
                *res+=*this;
                *res+=item;
                (*res)._temporary=true;
            return*res;
        };

        tale&operator+(cs_char&item){
            if(_temporary.value){
                *this+=_to_string(item);
                return*this;
            };

            tale*res=new tale[1];
                *res+=*this;
                *res+=item;
                (*res)._temporary=true;
            return*res;
        };

        tale&operator+=(tale&item){
            size_tc len=item.length();

            _buffer=(s_char*)realloc(_buffer,_length+len+1);
                memcpy(&_buffer[_length],item.gets(),len);
                _buffer[_length+=len]=0;

            if(item._temporary.value){
                delete&item;
            };

            return*this;
        };

        tale&operator+=(cs_char*text){
            size_tc len=strlen(text);

            _buffer=(s_char*)realloc(_buffer,_length+len+1);
                memcpy(&_buffer[_length],text,len);
                _buffer[_length+=len]=0;

            return*this;
        };

        tale&operator+=(cs_char&chr){
            _buffer=(s_char*)realloc(_buffer,_length+2);
                _buffer[_length]=chr;
                _buffer[_length+=1]=0;
            return*this;
        };

        tale&operator=(tale&item){
            delete[]_buffer;
                _length=item.length();
                _buffer=new s_char[_length];
                memcpy(_buffer,item.gets(),_length);
                _buffer[_length]=0;

            if(item._temporary.value){
                delete&item;
            };

            return*this;
        };

        tale&operator=(cs_char*text){
            delete[]_buffer;
                _length=strlen(text);
                _buffer=new s_char[_length];
                memcpy(_buffer,text,_length);
                _buffer[_length]=0;
            return*this;
        };

        tale&operator=(cs_char&chr){
            delete[]_buffer;
                _length=1;
                _buffer=new s_char[_length];
                _buffer[0]=chr;
                _buffer[_length]=0;
            return*this;
        };

        //constructor
        tale(cs_char*text){
            _length=strlen(text);
            _buffer=new s_char[_length];
            memcpy(_buffer,text,_length);
            _buffer[_length]=0;
        };

        tale(cs_char&chr){
            _length=1;
            _buffer=new s_char[_length];
            _buffer[0]=chr;
            _buffer[_length]=0;
        };

        tale(){
            _length=0;
            _buffer=new s_char[_length];
            _buffer[0]=0;
        };

        //destructor
        ~tale(){
            delete[]_buffer;
        };
};

#endif
Last edited on
I don't understand. Is it line 5 you are worried about? b+a would produce a temporary tail object "tale1 tale2 " that's assigned to a, so puts(a.gets()); would print "tale1 tale2 ". b has not been modified so puts(b.gets()); would print "tale2 ".
Last edited on
I said this because if THERE ARE NO CLONES that would happen...

I want to remove cloned tales after addition
By "clones" you mean temporary objects? If you did what I showed all temporary objects would be removed automatically. No memory leaks.
I don't understand what you sent me also doesn't work

do you know other solution?

Last edited on
hmm thanks peter... I got to know how swap works l0

when I realloc the target getting delete

this is so good, yey!
Topic archived. No new replies allowed.