How to use delete and delete[]?

Hi, I'm having trouble using delete. In the third menu, I'm supposed to free the memory of iUserPtr or itPtr, but every time i do that, my program crashes.
malloc: *** error for object 0x100103b48: incorrect checksum for freed object - object was probably modified after being freed.
*** set a breakpoint in malloc_error_break to debug

I don't know how to use delete, but if I don't use it, it'll take a lot of memory.
Please also point out if you come across any mistake. Thank you very much.

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
#include <iostream>
using std :: cout;
using std :: cin;
using std :: endl;
using std :: ios;

#include <iomanip>
using std :: setprecision;

#include <cmath>

#include <cstdlib>

#include <string>
using std :: string;

void displayArray(int*, int, int);
double mean(int*, int);

int main()
{
    int allocated_size = 25;
	int current_size = 10;
	int re_allocated_size=0;
    
	int *iUserPtr=0; // for user_array
	int *iReallocatePtr=0; // for re-allocation array
	int *itPtr; // traversal pointers
    
	// to dynamic allocate the (user_array) iUserPtr
	iUserPtr = new int[allocated_size];
    
	// initialize the user array for the first time
	itPtr = iUserPtr; // use the traversal pointer
	for(int i=0; i<allocated_size-1; i++)
        *(itPtr+i) = 0;
    *(itPtr+(allocated_size-1)) = -1; // mark the last item
	for(int i=0; i<current_size; i++)
        *(itPtr+i) = allocated_size -i;
    for (int i=0; i<allocated_size; i++)
        *(iUserPtr+i)= *(itPtr+i);
    
	string user_input, buf1, buf2, buf3;
	int change, value;
    
	bool stay = true;
    while(stay) //as long as the user doesn't choose q, the program will continue to go back to menu
    {
        
		cout << " ----------- COMMAND MENU ------------"<<endl
        << " 1 - display user_array and its size\n"
        << " 2 - modify user_array content\n"
        << " 3 - modify user_array size\n"
        << " 4 - mean of user_array\n"
        << " q - quit\n\n"
        << " --> Enter your choice: ";
		cin >> user_input;
        cin.ignore();
        if(user_input.size() == 1)
        {
            char ch = user_input[0];
            
            // displaying user_array
            if (ch == '1')
            {
                displayArray(iUserPtr, current_size, allocated_size);
            }
            
            // modify the user_array[]
            if (ch == '2')
            {
                int location;
                cout << "Enter a location between 1 and "<<current_size<<": ";
                cin >> buf1; location=atoi(buf1.c_str());
                change=*(iUserPtr+(location-1));
                cout << "The current value is "<< change <<". What is the new value? ";
                cin >> buf2; value=atoi(buf2.c_str());
                if(location>0 && location <=current_size)
                    *(iUserPtr+(location-1)) = value;
                cout <<endl;
            }
            
            // modify the size of user_array[]
            if (ch == '3')
            {
				int requested_size;
				cout << "new working size? ";
				cin >> requested_size;
				cin.ignore();
                
				// case 1. need for memory grows beyond 80% allocated
				if(requested_size > allocated_size*8/10)
				{
					// allocate new 4X the old allocated
					re_allocated_size = allocated_size*4; //pattern to grow
					iReallocatePtr = new int[re_allocated_size];
                    
					cout << "The new allocated size is "<< re_allocated_size << endl;
                    
					// initialize new arrays to display work space
					itPtr = iReallocatePtr;
					for(int i=0; i<re_allocated_size; i++)
                        *itPtr++ = re_allocated_size -i;
					itPtr = iReallocatePtr;
					for(int i=0; i<re_allocated_size; i++)
                        cout << *itPtr++ << " ";
					cout << endl;
                    
					itPtr = iReallocatePtr;
					for(int i=0; i<re_allocated_size-1; i++)
                        *(itPtr+i)= 0;
					*(itPtr+(re_allocated_size-1)) = -1; // mark the last item
                    for(int i=0; i<requested_size; i++)
                        *(itPtr+i) = re_allocated_size -i;
                    for (int i=0; i<re_allocated_size; i++)
                        *(iReallocatePtr+i)= *(itPtr+i); // transfering to new
                    
					allocated_size = re_allocated_size;
				}
				
				else if(requested_size < (allocated_size/10.0))
				{
					// to re_allocate new array size as 1/4 allocated
					re_allocated_size = allocated_size/4; //Patern to shrink
					iReallocatePtr = new int[re_allocated_size];
                    
					cout << "The new allocated size is "<< re_allocated_size << endl;
                    
					itPtr = iReallocatePtr;
					for(int i=0; i<re_allocated_size; i++)
                        *itPtr++ = re_allocated_size -i;
					itPtr = iReallocatePtr;
					for(int i=0; i<re_allocated_size; i++)
                        cout << *itPtr++ << " ";
					cout << endl;
                    
					itPtr = iReallocatePtr;
					for(int i=0; i<re_allocated_size-1; i++)
                        *(itPtr+i)= 0;
					*(itPtr+(re_allocated_size-1)) = -1; // mark the last item
                    for(int i=0; i<requested_size; i++)
                        *(itPtr+i) = re_allocated_size -i;
                    for (int i=0; i<re_allocated_size; i++)
                        *(iReallocatePtr+i)= *(itPtr+i); // transfering to new
                    
					allocated_size = re_allocated_size;
				}
                
                // case 3. need for memory requires no re_allocation
                else //allocated size is not changed when the requested size is below 80% and above 10%
                {
                    iReallocatePtr = new int[re_allocated_size];
                    re_allocated_size = allocated_size;
                    itPtr = iReallocatePtr; // use the traversal pointer
                    for(int i=0; i<re_allocated_size-1; i++)
                        *(itPtr+i) = 0;
                    *(itPtr+(allocated_size-1)) = -1; // mark the last item
                    for(int i=0; i<requested_size; i++)
                        *(itPtr+i) = allocated_size -i;
                    for (int i=0; i<re_allocated_size; i++)
                        *(iReallocatePtr+i)= *(itPtr+i);
                    allocated_size = re_allocated_size;
                }
                
				// all cases to update the current_size as requested
				current_size = requested_size;
                
                displayArray(iReallocatePtr, current_size, allocated_size);
                for (int i=0; i<allocated_size; i++)
                    *(iUserPtr+i)= *(iReallocatePtr+i);
            }
            
            //calculation of mean
            if (ch == '4')
            {
                cout << std::fixed << std::showpoint ;
                cout << "The mean of the integers (size of "<< current_size<<") is: " << setprecision(2)
                << mean(iUserPtr, current_size) <<"."<< endl << endl;
                
            }
			
            if(ch == 'q') //the program will close when the user enter q
                stay = false;
        }
    }
    return 0;
}

//********************************************************************************************************

//fuction to display the numbers in the array
void displayArray(int *arr, int elems, int allo)
{
    cout << "The array is allocated with " << allo
    << "\nCurrent working size " << elems
    << "\nThe last item is marked as "
    << *(arr + allo -1)
    << "\nThe allocated array content is: ";
	for(int i=0; i<allo; i++) cout << *arr++ << " ";
	cout << endl << endl;
}

// function to search for mean
double mean(int *l, int s)
{
    double total = 0.0;
    double average = 0;
    for(int i=0;i<s;i++)
        total+= *(l+i); //calculate the total of numbers in user_array
    average = total/s;
    return average;
}
http://www.cplusplus.com/forum/general/112111/

valgrind informs that you are stepping out of bounds in line 170.
1
2
for (int i=0; i<allocated_size; i++)
   *(iUserPtr+i)= *(iReallocatePtr+i);


line 152 doesn't look good
1
2
3
// case 3. need for memory requires no re_allocation
iReallocatePtr = new int[re_allocated_size]; //¿why are you allocating memory then?
re_allocated_size = allocated_size; //¿shouldn't this be before? 
but your code is too obfuscated to analyse.
it could be improved by creating a class that encapsulates the behaviour
1
2
3
4
5
6
7
8
//ch == '3'
array.resize( requested_size );

//dynamic_array::resize( int new_size )
if( new_size > size*8/10 )
   this->reallocate( size*4 );
else if( requested_size < allocated_size/10.0 )
   this->reallocate( size/4 );



> How to use delete and delete[]?
you shouldn't need them.
for dynamic arrays, use `std::vector'
for single objects, prefer to use objects. If that's not possible, wrap it in an smart pointer.
Topic archived. No new replies allowed.