0

I have made a code in which i am creating array of objects using new.I am extending the array by another new keyword and last index of previous array like following.Its working fine(The constructor is printing "a" number of times the net index). But i dont know is it safe or not? My main motive is to make a dynamic array but i cant use malloc because it dows not call constructors.So i am using new. But idont know how to reallocate memory after using new. If i use realloc then the program is not giving any error but realloc is not calling the constructor.

Summary:->Just want to call constructor while reallocating memory.

I have given the image of my code

class xy
{
    unsigned x[10],y[10],counter;
public:
    xy()
    {counter=0;}
    void setxy(unsigned a,unsigned b)
    {x[counter]=a;y[counter]=b;counter++;}
    void printxy()
    {for(unsigned i=0;i<counter;i++)cout<<"="<<x[i]<<"^3+"<<y[i]<<"^3";}
};
class unitcell
{
    unsigned long long value_cube,hasharr_len;
    xy* hasharr;
    unsigned long long* r_numberindexes;
    unsigned rarraylen;
public:
    unitcell()
    {cout<<"a";}
    unsigned long long gethasharraylen()
    {return hasharr_len;}
};
int main()
{
    unitcell *a=new unitcell[3];
    unitcell *b=a+3;
    b=new unitcell[3];
}
Aniket Bose
  • 55
  • 1
  • 8
  • You should never mix c functions like `malloc`, `realloc`, `calloc`, `free` with the c++ `new`, `new[]`, `delete`, `delete[]` operators for the same allocation. – Maciej Dziuban Sep 13 '20 at 12:14
  • Then how can i reallocate memory which is made by new???? – Aniket Bose Sep 13 '20 at 12:15
  • Please remove the `>` sign at the start of each line in your code. Those makes it impossible to compile it - and _why_ would an image of the code be useful? Also, not using whitespaces (like newlines and indentation etc.) does not make your code faster or anything. It only makes it hard to read. – Ted Lyngmo Sep 13 '20 at 12:18
  • Modern C++ provides smart containers to do the memory management for you. Such as `std::vector` and `std::unique_ptr` and `std::string`. Unless you are making a template container yourself, or are working in an environment was specific memory allocation needs, you ought not need to use `new` or `malloc` directly at all. – Eljay Sep 13 '20 at 12:28
  • `unitcell` is not TriviallyCopyable, so using `realloc` is not valid. If you need to have a resizable container with continuous storage for objects that are not TriviallyCopyable you have to use `std::vector`. – t.niese Sep 13 '20 at 13:19

1 Answers1

2

No.

First you're initializing b to the value of a+3 and then you're assigning the result of new to it, which will be a completely different value. You have no control over where your allocated memory will end up. You should assume each new invocation gives you a completely random address, whose value you cannot predict.

If you want to "extend" your array you have to allocate a bigger one, copy all the elements you already had and then delete the old one

unitcell *b = new unitcell[6];
std::copy(a, a+3, b);
delete a;
a = b;

STL class std::vector works in this way internally.

Maciej Dziuban
  • 474
  • 3
  • 21
  • 1
    `std::vector a(3); a.resize(6);` – anatolyg Sep 13 '20 at 12:12
  • I dont want to copy all the elements I already had. I want to use realloc because only if realloc can't extend memory then it copy all the elements from first to last and gives new address. But if it can extend in the current addres then it return current addres. – Aniket Bose Sep 13 '20 at 12:15
  • AFAIK there's no such thing in c++. If you want to use `realloc`, you have to allocate your memory with `malloc`, not `new`. – Maciej Dziuban Sep 13 '20 at 12:16
  • @AniketBose using `realloc` is only valid on memory allocated with `malloc` and only valid if the memory holds TriviallyCopyable. `std::vector` is the way how you do it in c++. – t.niese Sep 13 '20 at 13:23