Display unchanged array when I call it | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Display unchanged array when I call it

I insert a element into array and have a function that returns me the value where the insertion but I am getting the value wrong when I ask for its position. Look at the code https://code.sololearn.com/cX8AuakKH4y6/?ref=app

23rd Apr 2022, 12:29 AM
Yones Mussa
8 Answers
+ 5
If instead of copying, you pass &array you can make array_ as address of array. Then, you can modify what the array points to when it gets bigger or smaller.
23rd Apr 2022, 1:07 AM
John Wells
John Wells - avatar
+ 3
The vector copies the array into its own storage and forgets it existed.
23rd Apr 2022, 12:40 AM
John Wells
John Wells - avatar
+ 3
Write your own Vector class.
23rd Apr 2022, 12:48 AM
John Wells
John Wells - avatar
+ 2
Line 27
23rd Apr 2022, 1:02 AM
John Wells
John Wells - avatar
+ 2
Apart from what John Wells said, I see many problems in the code. 1. The constructor that is the spotlight of this question is not a good one. A vector class should maintain its own buffer and not allow users to give it the control of an unknown buffer. 2. In the copy constructor, you are allocating a buffer of x.size_ elements, but setting the capacity to x.capacity_. This is a blunder if x.size_ is not equal to x.capacity_ 3. In the constructor on line 12, copy-constructor and copy-assign operator, you are not deallocting the already existing buffer. This id a memory leak. Keep in mind that just doing `delete[] this->array;` Will call the destructor on each element in the vector 4. In the destructor, you are deallocting the buffer when array_ IS nullptr, instead of when array_ IS NOT nullptr
23rd Apr 2022, 6:53 AM
XXX
XXX - avatar
0
John Wells how would i be able to resolve this problem?
23rd Apr 2022, 12:44 AM
Yones Mussa
0
I dont see where im storing the arrays into a vector
23rd Apr 2022, 12:50 AM
Yones Mussa
0
Getting an error that we cant asign to it unknow size
23rd Apr 2022, 1:21 AM
Yones Mussa