How Could I Reallocate An Array Of Pointers So That Its Size Is Bigger

How Could I Reallocate An Array Of Pointers So That Its Size Is Bigger?

I have a class which initialises an array of pointers however I wish for their size to be incremented when needed so I am not limited to the size defined when ran.

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

//Part Of A Class
private:
	int size = 0;
	int maxSize = 10;
	Node* arrOfPointers[10];


//Part Of A Method Of The Class
			size_t newSize = size * 2;
			Node* newArrOfPointers = new Node*[newSize];
			memcpy(newArrOfPointers, arrOfPointers, size * sizeof(Node*));
			maxSize = newSize;
			arrOfPointers = newArrOfPointers;



The Error says I cannot convert from Node** to Node* but I don't understand how I can get it to replace the original array so only the size of the array is different.
Store you pointers in a container like std::vector<Node*> - this is a container whose size can be dynamically changed with push_back(), emplace_back(), resize() etc methods.

However it is efficient only to add new entries at the back of vectors. If you want efficiency for both front and back addition/deletion consider std::deque<Node*> and for addition/deletion at any point consider std::List<Node*>.

Also bear in mind you're working with raw/C-style pointers that need to delete their managed objects prior to going out of scope. You can avoid this in C++11 by working with smart pointers like std::unique_ptr or std::shared_ptr. So a vector of unqiue_ptrs would look like std::vector<std::unique_ptr<Node>> etc.
Last edited on
Topic archived. No new replies allowed.