Search code examples
c++structnew-operatordynamic-allocation

Dynamically Changing the Size of the Pointer in C++


I have the following structure

typedef struct DeviceInfo
{
    char[30] name;
    char[30] serial Number;

}DeviceInfo;

I am doing this    

DeviceInfo* m_DeviceInfo = new DeviceInfo[4];

// Populate m_DeviceInfo 

Then I wanted to re size m_DeviceInfo to 6 and want to preserve the Previous 4 Value as well.

How to do it in c++ ?


Solution

  • You can't do that with regular arrays. I suggest you to use vector which is able to grow as you add more elements to it (so you don't even have to specify initial size).