After java I study C ++, slowly I understand the removal of objects and pointers. Trying to write my ArrayList in cpp. When the internal array is filled, a new array is created, the data is added there, and then I clear the memory and transfer the pointer to the new array.

#include "ArrayList.h" #include <iostream> ArrayList::ArrayList() { lenght=0; sizeArr=10; values= new char[sizeArr];// } bool ArrayList::checkIndex(int index){ if(index<0 || index>lenght-1){ throw index; return false; } return true; } char* ArrayList::copyarray(char *src,int len, int newlenght){ char* newArr=new char[newlenght]; for(int i=0;i<len;i++){ newArr[i]=src[i]; } delete [] src; src=nullptr; src=newArr; return src; } char ArrayList::get(int index){ checkIndex(index); return values[index]; } void ArrayList::remove(int){ } int ArrayList::indexOf(const char* val){ return 0; } void ArrayList::add(const char* val){ values[lenght]=val; lenght++; if(lenght==sizeArr){ sizeArr=(sizeArr*2)-(sizeArr/2); copyarray(values,lenght,sizeArr); } } int ArrayList::size(){ return lenght; } bool ArrayList::isEmpty(){ return lenght==0; } bool ArrayList::conteins(const char* val){ return indexOf(val)!=-1; } int ArrayList::hashCode(){ return 1; } ArrayList::~ArrayList() { //delete values; } 

main method:

  ArrayList list; list.add("hello"); list.add("hello1"); list.add("hello2"); list.add("hello3"); list.add("hello"); list.add("hello1"); list.add("hello2"); list.add("hello3"); list.add("hello"); list.add("hello1"); list.add("hello2"); list.add("hello3"); list.add("hello"); list.add("hello1"); list.add("hello2"); list.add("hello3"); for(int i=0;i<list.size();i++) cout<<list.get(i)<<endl 

If I do not delete the old array in the copyarray method, then everything is displayed normally, and if I delete, then some added values ​​and some garbage are output.

  • one
    This code will not even compile, and you tell about some kind of "output" ... - AnT February
  • A lot of errors in each method - AR Hovsepyan

2 answers 2

values= new char[sizeArr];

If values are char * , then values[index] already char .

values[lenght]=val;

And you are trying to write const char * . In addition, you can not char * assign const char * . You need to make an array of pointers char **values; , allocate memory for a new line and copy it there. Example:

 #include<iostream> #include<cstring> class ArrayList { private: std::size_t array_size; std::size_t capacity; char **values; void array_realloc() { std::size_t new_capacity = std::size_t(capacity * 1.5); char **new_values = new char*[new_capacity]; for (std::size_t i = 0; i < array_size; i++) { new_values[i] = values[i]; } delete[] values; values = new_values; capacity = new_capacity; } public: // инициализация. capacity должен быть объявлен в классе раньше values ArrayList() : array_size(0), capacity(10), values(new char*[capacity]) {} ~ArrayList() { for (std::size_t i = 0; i < array_size; i++) { delete[] values[i]; } delete[] values; } void add(const char* val) { if (array_size == capacity) { array_realloc(); } std::size_t val_len = strlen(val) + 1; //+1 для '\0' values[array_size] = new char[val_len]; std::strncpy(values[array_size], val, val_len); array_size++; } std::size_t size() const { return array_size; } char* get(std::size_t index) { if (index >= array_size) throw "Bad index"; return values[index]; } }; int main() { ArrayList a; a.add("123"); a.add("qwerty"); a.add("zxcvb"); a.add("asdf"); a.add("lkhfgf"); for (int i = 0; i < a.size(); i++) { std::cout << a.get(i) << std::endl; } return 0; } 
  • The array of pointers is not obligatory at all - AR Hovsepyan
  • @ARHovsepyan and what do you suggest? - Drawn Raccoon
  • you can simply take a section of memory sufficient to record the expected result in it and remove the old one - AR Hovsepyan
 delete [] src; src=nullptr; src=newArr; return src; 

Here, the second and third lines are useless, and what the 4th one does is not used anywhere.

  • And so I throw my pointer in the third line to a new address in memory, I hope :) - Amos
  • @Amos, hoping in vain. - Qwertiy 3:04 pm
  • And how to be right? - Amos
  • @Amos, in this case the same as in Java. - Qwertiy