Pointer Practice

Hi, I am currently experimenting with pointers and classes. Everytime my pointer reach 80% of its size, I attempt to create a expand it's capacity by 100% by using a temporary array pointer. However, I have come across an error that states 'pointer being freed was not allocated pointer practce(8217,0x10008c580) malloc: *** set a breakpoint in malloc_error_break to debug.' To my knowledge, I allocated the array, could someone please take a look at this? Thank you
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

#include <iostream>

class Practice
{
private:
    int *array;
    int index;
    std::size_t size;
    
    
public:
    
    Practice(std::size_t size)
    {
        this->size = size;
        array =  new int [this->size];
        index = 0;
    }
    
    ~Practice()
    {
        delete [] array;
    }
    
    void Insert(int number)
    {
        array[index++] = number;
        if(double(index) / double(this->size) == 0.8)
        {
            this->size *= 2;
            int * newArray =  new int [this->size];
            for(int i = 0; i < index; i++)
                newArray[i] = array[i];
            
            delete [] array;
            array = new int [this->size];
            array = newArray;
            delete [] newArray;
        }
        
            
    }
    
    void Print()
    {
        for(int i = 0; i < index; i++)
            std::cout << array[i] << ' ';
        
        std::cout << std::endl;
    }
    
    std::size_t GetSize()
    {
        return this->size;
    }
    
    
};

int main(int argc, const char * argv[]) {
    
    int count;
    
    Practice p(10);
    for(count = 0; count < 9; count++)
        p.Insert(count+1);
    
        p.Print();
        std::cout << std::endl;
       
    for(int i = count; i < 20; count++)
        p.Insert(count+1);
    
    return 0;
}
> if(double(index) / double(this->size) == 0.8)
This should be >=, not ==


Too much here.
1
2
3
4
            delete [] array;
            array = new int [this->size];
            array = newArray;
            delete [] newArray;

It should be
1
2
            delete [] array;  // throw away the old
            array = newArray; // keep the new 
Last edited on
Works perfectly thank you!
There's also an error on L72...

Also the code can be somewhat simplified. Consider:

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
#include <iostream>
#include <algorithm>

class Practice {
private:
	int* array {};
	size_t index {};
	size_t size {};

public:
	Practice(std::size_t s) :size(s), array(new int[s]) {}

	~Practice() {
		delete[] array;
	}

	Practice(const Practice&) = delete;
	Practice& operator=(const Practice&) = delete;

	void Insert(int number) {
		array[index++] = number;

		if (double(index) / size >= 0.8) {
			const auto newArray { new int[size *= 2] };

			std::copy_n(array, index, newArray);
			delete[] array;
			array = newArray;
		}
	}

	void Print() const {
		std::copy_n(array, index, std::ostream_iterator<int>(std::cout, " "));
		std::cout << '\n';
	}

	size_t GetSize() const {
		return size;
	}
};

int main() {
	Practice p(10);
	int count {};

	for (; count < 9; ++count)
		p.Insert(count + 1);

	p.Print();

	for (int i { count }; i < 20; ++i)
		p.Insert(i + 1);

	p.Print();
}



1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

Topic archived. No new replies allowed.