Question

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++ ?

Was it helpful?

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).

OTHER TIPS

The good C++ way is to use an appropriate container for that. Apparently, you should use the std::vector container, e.g:

std::vector<DeviceInfo> m_DeviceInfo;
m_DeviceInfo.resize(4);

This requires some constraints on your DeviceInfo. In particular, it should have a constructor without arguments, and copy constructors...

And your question is badly phrased. You certainly don't change sizeof(DeviceInfo*) which is probably 4 bytes on a 32 bits machine, and 8 bytes on a 64 bits one.

You have two options in your problem and it depends if you want to use STL or not.

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

} DeviceInfo;

With STL:

//requires vector.h
vector<DeviceInfo> m_deviceInfo;

DeviceInfo dummy;
dummy.name = "dummyName";
dummy.serialNumber = "1234"; 

m_deviceInfo.insert(m_deviceInfo.begin(), dummy); 
//add as many DeviceInfo instance you need the same way

or without STL:

//implement this 
DeviceInfo* reallocArray(DeviceInfo* arr, int curItemNum, int newItemNumber)
{
   DeviceInfo* buf = new DeviceInfo[newItemNumber];

   for(int i = 0; i < curItemNum; i++)
     buf[i] = arr[i];

   for(int i = curItemNum; i < newItemNumber; i++)
     buf[i] = null;
}

//and in your main code
DeviceInfo m_DeviceInfo = new DeviceInfo[4];

m_DeviceInfo = reallocArray( m_DeviceInfo, 4, 6 );

1) Make a new array of size that fits, and copy all elements of the old array to the new one.

2) Use the std::vector (my recommendation).

m_DeviceInfo points to an array of DeviceInfo of 4 elements. There is no resizing with arrays. Instead you should delete and create it with 6 elements.

DeviceInfo * m_DeviceInfo2 = new DeviceInfo[6]; 
memcopy( m_DeviceInfo,m_DeviceInfo2, 4 );
delete[] m_DeviceInfo;

But you should use a vector.

std::vector<DeviceInfo> m_DeviceInfo;
// or 
std::vector<std::shared_ptr<DeviceInfo>> m_DeviceInfo;

To resize it

m_DeviceInfo.resize(m_DeviceInfo.size()+ 2);

The best possible solution is using vector in your program.

Refer this site http://www.yolinux.com/TUTORIALS/LinuxTutorialC++STL.html#VECTOR

This site will help you to solve your problem.

Here you can push the data.No need to bother about the size of structure.

Your syntax is wrong:

DeviceInfo m_DeviceInfo = new DeviceInfo[4];

should be:

DeviceInfo* m_DeviceInfo = new DeviceInfo[4];

A better alternative would be the use of std::vector.

std::vector<DeviceInfo> vec;

//populate:
DeviceInfo inf;
vec.push_back(inf);
vec.push_back(inf);
//....

well there are several ways to do this, but you should use the realloc function in c++. it will reallocate contiguous memory and also copies the value of previous memory into the new ones. for example:

temp_DevInfo = (DeviceInfo*) realloc (m_DeviceInfo, (2) * sizeof(struct DeviceInfo));
free(m_DeviceInfo);
m_deviceInfo = temp_DevInfo;

you do 2 * sizeof(DeviceInfo) because you want to add 2 more, plus the previous 4 is 6. then you should free/delete the previous object. and finally set old pointer to point to the new object you just allocated.

that should be the gist of it

look at the documentation of realloc.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top