Hello! There is a din. array of n elements. It is necessary to expand it ... Do not just take and allocate the newly created array in another area of ​​memory, but expand the existing one (add new elements to the tail) ... How to do this? Did this:

`d_WB=(UserData*)realloc(d_WB, count_of_WB_herroy*sizeof(UserData));` 

I climbed in memory and, it seems, the array is not expanding, but is being re-created ...

  • 2
    Yes, it will be recreated if there is not enough memory. Expand does not always work. Can allocate obviously more memory? By the way, if you use it with ++, use std::vector<UserData> - KoVadim

2 answers 2

To begin with: in C ++, malloc and realloc are not used, memory is allocated through new / delete . Manual control of memory is also a prerogative of pure C, in C ++ there is std::vector especially for your case.

Yes, and expanding the array is guaranteed in the same place in principle is impossible: what if the memory immediately behind the array is already taken?

If you need this, you have something wrong in the design. Tell us what real purpose you are pursuing. Surely this is done differently.

  • > Yes, and in principle it is impossible to expand the array in the same place: what if there is? contradict yourself a little. - KoVadim
  • in my box2d the address of the beginning of the array is rigidly clogged. In general, this is the problem ... I will allocate obviously more memory ... Thank you! - Alerr
  • Yes, inaccurately expressed. I will reformulate. - VladD
  • @Alerr: why do you have the address of the array, and not std::vector ? - VladD

As far as I know, a new array is created in vector, twice the previous one, rewritten and the necessary amount of data is added to it, and the previous one is deleted.