-1

I have array listetud, the size of this array is 10, i want to extend the size of the array every time it becomes full

class Ecole{
         
         string listetud[10];
         int taillelistetud;
    public:
         void extendlistetud(int taillelistetud){
             listetud[10+taillelistetud];//is this correct?
          }
463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
  • 14
    you cannot change the size of an array. Arrays have fixed size. Use `std::vector` – 463035818_is_not_an_ai Jan 11 '22 at 13:58
  • `listetud[10+taillelistetud];//is this correct?` Absolutely not, this is undefined behavior / you are referencing past the end of the array of 10 elements. The size of an array is fixed at compile time by the c++ standard. – drescherjm Jan 11 '22 at 14:02
  • Using old-style dynamic allocation, you can use `realloc`. Using C++ operators, allocate an extra array, copy the relevant elements, swap the pointers and deallocate the previous. –  Jan 11 '22 at 14:02
  • 1
    @YvesDaoust • a `realloc` on an array of `std::string` is not a good idea. – Eljay Jan 11 '22 at 14:20
  • @Eljay: presumably UB, but why would it fail ? –  Jan 11 '22 at 14:54
  • @YvesDaoust • *why would it fail?* Because it is UB. – Eljay Jan 11 '22 at 15:09
  • @Eljay: I mean can you think of an implementation that makes it fail ? –  Jan 11 '22 at 15:30
  • @YvesDaoust • Yes. All of the implementations I've ever used. Due to small string optimization, the string may have pointers to its own internals; when the bytes are copied into the realloc'd block of memory, those pointers are dangling. The new bytes for the new string objects have not had a string constructed there, which is going to go badly when trying to use those bytes as if there was a string object there. – Eljay Jan 11 '22 at 15:50
  • @YvesDaoust *I mean can you think of an implementation that makes it fail ?* -- IMO, that request is an unreasonable one. There are 3 major compilers, g++, clang, and Visual C++. For each of those compilers, there are multiple options can be used to build a program. For each of those compilers, there are different versions. Now, who would have the time to research all of these combinations and future combinations to test for `realloc` failure on `std::string`? Or would it just be simpler to not use `realloc` on an array of `std::string`? – PaulMcKenzie Jan 11 '22 at 16:25
  • @PaulMcKenzie: I disagree, it is a matter of common sense. Realloc will just move the string descriptors to new locations, what difference can it make ? (I am not asking a formal proof, and will not sue you if you are wrong.) –  Jan 11 '22 at 17:19

1 Answers1

-1

You CAN NOT change the size of the "fixed size" array

You need to use dynamic memory and request the amount of memory you need with the help of new

If you need to change the size dynamically when running the program You must use data structures such as linked lists (vector)

Note that the vector is not linked list But you can use vectors for what you want to do You can read the difference between the two here

DariushStony
  • 153
  • 8